3
3
Description:
4
4
This is a template to create your own discord bot in python.
5
5
6
- Version: 3.1.1
6
+ Version: 4.0
7
7
"""
8
8
9
9
import json
12
12
import random
13
13
import sys
14
14
15
- import discord
16
- from discord . ext import tasks
17
- from discord .ext . commands import Bot
18
- from discord_slash import SlashCommand , SlashContext
15
+ import disnake
16
+ from disnake import ApplicationCommandInteraction
17
+ from disnake .ext import tasks , commands
18
+ from disnake . ext . commands import Bot
19
19
20
20
import exceptions
21
21
28
28
"""
29
29
Setup bot intents (events restrictions)
30
30
For more information about intents, please go to the following websites:
31
- https://discordpy.readthedocs.io /en/latest/intents.html
32
- https://discordpy.readthedocs.io /en/latest/intents.html#privileged-intents
31
+ https://docs.disnake.dev /en/latest/intents.html
32
+ https://docs.disnake.dev /en/latest/intents.html#privileged-intents
33
33
34
34
35
35
Default Intents:
36
- intents.messages = True
37
- intents.reactions = True
38
- intents.guilds = True
39
- intents.emojis = True
40
36
intents.bans = True
41
- intents.guild_typing = False
42
- intents.typing = False
43
37
intents.dm_messages = False
44
38
intents.dm_reactions = False
45
39
intents.dm_typing = False
40
+ intents.emojis = True
46
41
intents.guild_messages = True
47
42
intents.guild_reactions = True
43
+ intents.guild_typing = False
44
+ intents.guilds = True
48
45
intents.integrations = True
49
46
intents.invites = True
47
+ intents.reactions = True
48
+ intents.typing = False
50
49
intents.voice_states = False
51
50
intents.webhooks = False
52
51
53
52
Privileged Intents (Needs to be enabled on dev page), please use them only if you need them:
54
- intents.presences = True
55
53
intents.members = True
54
+ intents.messages = True
55
+ intents.presences = True
56
56
"""
57
57
58
- intents = discord .Intents .default ()
58
+ intents = disnake .Intents .default ()
59
59
60
- bot = Bot (command_prefix = "" , intents = intents ) # The command prefix is a required argument, but will never be used
61
- slash = SlashCommand (bot , sync_commands = True )
60
+ bot = Bot (command_prefix = config ["prefix" ], intents = intents )
62
61
63
62
64
63
# The code in this even is executed when the bot is ready
65
64
@bot .event
66
65
async def on_ready ():
67
66
print (f"Logged in as { bot .user .name } " )
68
- print (f"Discord.py API version: { discord .__version__ } " )
67
+ print (f"disnake API version: { disnake .__version__ } " )
69
68
print (f"Python version: { platform .python_version ()} " )
70
69
print (f"Running on: { platform .system ()} { platform .release ()} ({ os .name } )" )
71
70
print ("-------------------" )
@@ -76,7 +75,7 @@ async def on_ready():
76
75
@tasks .loop (minutes = 1.0 )
77
76
async def status_task ():
78
77
statuses = ["with you!" , "with Krypton!" , "with humans!" ]
79
- await bot .change_presence (activity = discord .Game (random .choice (statuses )))
78
+ await bot .change_presence (activity = disnake .Game (random .choice (statuses )))
80
79
81
80
82
81
# Removes the default help command of discord.py to be able to create our custom help command.
@@ -96,35 +95,88 @@ async def status_task():
96
95
97
96
# The code in this event is executed every time someone sends a message, with or without the prefix
98
97
@bot .event
99
- async def on_message (message : discord .Message ):
98
+ async def on_message (message : disnake .Message ):
100
99
# Ignores if a command is being executed by a bot or by the bot itself
101
100
if message .author == bot .user or message .author .bot :
102
101
return
103
102
await bot .process_commands (message )
104
103
105
104
106
- # The code in this event is executed every time a command has been *successfully* executed
105
+ # The code in this event is executed every time a slash command has been *successfully* executed
107
106
@bot .event
108
- async def on_slash_command (ctx : SlashContext ):
109
- full_command_name = ctx .name
110
- split = full_command_name .split (" " )
111
- executed_command = str (split [0 ])
107
+ async def on_slash_command (interaction : ApplicationCommandInteraction ):
112
108
print (
113
- f"Executed { executed_command } command in { ctx .guild .name } (ID: { ctx .guild .id } ) by { ctx .author } (ID: { ctx .author .id } )" )
109
+ f"Executed { interaction . data . name } command in { interaction .guild .name } (ID: { interaction .guild .id } ) by { interaction .author } (ID: { interaction .author .id } )" )
114
110
115
111
116
- # The code in this event is executed every time a valid commands catches an error
112
+ # The code in this event is executed every time a valid slash command catches an error
117
113
@bot .event
118
- async def on_slash_command_error (context : SlashContext , error : Exception ):
114
+ async def on_slash_command_error (interaction : ApplicationCommandInteraction , error : Exception ):
119
115
if isinstance (error , exceptions .UserBlacklisted ):
120
116
"""
121
117
The code here will only execute if the error is an instance of 'UserBlacklisted', which can occur when using
122
118
the @checks.is_owner() check in your command, or you can raise the error by yourself.
123
119
124
120
'hidden=True' will make so that only the user who execute the command can see the message
125
121
"""
122
+ embed = disnake .Embed (
123
+ title = "Error!" ,
124
+ description = "You are blacklisted from using the bot." ,
125
+ color = 0xE02B2B
126
+ )
127
+ print ("A blacklisted user tried to execute a command." )
128
+ return await interaction .send (embed = embed , ephemeral = True )
129
+ elif isinstance (error , commands .errors .MissingPermissions ):
130
+ embed = disnake .Embed (
131
+ title = "Error!" ,
132
+ description = "You are missing the permission(s) `" + ", " .join (
133
+ error .missing_permissions ) + "` to execute this command!" ,
134
+ color = 0xE02B2B
135
+ )
126
136
print ("A blacklisted user tried to execute a command." )
127
- return await context .send ("You are blacklisted from using the bot." , hidden = True )
137
+ return await interaction .send (embed = embed , ephemeral = True )
138
+ raise error
139
+
140
+
141
+ # The code in this event is executed every time a normal command has been *successfully* executed
142
+ @bot .event
143
+ async def on_command_completion (ctx ):
144
+ fullCommandName = ctx .command .qualified_name
145
+ split = fullCommandName .split (" " )
146
+ executedCommand = str (split [0 ])
147
+ print (
148
+ f"Executed { executedCommand } command in { ctx .guild .name } (ID: { ctx .message .guild .id } ) by { ctx .message .author } (ID: { ctx .message .author .id } )" )
149
+
150
+
151
+ # The code in this event is executed every time a normal valid command catches an error
152
+ @bot .event
153
+ async def on_command_error (context , error ):
154
+ if isinstance (error , commands .CommandOnCooldown ):
155
+ minutes , seconds = divmod (error .retry_after , 60 )
156
+ hours , minutes = divmod (minutes , 60 )
157
+ hours = hours % 24
158
+ embed = disnake .Embed (
159
+ title = "Hey, please slow down!" ,
160
+ 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 '' } ." ,
161
+ color = 0xE02B2B
162
+ )
163
+ await context .send (embed = embed )
164
+ elif isinstance (error , commands .MissingPermissions ):
165
+ embed = disnake .Embed (
166
+ title = "Error!" ,
167
+ description = "You are missing the permission(s) `" + ", " .join (
168
+ error .missing_permissions ) + "` to execute this command!" ,
169
+ color = 0xE02B2B
170
+ )
171
+ await context .send (embed = embed )
172
+ elif isinstance (error , commands .MissingRequiredArgument ):
173
+ embed = disnake .Embed (
174
+ title = "Error!" ,
175
+ description = str (error ).capitalize (),
176
+ # We need to capitalize because the command arguments have no capital letter in the code.
177
+ color = 0xE02B2B
178
+ )
179
+ await context .send (embed = embed )
128
180
raise error
129
181
130
182
0 commit comments