33Description:
44This is a template to create your own discord bot in python.
55
6- Version: 4.0. 1
6+ Version: 4.1
77"""
88
99import json
1616from disnake import ApplicationCommandInteraction
1717from disnake .ext import tasks , commands
1818from disnake .ext .commands import Bot
19+ from disnake .ext .commands import Context
1920
2021import exceptions
2122
6061bot = Bot (command_prefix = config ["prefix" ], intents = intents )
6162
6263
63- # The code in this even is executed when the bot is ready
6464@bot .event
65- async def on_ready ():
65+ async def on_ready () -> None :
66+ """
67+ The code in this even is executed when the bot is ready
68+ """
6669 print (f"Logged in as { bot .user .name } " )
6770 print (f"disnake API version: { disnake .__version__ } " )
6871 print (f"Python version: { platform .python_version ()} " )
@@ -71,47 +74,69 @@ async def on_ready():
7174 status_task .start ()
7275
7376
74- # Setup the game status task of the bot
7577@tasks .loop (minutes = 1.0 )
76- async def status_task ():
78+ async def status_task () -> None :
79+ """
80+ Setup the game status task of the bot
81+ """
7782 statuses = ["with you!" , "with Krypton!" , "with humans!" ]
7883 await bot .change_presence (activity = disnake .Game (random .choice (statuses )))
7984
8085
8186# Removes the default help command of discord.py to be able to create our custom help command.
8287bot .remove_command ("help" )
8388
84- if __name__ == "__main__" :
85- for file in os .listdir ("./cogs" ):
89+
90+ def load_commands (command_type : str ) -> None :
91+ for file in os .listdir (f"./cogs/{ command_type } " ):
8692 if file .endswith (".py" ):
8793 extension = file [:- 3 ]
8894 try :
89- bot .load_extension (f"cogs.{ extension } " )
95+ bot .load_extension (f"cogs.{ command_type } . { extension } " )
9096 print (f"Loaded extension '{ extension } '" )
9197 except Exception as e :
9298 exception = f"{ type (e ).__name__ } : { e } "
9399 print (f"Failed to load extension { extension } \n { exception } " )
94100
95101
96- # The code in this event is executed every time someone sends a message, with or without the prefix
102+ if __name__ == "__main__" :
103+ """
104+ This will automatically load slash commands and normal commands located in their respective folder.
105+
106+ If you want to remove slash commands, which is not recommended due to the Message Intent being a privileged intent, you can remove the loading of slash commands below.
107+ """
108+ load_commands ("slash" )
109+ load_commands ("normal" )
110+
111+
97112@bot .event
98- async def on_message (message : disnake .Message ):
99- # Ignores if a command is being executed by a bot or by the bot itself
113+ async def on_message (message : disnake .Message ) -> None :
114+ """
115+ The code in this event is executed every time someone sends a message, with or without the prefix
116+ :param message: The message that was sent.
117+ """
100118 if message .author == bot .user or message .author .bot :
101119 return
102120 await bot .process_commands (message )
103121
104122
105- # The code in this event is executed every time a slash command has been *successfully* executed
106123@bot .event
107- async def on_slash_command (interaction : ApplicationCommandInteraction ):
124+ async def on_slash_command (interaction : ApplicationCommandInteraction ) -> None :
125+ """
126+ The code in this event is executed every time a slash command has been *successfully* executed
127+ :param interaction: The slash command that has been executed.
128+ """
108129 print (
109130 f"Executed { interaction .data .name } command in { interaction .guild .name } (ID: { interaction .guild .id } ) by { interaction .author } (ID: { interaction .author .id } )" )
110131
111132
112- # The code in this event is executed every time a valid slash command catches an error
113133@bot .event
114- async def on_slash_command_error (interaction : ApplicationCommandInteraction , error : Exception ):
134+ async def on_slash_command_error (interaction : ApplicationCommandInteraction , error : Exception ) -> None :
135+ """
136+ The code in this event is executed every time a valid slash command catches an error
137+ :param interaction: The slash command that failed executing.
138+ :param error: The error that has been faced.
139+ """
115140 if isinstance (error , exceptions .UserBlacklisted ):
116141 """
117142 The code here will only execute if the error is an instance of 'UserBlacklisted', which can occur when using
@@ -138,19 +163,26 @@ async def on_slash_command_error(interaction: ApplicationCommandInteraction, err
138163 raise error
139164
140165
141- # The code in this event is executed every time a normal command has been *successfully* executed
142166@bot .event
143- async def on_command_completion (ctx ):
144- fullCommandName = ctx .command .qualified_name
145- split = fullCommandName .split (" " )
146- executedCommand = str (split [0 ])
167+ async def on_command_completion (context : Context ) -> None :
168+ """
169+ The code in this event is executed every time a normal command has been *successfully* executed
170+ :param context: The context of the command that has been executed.
171+ """
172+ full_command_name = context .command .qualified_name
173+ split = full_command_name .split (" " )
174+ executed_command = str (split [0 ])
147175 print (
148- f"Executed { executedCommand } command in { ctx .guild .name } (ID: { ctx .message .guild .id } ) by { ctx .message .author } (ID: { ctx .message .author .id } )" )
176+ f"Executed { executed_command } command in { context .guild .name } (ID: { context .message .guild .id } ) by { context .message .author } (ID: { context .message .author .id } )" )
149177
150178
151- # The code in this event is executed every time a normal valid command catches an error
152179@bot .event
153- async def on_command_error (context , error ):
180+ async def on_command_error (context : Context , error ) -> None :
181+ """
182+ The code in this event is executed every time a normal valid command catches an error
183+ :param context: The normal command that failed executing.
184+ :param error: The error that has been faced.
185+ """
154186 if isinstance (error , commands .CommandOnCooldown ):
155187 minutes , seconds = divmod (error .retry_after , 60 )
156188 hours , minutes = divmod (minutes , 60 )
0 commit comments