-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbot.py
More file actions
executable file
·145 lines (117 loc) · 5.23 KB
/
bot.py
File metadata and controls
executable file
·145 lines (117 loc) · 5.23 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
"""
#######################################################################################
This is the main file for our bot it contains initialization #
And call for all the cogs #
#
#######################################################################################
"""
import discord
import asyncio
import os
import platform
import sys
import datetime
import time
from aiohttp import ClientSession
from discord.ext import commands, tasks
from core.utils import getchannel, getuser, getguild
from keep_alive import keep_alive
from core.utils import send_embed, loads_to_object
from core import errors
from json import loads
if not os.path.isfile("config.json"):
sys.exit("'config.json' not found! Please add it and try again.")
else:
config = loads_to_object("config.json")
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
intents.messages = True
intents.emojis = True
bot = commands.Bot(command_prefix=config.BOT_PREFIX, intents=intents)
bot.session = ClientSession()
# The code in this even is executed when the bot is ready
@bot.event
async def on_ready():
bot.loop.create_task(status_task())
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("-------------------")
# Setup the game status task of the bot
async def status_task():
while True:
await bot.change_presence(activity=discord.Game("with you!"))
await asyncio.sleep(60)
await bot.change_presence(activity=discord.Game(f"{config.BOT_PREFIX} help"))
await asyncio.sleep(60)
await bot.change_presence(activity=discord.Game("with humans!"))
await asyncio.sleep(60)
# this is very important we will not use the default help command .
# Removes the default help command of discord.py to be able to create our custom help command.
bot.remove_command("help")
# This will load the cogs set in startup cogs !!
# with this we can specify the commands that we need and the one we don't !
if __name__ == "__main__":
for extension in config.STARTUP_COGS:
try:
bot.load_extension(extension)
extension = extension.replace("cogs.", "")
print(f"Loaded extension '{extension}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
extension = extension.replace("cogs.", "")
print(f"Failed to load extension {extension}\n{exception}")
# 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])
guild_name = "Private DM "
if ctx.guild:
guild_name = ctx.guild.name
print(
f"Executed {executedCommand} command in {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(context, error):
# here we log the error
print("Error type:", type(error))
# then handle it !
if isinstance(error, commands.CommandOnCooldown):
await send_embed(context, "Error!", "This command is on a %.2fs cooldown" % error.retry_after)
elif isinstance(error, commands.errors.PrivateMessageOnly):
await send_embed(context, "DMs only", "This service is only available in direct messages", discord.Colour.gold())
elif isinstance(error, commands.errors.MissingRequiredArgument):
await send_embed(context, "Missing Arguments", "you need to specify the UUID", discord.Colour.gold())
elif isinstance(error, discord.Forbidden):
await send_embed(context, "Permission Denied", "I don't have permissions to post in that channel", discord.Colour.gold())
elif isinstance(error, errors.AuthorizationError):
await send_embed(context, "Error!", "You don't have the permission to use this command.")
elif isinstance(error, commands.errors.CommandNotFound):
await send_embed(context, "Invalid Command", "Sorry I don't understand this command")
elif isinstance(error, commands.errors.NoPrivateMessage):
await send_embed(context, "Cannot be executed in Private Message", "Command cannot be executed in a private message")
else:
print("Uncaught error !")
print("Error type:", type(error))
print("Error message:", error)
await context.send(":x: Error")
raise error
# get the discord token
if "DISCORD_TOKEN" in os.environ:
TOKEN = os.getenv("DISCORD_TOKEN")
else:
configJson = loads(open("config.json", "r").read())
TOKEN = configJson["DISCORD_TOKEN"]
if TOKEN == None:
raise "Server token not found"
# run this function to launch the background job
# this function is very util when you run your bot on environment
# that kills your process
keep_alive()
# Run the bot with the token
bot.run(TOKEN)