-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (65 loc) · 2.39 KB
/
Copy pathmain.py
File metadata and controls
81 lines (65 loc) · 2.39 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
import asyncio
import logging
from typing import cast
import discord
from discord import app_commands
from discord.ext import commands
import config
from config import COGS, COMMAND_PREFIX, DISCORD_TOKEN
from utils import errors
from utils.context import guild_only
from utils.help import build as build_help
from utils.startup import serve
config.configure_logging()
config.log_runtime()
config.validate()
log = logging.getLogger("loopify")
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
bot = commands.Bot(
command_prefix=COMMAND_PREFIX,
intents=intents,
help_command=None, # replaced by the generated one below
case_insensitive=True,
# Every command acts on a server's voice and queue, so `/` commands are not
# offered in DMs at all. guild_only below still covers the `!` ones.
allowed_contexts=app_commands.AppCommandContext(guild=True),
)
bot.add_check(guild_only)
@bot.event
async def on_ready() -> None:
log.info("Logged in as %s (ID: %s) — serving %d guild(s)",
bot.user, bot.user.id if bot.user else "?", len(bot.guilds))
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.listening,
name="/play",
)
)
@bot.event
async def on_command_error(ctx: commands.Context[commands.Bot],
error: commands.CommandError) -> None:
# All of it lives in utils.errors so it can be tested without a gateway.
await errors.handle(ctx, error)
@bot.hybrid_command(name="help")
async def help_command(ctx: commands.Context[commands.Bot]) -> None:
"""Show this message."""
await ctx.send(embed=build_help(bot, COMMAND_PREFIX))
async def main() -> None:
async with bot:
for cog in COGS:
try:
await bot.load_extension(cog)
log.info("Loaded cog: %s", cog)
except Exception:
log.exception("Failed to load cog: %s", cog)
# Not bot.start(): the login needs retrying, and voice has to be
# released within a bound before close() waits it out. See utils.startup.
# config.validate() has already exited if the token is missing.
await serve(bot, cast(str, DISCORD_TOKEN))
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
log.info("Shutting down (KeyboardInterrupt).")