-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
66 lines (57 loc) · 2.39 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
import discord
import os
import platform
import sys
from discord.ext.commands import Bot
try:
import config
except ImportError:
sys.exit("'config.py' not found! Please add it and try again.")
intents = discord.Intents.all()
intents.members = True
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():
for extension in config.STARTUP_COGS:
print(f"Loading extension '{extension}'")
await bot.load_extension(extension)
await bot.change_presence(activity=discord.Game("with electrons"))
print(f"Logged in as: {bot.user.name}")
print(f"Discord.py API version: {discord.__version__}")
print(f"Python version: {platform.python_version()}")
print(f"Running on: {platform.system()} {platform.release()} ({os.name})")
print("-------------------")
# Removes the default help command of discord.py to be able to create our custom help command.
bot.remove_command("help")
# 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
if not message.content.startswith("!"):
return
await message.add_reaction(config.HOURGLASS_EMOJI)
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: discord.ext.commands.Context):
fullCommandName = ctx.command.qualified_name
split = fullCommandName.split(" ")
executedCommand = str(split[0])
await ctx.message.remove_reaction(config.HOURGLASS_EMOJI, member=ctx.me)
await ctx.message.add_reaction(config.GOOD_EMOJI)
print(f"Executed {executedCommand} command in {ctx.guild.name} 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(ctx: discord.ext.commands.Context, error):
print(error)
await ctx.message.remove_reaction(config.HOURGLASS_EMOJI, member=ctx.me)
await ctx.message.add_reaction(config.FAILED_EMOJI)
if isinstance(error, discord.ext.commands.errors.CommandNotFound):
await ctx.message.reply("Command not found!")
else:
await ctx.message.reply(str(error.original))
# Run the bot with the token
bot.run(config.TOKEN)