From a0319a4097a021596eb8e9f061d6986e69ce592e Mon Sep 17 00:00:00 2001 From: ElBe <90863907+ElBe-Plaq@users.noreply.github.com> Date: Wed, 14 Sep 2022 09:27:57 +0200 Subject: [PATCH] Update bot.py --- Bot/bot.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/Bot/bot.py b/Bot/bot.py index bb8b3dc..7e40e65 100644 --- a/Bot/bot.py +++ b/Bot/bot.py @@ -2,7 +2,7 @@ Bot from The Guardians. © by ElBe. -Version: 0.1.0 +Version: 0.1.3 ''' #Imports @@ -16,7 +16,6 @@ import time import logging import platform import psutil -from prettytable import PrettyTable from random import randint, choice #Bot modules @@ -44,3 +43,86 @@ MISSING = utils.MISSING #Starttime starttime = time.time() + +#JSON data +token = functions.json_module.get_config('Config')['Token'] +version = functions.json_module.get_config('Config')['Version'] +credits = functions.json_module.get_config('Config')['Credits'] +icon = functions.json_module.get_config('Images')['Logo'] +commands = functions.json_module.get_config('Commands') + +#Setup +logging.basicConfig(filename='log.txt', level=logging.INFO) +intents = discord.Intents.all() + +#Main +class TheGuardiansBot(discord.Client): + '''Bot.''' + + async def on_connect(self): + logging.info(str(datetime.datetime.now()) + 'Bot connected to the Discord API.') + print(functions.console.info('Bot connected to the Discord API.')) + + async def on_ready(self): + logging.info(str(datetime.datetime.now()) + 'Bot logged in as ' + client.user.name + '.') + print(functions.console.info('Bot logged in as ' + client.user.name + '.')) + print('') + print('Log (Consolebased)') + print('------------------') + + while True: + await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f'/help for help.')) + await asyncio.sleep(10) + await client.change_presence(activity=discord.Game(name=' with The Guardians on 6b6t.')) + await asyncio.sleep(10) + if credits: + await client.change_presence(activity=discord.Game(name=' programmed by ElBe.')) + await asyncio.sleep(10) + + async def on_resumed(self): + logging.info(str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- Bot resumed seesion.') + print(functions.console.log('Bot resumed a session.')) + + #Commands + + async def on_member_join(self, member): + async def log(text: str): + '''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)) + print(functions.console.log(str(text))) + + if not str(member) == client.user: + channel = discord.utils.get(member.guild.text_channels, name="hi") + 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_footer(text='Bot made by ElBe for The Guardians.') + await channel.send(embed=joinEmbed) + await member.add_roles(discord.utils.get(member.guild.roles, name="member")) + await log('@' + str(member) + ' joined to ' + str(member.guild.name) + '.') + + async def on_member_remove(self, member): + async def log(text: str): + '''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)) + print(functions.console.log(str(text))) + + if not str(member) == client.user: + channel = discord.utils.get(member.guild.text_channels, name="bye") + joinEmbed = discord.Embed(title='Goodbye!', description='<@!' + str(member.id) + f'> left {member.guild.name}.') + joinEmbed.set_thumbnail(url=member.avatar.url) + joinEmbed.set_footer(text='Bot made by ElBe for The Guardians.') + await channel.send(embed=joinEmbed) + await log('@' + str(member) + ' left ' + str(member.guild.name) + '.') + + async def on_disconnect(self): + async def error(text: str): + '''Send a error text and save it in the logfile.''' + logging.info(str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- Error: ' + str(text)) + print(functions.console.error(str(text))) + + await error('Bot disconected from discord.') + + +#Run +client = TheGuardiansBot(intents = intents, max_messages=None) +client.run(token)#, log_handler=None)