-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
105 lines (83 loc) · 3.61 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
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
import os
import platform
import random
import sys
import re
import nextcord
import yaml
from nextcord.ext import commands, tasks
from nextcord.ext.commands import Bot
if "DnDBot" not in str(os.getcwd()):
os.chdir("./DnDBot")
with open("config.yaml") as file:
config = yaml.load(file, Loader=yaml.FullLoader)
intents = nextcord.Intents.default()
bot = Bot(command_prefix=config["bot_prefix"], intents=intents)
# The code in this even is executed when the bot is ready
@bot.event
async def on_ready():
print(f"Logged in as {bot.user.name}")
print(f"nextcord.py API version: {nextcord.__version__}")
print(f"Python version: {platform.python_version()}")
print(f"Running on: {platform.system()} {platform.release()} ({os.name})")
print("-------------------")
status_task.start()
# Setup the game status task of the bot
@tasks.loop(minutes=1.0)
async def status_task():
statuses = ["D&D"]
await bot.change_presence(activity=nextcord.Game(random.choice(statuses)))
# Removes the default help command of nextcord.py to be able to create our custom help command.
bot.remove_command("help")
if __name__ == "__main__":
for file in os.listdir("./cogs"):
if file.endswith(".py"):
extension = file[:-3]
try:
bot.load_extension(f"cogs.{extension}")
print(f"Loaded extension '{extension}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
print(f"Failed to load extension {extension}\n{exception}")
# The code in this event is executed every time someone sends a message, with or without the prefix
@bot.event
async def on_message(message):
# Ignores if a command is being executed by a bot or by the bot itself
if message.author == bot.user or message.author.bot:
return
# Ignores if a command is being executed by a blacklisted user
if message.author.id in config["blacklist"]:
return
await bot.process_commands(message)
# The code in this event is executed every time a command has been *successfully* executed
@bot.event
async def on_command_completion(ctx):
fullCommandName = ctx.command.qualified_name
split = fullCommandName.split(" ")
executedCommand = str(split[0])
print(
f"Executed {executedCommand} command in {ctx.guild.name} (ID: {ctx.message.guild.id}) by {ctx.message.author} (ID: {ctx.message.author.id})")
# The code in this event is executed every time a valid commands catches an error
@bot.event
async def on_command_error(context, error):
if isinstance(error, commands.CommandOnCooldown):
minutes, seconds = divmod(error.retry_after, 60)
hours, minutes = divmod(minutes, 60)
hours = hours % 24
embed = nextcord.Embed(
title="Hey, please slow down!",
description=f"You can use this command again in {f'{round(hours)} hours' if round(hours) > 0 else ''} {f'{round(minutes)} minutes' if round(minutes) > 0 else ''} {f'{round(seconds)} seconds' if round(seconds) > 0 else ''}.",
color=config["error"]
)
await context.send(embed=embed)
elif isinstance(error, commands.MissingPermissions):
embed = nextcord.Embed(
title="Error!",
description="You are missing the permission `" + ", ".join(
error.missing_perms) + "` to execute this command!",
color=config["error"]
)
await context.send(embed=embed)
raise error
# Run the bot with the token
bot.run(config["token"])