Files
cogs
.gitignore
main.py
requirements.txt
storage.py
Code
11 663
main.py
import logging
import os
from typing import Optional
import discord
from discord.ext import commands
from dotenv import load_dotenv
from storage import Storage
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
logger = logging.getLogger("all_in_one_bot")
def get_prefix(_: commands.Bot, __: discord.Message) -> str:
return commands.when_mentioned_or("!")(_, __)
class AllInOneBot(commands.Bot):
def __init__(self) -> None:
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
intents.reactions = True
super().__init__(
command_prefix=get_prefix,
intents=intents,
case_insensitive=True,
help_command=None,
)
self.storage = Storage()
async def setup_hook(self) -> None:
extensions = (
"cogs.moderation",
"cogs.autoroles",
"cogs.welcome",
"cogs.reaction_roles",
"cogs.polls",
"cogs.utility",
)
for extension in extensions:
await self.load_extension(extension)
test_guild_id = os.getenv("TEST_GUILD_ID")
if test_guild_id:
guild = discord.Object(id=int(test_guild_id))
self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)
logger.info("Synced application commands to test guild %s", test_guild_id)
else:
await self.tree.sync()
logger.info("Synced global application commands")
bot = AllInOneBot()
@bot.event
async def on_ready() -> None:
if bot.user:
logger.info("Logged in as %s (%s)", bot.user, bot.user.id)
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError) -> None:
error = getattr(error, "original", error)
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have permission to use that command.", delete_after=8)
return
if isinstance(error, commands.BotMissingPermissions):
await ctx.send("I am missing the permissions needed for that action.", delete_after=8)
return
if isinstance(error, commands.NoPrivateMessage):
await ctx.send("That command can only be used in a server.", delete_after=8)
return
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f"Missing argument: `{error.param.name}`.", delete_after=8)
return
if isinstance(error, commands.BadArgument):
await ctx.send("One or more arguments were invalid.", delete_after=8)
return
logger.exception("Unhandled command error", exc_info=error)
await ctx.send("Something went wrong while running that command.", delete_after=8)
@bot.tree.error
async def on_app_command_error(
interaction: discord.Interaction,
error: discord.app_commands.AppCommandError,
) -> None:
error = getattr(error, "original", error)
if isinstance(error, discord.app_commands.MissingPermissions):
message = "You do not have permission to use that command."
elif isinstance(error, discord.app_commands.BotMissingPermissions):
message = "I am missing the permissions needed for that action."
elif isinstance(error, discord.app_commands.NoPrivateMessage):
message = "That command can only be used in a server."
else:
logger.exception("Unhandled application command error", exc_info=error)
message = "Something went wrong while running that command."
if interaction.response.is_done():
await interaction.followup.send(message, ephemeral=True)
else:
await interaction.response.send_message(message, ephemeral=True)
token: Optional[str] = os.getenv("DISCORD_TOKEN")
if not token:
raise RuntimeError("DISCORD_TOKEN is missing. Copy .env.example to .env and add your token.")
bot.run(token)