-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjoshgone.py
More file actions
87 lines (73 loc) · 2.45 KB
/
joshgone.py
File metadata and controls
87 lines (73 loc) · 2.45 KB
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
import os
import asyncio
import inspect
import discord
from discord.ext import commands
# These extensions are loaded automatically on startup
LOAD_ON_STARTUP = (
"admin", "censor", "chant", "music", "database", "thicc", "gee", "remind",
"split", "get", "relay", "solver", "sort", "info", "exec", "execbf",
)
# We need intents to resolve a name to a Member object
intents = discord.Intents.default()
intents.members = True
if hasattr(intents, "message_content"):
intents.message_content = True
# Our prefix is % or @joshgone
command_prefix = commands.when_mentioned_or("%")
# Use v1.x help command
try:
help_command = commands.DefaultHelpCommand(
show_parameter_descriptions=False
)
except TypeError:
help_command = None
# Wrap a non-awaitable value with an awaitable
def _wrap_async(value):
if inspect.isawaitable(value):
return value
future = asyncio.Future() # This is awaitable
future.set_result(value)
return future
# This function exists so that bot is garbage collected after the function
# ends.
async def _run(token, **bot_kwargs):
bot = commands.Bot(**bot_kwargs)
# Helper for improving compatibility between discord.py v1.x and v2.x
bot.wrap_async = _wrap_async
# Get list of extensions to load
extensions = list(LOAD_ON_STARTUP)
if int(os.environ.get("JOSHGONE_REPL", "0")):
extensions.append("repl")
# Load extensions
for module in extensions:
await bot.wrap_async(bot.load_extension(f"extensions.{module}"))
print(f"Loaded {module}")
print(f"All extensions loaded: [{', '.join(extensions)}]")
try:
await bot.start(token)
finally:
# Force the GC to run before closing the loop so objects that use
# loop.call_soon in their .__del__ methods can be garbage collected
# without giving an annoying `Exception ignored in <something>
# RuntimeError: Event loop is closed`.
del bot
import gc
gc.collect()
def run(token, **bot_kwargs):
"""Runs JoshGone with the provided token and bot options"""
# The actual bot is run and deleted inside the _run function.
try:
asyncio.run(_run(token, **bot_kwargs))
except KeyboardInterrupt:
pass
def main():
"""Entry point to run JoshGone"""
run(
os.environ["JOSHGONE_TOKEN"],
command_prefix=command_prefix,
intents=intents,
help_command=help_command,
)
if __name__ == "__main__":
main()