forked from whoneedsabudget/fixed-that-for-you
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (98 loc) · 2.88 KB
/
main.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
"""
Main script to run
This script initializes extensions and starts the bot
"""
import os
import sys
from interactions import (
Activity,
ActivityType,
BrandColors,
Button,
ButtonStyle,
Client,
component_callback,
ComponentContext,
Embed,
errors,
Intents,
listen,
MISSING
)
from interactions.api.events import CommandError
from config import config
from src import logutil
logger = logutil.init_logger(__name__)
logger.debug(
"Debug mode is %s; This is not a warning, \
just an indicator. You may safely ignore",
config.DEBUG,
)
if not os.environ.get("TOKEN"):
logger.critical("TOKEN variable not set. Cannot continue")
sys.exit(1)
client = Client(
token=os.environ.get("TOKEN"),
activity=Activity(
name="with social media links", type=ActivityType.PLAYING
),
intents=Intents.DEFAULT | Intents.GUILDS | Intents.GUILD_MESSAGES | Intents.MESSAGE_CONTENT,
debug_scope=MISSING,
send_command_tracebacks=False,
logger=logger
)
# Enable hot-reload and traceback printing if debug is enabled
if config.DEBUG:
client.send_command_tracebacks=True
client.load_extension("interactions.ext.jurigged")
@listen()
async def on_startup():
"""Called when the bot starts"""
logger.info(f"Logged in as {client.user}")
@component_callback("close_msg")
async def close_msg(ctx: ComponentContext):
"""Close the message"""
message = await ctx.edit_origin()
await ctx.delete(message)
@listen(CommandError, disable_default_listeners=False)
async def on_command_error(event: CommandError):
if isinstance(event.error, errors.CommandCheckFailure):
if event.error.check.__name__ == 'not_a_bot':
await event.ctx.send(
embeds=Embed(
description="This command can't be run against bot messages!",
color=BrandColors.YELLOW,
),
components=Button(
label="Close",
custom_id="close_msg",
style=ButtonStyle.GREY
),
ephemeral=True
)
if not event.ctx.responded:
await event.ctx.send(
embeds=Embed(
description="Something went wrong! Please try again later.",
color=BrandColors.RED,
),
components=Button(
label="Close",
custom_id="close_msg",
style=ButtonStyle.GREY
),
ephemeral=True
)
# get all python files in "extensions" folder
extensions = [
f"extensions.{f[:-3]}"
for f in os.listdir("extensions")
if f.endswith(".py") and not f.startswith("_")
]
for extension in extensions:
try:
client.load_extension(extension)
logger.info(f"Loaded extension {extension}")
except errors.ExtensionLoadException as e:
logger.exception(f"Failed to load extension {extension}.", exc_info=e)
client.start()