|
| 1 | +# standard imports |
| 2 | +import asyncio |
| 3 | +import os |
| 4 | +import threading |
| 5 | + |
| 6 | +# lib imports |
| 7 | +import discord |
| 8 | + |
| 9 | +# local imports |
| 10 | +from src.common import bot_name, get_avatar_bytes, org_name |
| 11 | +from src.discord.tasks import daily_task |
| 12 | +from src.discord.views import DonateCommandView |
| 13 | + |
| 14 | + |
| 15 | +class Bot(discord.Bot): |
| 16 | + """ |
| 17 | + Discord bot class. |
| 18 | +
|
| 19 | + This class extends the discord.Bot class to include additional functionality. The class will automatically |
| 20 | + enable all intents and sync commands on startup. The class will also update the bot presence, username, and avatar |
| 21 | + when the bot is ready. |
| 22 | + """ |
| 23 | + def __init__(self, *args, **kwargs): |
| 24 | + if 'intents' not in kwargs: |
| 25 | + intents = discord.Intents.all() |
| 26 | + kwargs['intents'] = intents |
| 27 | + if 'auto_sync_commands' not in kwargs: |
| 28 | + kwargs['auto_sync_commands'] = True |
| 29 | + super().__init__(*args, **kwargs) |
| 30 | + |
| 31 | + self.bot_thread = threading.Thread(target=lambda: None) |
| 32 | + self.token = os.environ['DISCORD_BOT_TOKEN'] |
| 33 | + |
| 34 | + self.load_extension( |
| 35 | + name='src.discord.cogs', |
| 36 | + recursive=True, |
| 37 | + store=False, |
| 38 | + ) |
| 39 | + |
| 40 | + async def on_ready(self): |
| 41 | + """ |
| 42 | + Bot on ready event. |
| 43 | +
|
| 44 | + This function runs when the discord bot is ready. The function will update the bot presence, update the username |
| 45 | + and avatar, and start daily tasks. |
| 46 | + """ |
| 47 | + print(f'py-cord version: {discord.__version__}') |
| 48 | + print(f'Logged in as {self.user.name} (ID: {self.user.id})') |
| 49 | + print(f'Servers connected to: {self.guilds}') |
| 50 | + |
| 51 | + # update the username and avatar |
| 52 | + avatar_img = get_avatar_bytes() |
| 53 | + if await self.user.avatar.read() != avatar_img or self.user.name != bot_name: |
| 54 | + await self.user.edit(username=bot_name, avatar=avatar_img) |
| 55 | + |
| 56 | + await self.change_presence( |
| 57 | + activity=discord.Activity(type=discord.ActivityType.watching, name=f"the {org_name} server") |
| 58 | + ) |
| 59 | + |
| 60 | + self.add_view(DonateCommandView()) # register view for persistent listening |
| 61 | + |
| 62 | + await self.sync_commands() |
| 63 | + |
| 64 | + try: |
| 65 | + os.environ['DAILY_TASKS'] |
| 66 | + except KeyError: |
| 67 | + daily_task.start(bot=self) |
| 68 | + else: |
| 69 | + if os.environ['DAILY_TASKS'].lower() == 'true': |
| 70 | + daily_task.start(bot=self) |
| 71 | + else: |
| 72 | + print("'DAILY_TASKS' environment variable is disabled") |
| 73 | + |
| 74 | + def start_threaded(self): |
| 75 | + try: |
| 76 | + # Login the bot in a separate thread |
| 77 | + self.bot_thread = threading.Thread( |
| 78 | + target=self.loop.run_until_complete, |
| 79 | + args=(self.start(token=self.token),), |
| 80 | + daemon=True |
| 81 | + ) |
| 82 | + self.bot_thread.start() |
| 83 | + except KeyboardInterrupt: |
| 84 | + print("Keyboard Interrupt Detected") |
| 85 | + self.stop() |
| 86 | + |
| 87 | + def stop(self, future: asyncio.Future = None): |
| 88 | + print("Attempting to stop daily tasks") |
| 89 | + daily_task.stop() |
| 90 | + print("Attempting to close bot connection") |
| 91 | + if self.bot_thread is not None and self.bot_thread.is_alive(): |
| 92 | + asyncio.run_coroutine_threadsafe(self.close(), self.loop) |
| 93 | + self.bot_thread.join() |
| 94 | + print("Closed bot") |
| 95 | + |
| 96 | + # Set a result for the future to mark it as done (unit testing) |
| 97 | + if future and not future.done(): |
| 98 | + future.set_result(None) |
0 commit comments