Skip to content

Commit 5be41d3

Browse files
author
Krypton
committed
Template v4.1
* Added the `hackban` command * Separated slash commands and normal commands so that you remove one of them more easily * Moved normal commands in [`cogs/normal`](cogs/normal) * Moved slash commands in [`cogs/slash`](cogs/slash)
1 parent 86f3e71 commit 5be41d3

18 files changed

+1336
-852
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ I would've been happy if there were any template existing. However, there wasn't
1717
decided to create my own template to let <b>you</b> guys create your discord bot easily.
1818

1919
Please note that this template is not supposed to be the best template, but a good template to start learning how
20-
discord.py works and to make your own bot easily. After the version 4.0 the template is using disnake, as discord.py has stoppped development.
20+
discord.py works and to make your own bot easily. After the version 4.0 the template is using disnake, as discord.py has
21+
stoppped development.
2122

2223
If you plan to use this template to make your own template or bot, you **have to**:
2324

TODO.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
# TODO List
22

3-
* [ ] Add a *"hackban"* command (ban a user by ID from a guild without him being in it)
4-
* [ ] Add a basic slur word filter
5-
* [ ] Add a code of conduct for the repository
3+
## Version 4.2 (Goal: Middle January 2022)
4+
5+
* [ ] Add a timeout command.
6+
7+
## Version 4.3 (Goal: End February 2022)
8+
69
* [ ] Add a lock command that will let administrators temporary lock a channel, useful in case of a raid
710
* [ ] Add an archive command that lets administrators archive a text channel in a text file
11+
12+
## No milestone
13+
14+
* [ ] Add a code of conduct for the repository
15+
* [ ] Add a basic slur word filter
816
* [ ] Add an issue and pull request template for the repository
17+
18+
## Done
19+
20+
* [X] Add a *"hackban"* command (ban a user by ID from a guild without the user being in it)
921
* [X] Be able to save data to JSON (blacklist, owners, etc.)
1022
* [X] Move config to JSON

UPDATES.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
Here is the list of all the updates that I made on this template.
44

5+
### Version 4.1 (09 January 2022)
6+
7+
* Added the `hackban` command
8+
* Separated slash commands and normal commands so that you remove one of them more easily
9+
* Moved normal commands in [`cogs/normal`](cogs/normal)
10+
* Moved slash commands in [`cogs/slash`](cogs/slash)
11+
512
### Version 4.0.1
613

714
* Fixed some *weird* code

bot.py

+55-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
This is a template to create your own discord bot in python.
55
6-
Version: 4.0.1
6+
Version: 4.1
77
"""
88

99
import json
@@ -16,6 +16,7 @@
1616
from disnake import ApplicationCommandInteraction
1717
from disnake.ext import tasks, commands
1818
from disnake.ext.commands import Bot
19+
from disnake.ext.commands import Context
1920

2021
import exceptions
2122

@@ -60,9 +61,11 @@
6061
bot = 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.
8287
bot.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

Comments
 (0)