3
3
Description:
4
4
This is a template to create your own discord bot in python.
5
5
6
- Version: 4.0. 1
6
+ Version: 4.1
7
7
"""
8
8
9
9
import json
16
16
from disnake import ApplicationCommandInteraction
17
17
from disnake .ext import tasks , commands
18
18
from disnake .ext .commands import Bot
19
+ from disnake .ext .commands import Context
19
20
20
21
import exceptions
21
22
60
61
bot = Bot (command_prefix = config ["prefix" ], intents = intents )
61
62
62
63
63
- # The code in this even is executed when the bot is ready
64
64
@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
+ """
66
69
print (f"Logged in as { bot .user .name } " )
67
70
print (f"disnake API version: { disnake .__version__ } " )
68
71
print (f"Python version: { platform .python_version ()} " )
@@ -71,47 +74,69 @@ async def on_ready():
71
74
status_task .start ()
72
75
73
76
74
- # Setup the game status task of the bot
75
77
@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
+ """
77
82
statuses = ["with you!" , "with Krypton!" , "with humans!" ]
78
83
await bot .change_presence (activity = disnake .Game (random .choice (statuses )))
79
84
80
85
81
86
# Removes the default help command of discord.py to be able to create our custom help command.
82
87
bot .remove_command ("help" )
83
88
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 } " ):
86
92
if file .endswith (".py" ):
87
93
extension = file [:- 3 ]
88
94
try :
89
- bot .load_extension (f"cogs.{ extension } " )
95
+ bot .load_extension (f"cogs.{ command_type } . { extension } " )
90
96
print (f"Loaded extension '{ extension } '" )
91
97
except Exception as e :
92
98
exception = f"{ type (e ).__name__ } : { e } "
93
99
print (f"Failed to load extension { extension } \n { exception } " )
94
100
95
101
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
+
97
112
@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
+ """
100
118
if message .author == bot .user or message .author .bot :
101
119
return
102
120
await bot .process_commands (message )
103
121
104
122
105
- # The code in this event is executed every time a slash command has been *successfully* executed
106
123
@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
+ """
108
129
print (
109
130
f"Executed { interaction .data .name } command in { interaction .guild .name } (ID: { interaction .guild .id } ) by { interaction .author } (ID: { interaction .author .id } )" )
110
131
111
132
112
- # The code in this event is executed every time a valid slash command catches an error
113
133
@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
+ """
115
140
if isinstance (error , exceptions .UserBlacklisted ):
116
141
"""
117
142
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
138
163
raise error
139
164
140
165
141
- # The code in this event is executed every time a normal command has been *successfully* executed
142
166
@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 ])
147
175
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 } )" )
149
177
150
178
151
- # The code in this event is executed every time a normal valid command catches an error
152
179
@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
+ """
154
186
if isinstance (error , commands .CommandOnCooldown ):
155
187
minutes , seconds = divmod (error .retry_after , 60 )
156
188
hours , minutes = divmod (minutes , 60 )
0 commit comments