-
Notifications
You must be signed in to change notification settings - Fork 19
/
apollo.py
157 lines (135 loc) · 4.52 KB
/
apollo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
import asyncio
import logging
import discord
from discord import Intents
from discord.ext import commands
from discord.ext.commands import Bot, Context, check, errors, when_mentioned_or
from config import CONFIG
from utils.custom_help import SimplePrettyHelp
from utils.utils import done_react, is_compsoc_exec_in_guild, wait_react
DESCRIPTION = """
Apollo is the Discord bot for the University of Warwick Computing Society, designed to augment the server with a number of utilities and website services.
Apollo is open source and available at: https://github.com/UWCS/apollo. Pull requests are welcome!
"""
# The command extensions to be loaded by the bot
EXTENSIONS = [
"cogs.commands.announce",
"cogs.commands.birthday",
"cogs.commands.counting",
"cogs.commands.chatgpt",
"cogs.commands.dalle",
"cogs.commands.date",
"cogs.commands.eightball",
"cogs.commands.event_sync",
"cogs.commands.flip",
"cogs.commands.karma_admin",
"cogs.commands.karma_blacklist",
"cogs.commands.karma",
"cogs.commands.lcalc",
"cogs.commands.misc",
"cogs.commands.onmessage",
"cogs.commands.openaiadmin",
"cogs.commands.quotes",
"cogs.commands.rolemenu",
"cogs.commands.reminders",
"cogs.commands.roll",
"cogs.commands.run",
"cogs.commands.roomsearch",
"cogs.commands.say",
"cogs.commands.summarise",
"cogs.commands.system",
"cogs.commands.tex",
"cogs.commands.vote",
"cogs.commands.widen",
"cogs.commands.xkcd",
"cogs.channel_checker",
"cogs.database",
"cogs.irc",
"cogs.parallelism",
"cogs.welcome",
]
intents = Intents.default()
intents.members = True
intents.message_content = True
bot = Bot(
command_prefix=when_mentioned_or(CONFIG.PREFIX),
description=DESCRIPTION,
intents=intents,
help_command=SimplePrettyHelp(),
)
@bot.command()
@check(is_compsoc_exec_in_guild)
async def reload_cogs(ctx: Context[Bot]):
for extension in EXTENSIONS:
await bot.reload_extension(extension)
await ctx.message.add_reaction("✅")
@bot.event
async def on_ready():
logging.info("Logged in as")
logging.info(str(bot.user))
logging.info("------")
async def main():
logging.basicConfig(
level=logging.getLevelName(CONFIG.LOG_LEVEL),
format="[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("apollo.log"),
logging.StreamHandler(),
],
)
async with bot:
for extension in EXTENSIONS:
try:
logging.info(f"Attempting to load extension {extension}")
await bot.load_extension(extension)
except Exception as e:
logging.exception("Failed to load extension {extension}", exc_info=e)
await bot.start(CONFIG.DISCORD_TOKEN)
@bot.command()
@commands.guild_only()
@check(is_compsoc_exec_in_guild)
@done_react
@wait_react
async def sync(ctx: Context[Bot]) -> None:
"""
Syncs slash commands to server
"""
synced = await ctx.bot.tree.sync()
await ctx.reply(f"Synced {len(synced)} commands globally to the current guild.")
@bot.event
async def on_command_error(ctx: Context[Bot], error: Exception):
# await ctx.message.add_reaction("🚫")
message = ""
reraise = None
# Custom discord parsing error messages
if isinstance(error, errors.CommandNotFound):
pass
elif isinstance(error, errors.NoPrivateMessage):
message = "Cannot run this command in DMs"
elif isinstance(error, errors.ExpectedClosingQuoteError):
message = f"Mismatching quotes, {str(error)}"
elif isinstance(error, errors.MissingRequiredArgument):
assert ctx.command
message = f"Argument {str(error.param.name)} is missing\nUsage: `{ctx.prefix}{ctx.command.name} {ctx.command.signature}`"
elif isinstance(error, discord.Forbidden):
message = f"Bot does not have permissions to do this. {str(error.text)}"
reraise = error
elif isinstance(error, errors.CheckFailure):
pass
elif hasattr(error, "original"):
await on_command_error(ctx, error.original) # type: ignore not sure what the deal is here
return
elif isinstance(error, errors.CommandError):
message = str(error)
else:
message = f"{error}"
reraise = error
if reraise:
logging.error(reraise, exc_info=True)
if message:
await ctx.reply(f"**Error:** `{message}`")
if reraise:
raise reraise
if __name__ == "__main__":
asyncio.run(main())