33Description:
44This is a template to create your own discord bot in python.
55
6- Version: 3.1.1
6+ Version: 4.0
77"""
88
99import json
1212import random
1313import sys
1414
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
1919
2020import exceptions
2121
2828"""
2929Setup bot intents (events restrictions)
3030For 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
3333
3434
3535Default Intents:
36- intents.messages = True
37- intents.reactions = True
38- intents.guilds = True
39- intents.emojis = True
4036intents.bans = True
41- intents.guild_typing = False
42- intents.typing = False
4337intents.dm_messages = False
4438intents.dm_reactions = False
4539intents.dm_typing = False
40+ intents.emojis = True
4641intents.guild_messages = True
4742intents.guild_reactions = True
43+ intents.guild_typing = False
44+ intents.guilds = True
4845intents.integrations = True
4946intents.invites = True
47+ intents.reactions = True
48+ intents.typing = False
5049intents.voice_states = False
5150intents.webhooks = False
5251
5352Privileged Intents (Needs to be enabled on dev page), please use them only if you need them:
54- intents.presences = True
5553intents.members = True
54+ intents.messages = True
55+ intents.presences = True
5656"""
5757
58- intents = discord .Intents .default ()
58+ intents = disnake .Intents .default ()
5959
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 )
6261
6362
6463# The code in this even is executed when the bot is ready
6564@bot .event
6665async def on_ready ():
6766 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__ } " )
6968 print (f"Python version: { platform .python_version ()} " )
7069 print (f"Running on: { platform .system ()} { platform .release ()} ({ os .name } )" )
7170 print ("-------------------" )
@@ -76,7 +75,7 @@ async def on_ready():
7675@tasks .loop (minutes = 1.0 )
7776async def status_task ():
7877 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 )))
8079
8180
8281# 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():
9695
9796# The code in this event is executed every time someone sends a message, with or without the prefix
9897@bot .event
99- async def on_message (message : discord .Message ):
98+ async def on_message (message : disnake .Message ):
10099 # Ignores if a command is being executed by a bot or by the bot itself
101100 if message .author == bot .user or message .author .bot :
102101 return
103102 await bot .process_commands (message )
104103
105104
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
107106@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 ):
112108 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 } )" )
114110
115111
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
117113@bot .event
118- async def on_slash_command_error (context : SlashContext , error : Exception ):
114+ async def on_slash_command_error (interaction : ApplicationCommandInteraction , error : Exception ):
119115 if isinstance (error , exceptions .UserBlacklisted ):
120116 """
121117 The code here will only execute if the error is an instance of 'UserBlacklisted', which can occur when using
122118 the @checks.is_owner() check in your command, or you can raise the error by yourself.
123119
124120 'hidden=True' will make so that only the user who execute the command can see the message
125121 """
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+ )
126136 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 )
128180 raise error
129181
130182
0 commit comments