|
| 1 | +""" |
| 2 | +Main script to run |
| 3 | +
|
| 4 | +This script initializes cogs and starts the bot |
| 5 | +
|
| 6 | +Code taken from my contributions in: |
| 7 | +https://github.com/savioxavier/repo-finder-bot/ |
| 8 | +Additional thanks to savioxavier |
| 9 | +""" |
| 10 | +import os |
| 11 | +import sys |
| 12 | + |
| 13 | +import discord |
| 14 | +from discord.ext import commands |
| 15 | +from discord.ext.commands.errors import ExtensionFailed, NoEntryPointError |
| 16 | +from discord_slash import SlashCommand |
| 17 | +from dotenv import load_dotenv |
| 18 | + |
| 19 | +from src import logutil |
| 20 | +from config import DEBUG, DEBUG_DISCORD |
| 21 | +from config import PREFIX as bot_prefix |
| 22 | + |
| 23 | +load_dotenv() |
| 24 | + |
| 25 | +# Configure logging for this main.py handler |
| 26 | +logger = logutil.init_logger("main.py") |
| 27 | + |
| 28 | +# Configure logging for Discord.py |
| 29 | +if DEBUG: |
| 30 | + logging = logutil.get_logger("discord") |
| 31 | + |
| 32 | +logger.warning("Debug mode is %s; Discord debug is %s", DEBUG, DEBUG_DISCORD) |
| 33 | + |
| 34 | +# Instantiate environment variables |
| 35 | +DEV_GUILD = [int(os.environ.get("DEV_GUILD"))] |
| 36 | +TOKEN = os.environ.get("TOKEN") |
| 37 | + |
| 38 | +intents = discord.Intents.default() |
| 39 | +intents.members = True # pylint: disable=assigning-non-slot # noqa |
| 40 | + |
| 41 | +# Define the client |
| 42 | +client = commands.Bot(command_prefix=bot_prefix, |
| 43 | + case_insensitive=True, |
| 44 | + intents=intents, |
| 45 | + help_command=None, |
| 46 | + ) |
| 47 | + |
| 48 | +# Define the slash command handler |
| 49 | +slash = SlashCommand(client, |
| 50 | + sync_commands=True, |
| 51 | + sync_on_cog_reload=True,) |
| 52 | + |
| 53 | + |
| 54 | +# BEGIN on_ready |
| 55 | +async def on_ready(): |
| 56 | + "Called when bot is ready to receive interactions" |
| 57 | + logger.info( |
| 58 | + "Logged in as %s#%s", |
| 59 | + client.user.name, |
| 60 | + client.user.discriminator |
| 61 | + ) |
| 62 | +# END on_ready |
| 63 | + |
| 64 | +# BEGIN command_help |
| 65 | +async def command_help(ctx): |
| 66 | + "Help command" |
| 67 | + logger.debug("%s - initiated help command", |
| 68 | + ctx.message.author) |
| 69 | + |
| 70 | + try: |
| 71 | + _created_at = ctx.message.created_at |
| 72 | + except AttributeError: |
| 73 | + _created_at = ctx.created_at |
| 74 | + |
| 75 | + help_embed = discord.Embed( |
| 76 | + title="Help", |
| 77 | + description="Fill me in!", |
| 78 | + timestamp=_created_at |
| 79 | + ) |
| 80 | + |
| 81 | + help_embed.set_thumbnail(url=client.user.avatar_url) |
| 82 | + help_embed.set_footer(text="Boilerplate Bot") |
| 83 | + |
| 84 | + await ctx.send(embed=help_embed) |
| 85 | +# END command_help |
| 86 | + |
| 87 | +# BEGIN on_command_error |
| 88 | +async def on_command_error(ctx: commands.Context, error): |
| 89 | + "Gets called when a command fails" |
| 90 | + if isinstance(error, commands.CommandOnCooldown): |
| 91 | + # handle cooldown |
| 92 | + logger.debug( |
| 93 | + "%s - initiated a command on cooldown [!]" |
| 94 | + ) |
| 95 | + await ctx.send( |
| 96 | + f"This command is on cooldown. Try again after `{round(error.retry_after)}` seconds.", |
| 97 | + delete_after=5 |
| 98 | + ) |
| 99 | + else: |
| 100 | + # handle anything else |
| 101 | + logger.warning( |
| 102 | + "A discord.py command error occured:\n%s", |
| 103 | + error |
| 104 | + ) |
| 105 | + await ctx.send( |
| 106 | + f"A discord.py command error occured:\n{error}", |
| 107 | + delete_after=10 |
| 108 | + ) |
| 109 | +# END on_command_error |
| 110 | + |
| 111 | +# BEGIN cogs_dynamic_loader |
| 112 | + |
| 113 | +# Fill this array with Python files in /cogs |
| 114 | +# This omits __init__.py, template.py, and excludes files without a py file extension |
| 115 | +command_modules = [ |
| 116 | + module[:-3] |
| 117 | + for module in os.listdir(f"{os.path.dirname(__file__)}/cogs") |
| 118 | + if module not in ("__init__.py", "template.py") and module[-3:] == ".py" |
| 119 | +] |
| 120 | + |
| 121 | +if command_modules or command_modules == []: |
| 122 | + logger.info( |
| 123 | + "Importing %s cogs: %s", |
| 124 | + len(command_modules), |
| 125 | + ', '.join(command_modules) |
| 126 | + ) |
| 127 | +else: |
| 128 | + logger.warning("Could not import any cogs!") |
| 129 | + |
| 130 | +for module in command_modules: |
| 131 | + try: |
| 132 | + client.load_extension("cogs." + module) |
| 133 | + except NoEntryPointError: |
| 134 | + logger.error( |
| 135 | + "Could not import cog %s: The cog has no setup function - NoEntryPointError", |
| 136 | + module |
| 137 | + ) |
| 138 | + logger.debug(str(sys.exc_info()[2])) |
| 139 | + except ExtensionFailed: |
| 140 | + logger.error( |
| 141 | + "Could not import cog %s: The cog failed to execute", |
| 142 | + module |
| 143 | + ) |
| 144 | + logger.debug(str(sys.exc_info()[2])) |
| 145 | + except Exception as e: # pylint: disable=broad-except |
| 146 | + logger.error( |
| 147 | + "Could not import cog %s:\n%s", |
| 148 | + module, |
| 149 | + e |
| 150 | + ) |
| 151 | + |
| 152 | +logger.info("Cog initialization complete") |
| 153 | +logger.debug( |
| 154 | + "Cogs incoming:\n%s\n", |
| 155 | + ',\n'.join(command_modules) |
| 156 | +) |
| 157 | +# END cogs_dynamic_loader |
| 158 | + |
| 159 | +client.run(TOKEN) |
0 commit comments