## DistilleryCog Class The `DistilleryCog` class is a subclass of `commands.Cog` from the `discord.py` library [[discord.py library]]. It extends the functionality of the bot to include the ability to create and manage slash commands. ### Initialization The `__init__` method initializes the `DistilleryCog` instance: ```python def __init__(self, bot): request_id = "N/A" try: self.bot = bot self.bot.loop.create_task(self.setup_commands()) except Exception as e: raise ``` It takes the `bot` instance as a parameter, assigns it to `self.bot`, and creates a task to set up the commands using the `setup_commands` method. ### Setting Up Commands The `setup_commands` method is responsible for creating command functions based on the `DISTILLERY_COMMANDS` [[Distillery Commands]] configuration: ```python async def setup_commands(self): request_id = "N/A" try: aws_manager = await distillery_aws.AWSManager.get_instance() print("Creating command functions...") for distillery_command in DISTILLERY_COMMANDS: print(f"-> Creating command function {distillery_command['COMMAND_NAME']}...") await self.create_slashcommand(distillery_command['COMMAND_NAME'], distillery_command['COMMAND_DESCRIPTION'], distillery_command['COMMAND_ARGS']) aws_manager.print_log('N/A', INSTANCE_IDENTIFIER, f"Command function {distillery_command['COMMAND_NAME']} created.", level='INFO') except Exception as e: formatted_exception = better_exceptions.format_exception(*sys.exc_info()) formatted_traceback = ''.join(formatted_exception) aws_manager.print_log(request_id, INSTANCE_IDENTIFIER, formatted_traceback, level='ERROR') ``` It iterates over each command in `DISTILLERY_COMMANDS` and calls the `create_slashcommand` (see bellow) method to create the corresponding slash command. If it is successfully created it prints that the function was created in the aws log. If there was an error, the error is printed instead. [[better_exceptions library]] ### Creating Slash Commands The `create_slashcommand` method dynamically creates a slash command function based on the provided command details. Given that this is a larger chunk of code let's take a look at it closer [[create_slashcommand]] ### Handling Member Updates The `on_member_update` function is an asynchronous event listener in a Discord bot that handles updates to a member's roles. When a member's roles change, the function calculates the new and removed roles, generates a unique request ID, and obtains a lock using AWS Memcached to ensure thread safety. The function then validates the user's presence in the server and updates the user's "Distill" credits based on the newly assigned roles. If the new roles correspond to specific product or subscription tiers, it logs these changes and updates the credits accordingly. Finally, it determines the user's highest subscription tier and updates their user type in the database. If any exceptions occur during the process, they are raised for further handling. Let's take a closer look at the code [[on_member_update]] ### On Ready Event The `on_ready` method is a listener that triggers when the bot successfully connects to Discord: ```python @commands.Cog.listener() async def on_ready(self): request_id = "N/A" try: print('-----------------START-OF-BOT---------------------') print(f'{bot.user} has connected to Discord!') aws_manager = await distillery_aws.AWSManager.get_instance() aws_manager.setup_logging() # Set up logging configuration here aws_manager.print_log('N/A', INSTANCE_IDENTIFIER, f"Bot started!", level='INFO') scheduler.start() await check_queue_and_send.start() except asyncio.CancelledError: aws_manager.print_log('N/A', INSTANCE_IDENTIFIER, "Main function cancelled.", level='INFO') except Exception as e: formatted_exception = better_exceptions.format_exception(*sys.exc_info()) formatted_traceback = ''.join(formatted_exception) aws_manager.print_log(request_id, INSTANCE_IDENTIFIER, formatted_traceback, level='ERROR') finally: print('------------------END-OF-BOT----------------------') ``` It sets up logging, starts the scheduler, and initiates the `check_queue_and_send` task to periodically check the send queue and process any pending requests. **The `DistilleryCog` class provides the core functionality for managing slash commands, handling member updates, and setting up the bot when it connects to Discord.**