forked from kkrypt0nn/Python-Discord-Bot-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
132 lines (105 loc) · 4.2 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
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
""""
Copyright © Krypton 2021 - https://github.com/kkrypt0nn
Description:
This is a template to create your own discord bot in python.
Version: 3.1.1
"""
import json
import os
import platform
import random
import sys
import discord
from discord.ext import tasks
from discord.ext.commands import Bot
from discord_slash import SlashCommand, SlashContext
import exceptions
if not os.path.isfile("config.json"):
sys.exit("'config.json' not found! Please add it and try again.")
else:
with open("config.json") as file:
config = json.load(file)
"""
Setup bot intents (events restrictions)
For more information about intents, please go to the following websites:
https://discordpy.readthedocs.io/en/latest/intents.html
https://discordpy.readthedocs.io/en/latest/intents.html#privileged-intents
Default Intents:
intents.messages = True
intents.reactions = True
intents.guilds = True
intents.emojis = True
intents.bans = True
intents.guild_typing = False
intents.typing = False
intents.dm_messages = False
intents.dm_reactions = False
intents.dm_typing = False
intents.guild_messages = True
intents.guild_reactions = True
intents.integrations = True
intents.invites = True
intents.voice_states = False
intents.webhooks = False
Privileged Intents (Needs to be enabled on dev page), please use them only if you need them:
intents.presences = True
intents.members = True
"""
intents = discord.Intents.default()
bot = Bot(command_prefix="", intents=intents) # The command prefix is a required argument, but will never be used
slash = SlashCommand(bot, sync_commands=True)
# 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"Discord.py API version: {discord.__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 = ["with you!", "with Krypton!", "with humans!"]
await bot.change_presence(activity=discord.Game(random.choice(statuses)))
# Removes the default help command of discord.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: discord.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
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_slash_command(ctx: SlashContext):
full_command_name = ctx.name
split = full_command_name.split(" ")
executed_command = str(split[0])
print(
f"Executed {executed_command} command in {ctx.guild.name} (ID: {ctx.guild.id}) by {ctx.author} (ID: {ctx.author.id})")
# The code in this event is executed every time a valid commands catches an error
@bot.event
async def on_slash_command_error(context: SlashContext, error: Exception):
if isinstance(error, exceptions.UserBlacklisted):
"""
The code here will only execute if the error is an instance of 'UserBlacklisted', which can occur when using
the @checks.is_owner() check in your command, or you can raise the error by yourself.
'hidden=True' will make so that only the user who execute the command can see the message
"""
print("A blacklisted user tried to execute a command.")
return await context.send("You are blacklisted from using the bot.", hidden=True)
raise error
# Run the bot with the token
bot.run(config["token"])