-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
68 lines (54 loc) · 2.03 KB
/
bot.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
import discord
from discord.ext import commands
import private.config as config
from typing import Literal, Optional
import asyncio
import os
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', help_command=None, intents=intents, application_id=config.application_id)
# Events
@bot.event
async def on_ready():
print('Bot is ready')
# Sync commands to the current guild or globally if no guilds are provided (credits: https://about.abstractumbra.dev/discord.py/2023/01/29/sync-command-example.html)
@bot.command()
@commands.guild_only()
@commands.is_owner()
async def sync(ctx: commands.Context, guilds: commands.Greedy[discord.Object], spec: Optional[Literal["~", "*", "^"]] = None) -> None:
if not guilds:
if spec == "~":
synced = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "*":
ctx.bot.tree.copy_global_to(guild=ctx.guild)
synced = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "^":
ctx.bot.tree.clear_commands(guild=ctx.guild)
await ctx.bot.tree.sync(guild=ctx.guild)
synced = []
else:
synced = await ctx.bot.tree.sync()
await ctx.send(
f"Synced {len(synced)} commands {'globally' if spec is None else 'to the current guild.'}"
)
return
ret = 0
for guild in guilds:
try:
await ctx.bot.tree.sync(guild=guild)
except discord.HTTPException:
pass
else:
ret += 1
await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.")
# Load cogs from the cogs directory on startup
async def load():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
# Run the bot with the token from the config file and load the cogs on startup
async def main():
await load()
await bot.start(config.token)
# Run the main function with the asyncio event loop to start the bot
asyncio.run(main())