Updated to v1.9.3

This commit is contained in:
ElBe 2022-11-12 22:11:04 +01:00 committed by GitHub
parent 2380cf4d67
commit bf4a851f7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
Discord bot template. Discord bot template.
© by ElBe. © by ElBe.
Version: 0.1.9 Version: 1.9
''' '''
#Imports #Imports
@ -14,10 +14,18 @@ import time
import logging import logging
import platform import platform
import psutil import psutil
import requests
#Bot modules #Bot modules
import functions import functions
import commands import errors
import commands as create_commands
import version as bot_version
#Check modules and python
if not int(platform.python_version().replace('.','')) <= 3110:
errors.OutdatedVersionError('Python', '3.11.0 or higher', 'https://python.org')
#Start #Start
print('Discord.py Bot') print('Discord.py Bot')
print('----------------------------') print('----------------------------')
@ -26,7 +34,7 @@ print('')
print('Start Informations') print('Start Informations')
print('------------------') print('------------------')
print('Discord version: ' + discord.__version__) print('Discord version: ' + discord.__version__)
print('Bot version: ' + functions.json_module.get_config('Config')['Version']) print('Bot version: ' + bot_version.version)
print('') print('')
print('Starting') print('Starting')
print('--------') print('--------')
@ -44,14 +52,16 @@ starttime = time.time()
#JSON data #JSON data
try: try:
token = functions.json_module.get_config('Config')['Token'] token = functions.json_module.get_config('Config')['Token']
version = functions.json_module.get_config('Config')['Version'] version = bot_version.version
credits = functions.json_module.get_config('Config')['Credits'] credits = functions.json_module.get_config('Config')['Credits']
footer = functions.json_module.get_config('Config')['Footer'] footer = functions.json_module.get_config('Config')['Footer']
memberRole = functions.json_module.get_config('Roles')['Member']
welcomeChannel = functions.json_module.get_config('Channels')['Welcome'] welcomeChannel = functions.json_module.get_config('Channels')['Welcome']
goodbyeChannel = functions.json_module.get_config('Channels')['Goodbye'] goodbyeChannel = functions.json_module.get_config('Channels')['Goodbye']
commands = functions.json_module.get_config('Commands') commands = functions.json_module.get_config('Commands')
except Exception as e: except Exception as e:
print(functions.console.error('Error while trying to get data from the config file.\n' + str(e))) print(functions.console.error('Error while trying to get data from the config file.\n' + str(e)))
exit()
#Setup #Setup
logging.basicConfig(filename='log.txt', level=logging.INFO) logging.basicConfig(filename='log.txt', level=logging.INFO)
@ -60,9 +70,12 @@ intents = discord.Intents.all()
#Create commands #Create commands
try: try:
if functions.json_module.get_config('Created', 'commands.json') == 0: if functions.json_module.get_config('Created', 'commands.json') == 0:
commands.run() create_commands
else:
None
except Exception as e: except Exception as e:
print(functions.console.error('Error while trying to create the commands.\n' + str(e))) print(functions.console.error('Error while trying to create the commands.\n' + str(e)))
exit()
#Main #Main
class Bot(discord.Client): class Bot(discord.Client):
@ -234,26 +247,30 @@ class Bot(discord.Client):
await interaction.response.send_message(embed=commandDisabledEmbed, ephemeral=True) await interaction.response.send_message(embed=commandDisabledEmbed, ephemeral=True)
async def on_member_join(self, member): async def on_member_join(self, member):
if not welcomeChannel == "":
async def log(text: str): async def log(text: str):
'''Log a text and save it in the logfile and the console.''' '''Log a text and save it in the logfile and the console.'''
logging.info(str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- ' + str(text)) logging.info(str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- ' + str(text))
print(functions.console.log(str(text))) print(functions.console.log(str(text)))
if not welcomeChannel == '':
if not member == client.user: if not member == client.user:
channel = discord.utils.get(member.guild.text_channels, name=welcomeChannel) channel = discord.utils.get(member.guild.text_channels, name=welcomeChannel)
joinEmbed = discord.Embed(title='Welcome!', description='Hello <@!' + str(member.id) + f'>! \nThank you for joining {member.guild.name}!') joinEmbed = discord.Embed(title='Welcome!', description='Hello <@!' + str(member.id) + f'>! \nThank you for joining {member.guild.name}!')
joinEmbed.set_thumbnail(url=member.avatar.url) joinEmbed.set_thumbnail(url=member.avatar.url)
joinEmbed.set_footer(text=footer) joinEmbed.set_footer(text=footer)
await channel.send(embed=joinEmbed) await channel.send(embed=joinEmbed)
await member.add_roles(discord.utils.get(member.guild.roles, name='member')) await member.add_roles(discord.utils.get(member.guild.roles, name=str(member)))
await log('@' + str(member) + ' joined to ' + str(member.guild.name) + '.') await log('@' + str(member) + ' joined to ' + str(member.guild.name) + '.')
async def on_member_remove(self, member): async def on_member_remove(self, member):
if not goodbyeChannel == "":
async def log(text: str): async def log(text: str):
'''Log a text and save it in the logfile and the console.''' '''Log a text and save it in the logfile and the console.'''
logging.info(str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- ' + str(text)) logging.info(str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- ' + str(text))
print(functions.console.log(str(text))) print(functions.console.log(str(text)))
if not goodbyeChannel == '':
if not member == client.user: if not member == client.user:
channel = discord.utils.get(member.guild.text_channels, name=goodbyeChannel) channel = discord.utils.get(member.guild.text_channels, name=goodbyeChannel)
leaveEmbed = discord.Embed(title='Goodbye!', description='<@!' + str(member.id) + f'> left {member.guild.name}.') leaveEmbed = discord.Embed(title='Goodbye!', description='<@!' + str(member.id) + f'> left {member.guild.name}.')
@ -271,8 +288,12 @@ class Bot(discord.Client):
await error('Bot disconected from discord.') await error('Bot disconected from discord.')
#Run #Run
try: client = Bot(intents = intents, max_messages=None)
client = Bot(intents = intents, max_messages=None) new_version = str(str(str(requests.get('https://raw.githubusercontent.com/ElBe-Plaq/discord.py-bot-template/main/version.py').text).replace('version = ', '')).replace('\n', '')).replace('\'', '')
if version == new_version:
try:
client.run(token, log_handler=None) client.run(token, log_handler=None)
except Exception as e: except discord.errors.DiscordException:
print(functions.console.error('Error while trying to connect to discord.\n' + str(e))) errors.APIError('Error while trying to connect to discord.')
else:
errors.OutdatedVersionError('the template', new_version, 'https://github.com/ElBe-Plaq/discord.py-bot-template')