Skip to content

Commit

Permalink
Template v4.1
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
Krypton committed Jan 9, 2022
1 parent 86f3e71 commit 5be41d3
Show file tree
Hide file tree
Showing 18 changed files with 1,336 additions and 852 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ I would've been happy if there were any template existing. However, there wasn't
decided to create my own template to let <b>you</b> guys create your discord bot easily.

Please note that this template is not supposed to be the best template, but a good template to start learning how
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.
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.

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

Expand Down
18 changes: 15 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# TODO List

* [ ] Add a *"hackban"* command (ban a user by ID from a guild without him being in it)
* [ ] Add a basic slur word filter
* [ ] Add a code of conduct for the repository
## Version 4.2 (Goal: Middle January 2022)

* [ ] Add a timeout command.

## Version 4.3 (Goal: End February 2022)

* [ ] Add a lock command that will let administrators temporary lock a channel, useful in case of a raid
* [ ] Add an archive command that lets administrators archive a text channel in a text file

## No milestone

* [ ] Add a code of conduct for the repository
* [ ] Add a basic slur word filter
* [ ] Add an issue and pull request template for the repository

## Done

* [X] Add a *"hackban"* command (ban a user by ID from a guild without the user being in it)
* [X] Be able to save data to JSON (blacklist, owners, etc.)
* [X] Move config to JSON
7 changes: 7 additions & 0 deletions UPDATES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

### Version 4.1 (09 January 2022)

* 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)

### Version 4.0.1

* Fixed some *weird* code
Expand Down
78 changes: 55 additions & 23 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Description:
This is a template to create your own discord bot in python.
Version: 4.0.1
Version: 4.1
"""

import json
Expand All @@ -16,6 +16,7 @@
from disnake import ApplicationCommandInteraction
from disnake.ext import tasks, commands
from disnake.ext.commands import Bot
from disnake.ext.commands import Context

import exceptions

Expand Down Expand Up @@ -60,9 +61,11 @@
bot = Bot(command_prefix=config["prefix"], intents=intents)


# The code in this even is executed when the bot is ready
@bot.event
async def on_ready():
async def on_ready() -> None:
"""
The code in this even is executed when the bot is ready
"""
print(f"Logged in as {bot.user.name}")
print(f"disnake API version: {disnake.__version__}")
print(f"Python version: {platform.python_version()}")
Expand All @@ -71,47 +74,69 @@ async def on_ready():
status_task.start()


# Setup the game status task of the bot
@tasks.loop(minutes=1.0)
async def status_task():
async def status_task() -> None:
"""
Setup the game status task of the bot
"""
statuses = ["with you!", "with Krypton!", "with humans!"]
await bot.change_presence(activity=disnake.Game(random.choice(statuses)))


# Removes the default help command of discord.py to be able to create our custom help command.
bot.remove_command("help")

if __name__ == "__main__":
for file in os.listdir("./cogs"):

def load_commands(command_type: str) -> None:
for file in os.listdir(f"./cogs/{command_type}"):
if file.endswith(".py"):
extension = file[:-3]
try:
bot.load_extension(f"cogs.{extension}")
bot.load_extension(f"cogs.{command_type}.{extension}")
print(f"Loaded extension '{extension}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
print(f"Failed to load extension {extension}\n{exception}")


# The code in this event is executed every time someone sends a message, with or without the prefix
if __name__ == "__main__":
"""
This will automatically load slash commands and normal commands located in their respective folder.
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.
"""
load_commands("slash")
load_commands("normal")


@bot.event
async def on_message(message: disnake.Message):
# Ignores if a command is being executed by a bot or by the bot itself
async def on_message(message: disnake.Message) -> None:
"""
The code in this event is executed every time someone sends a message, with or without the prefix
:param message: The message that was sent.
"""
if message.author == bot.user or message.author.bot:
return
await bot.process_commands(message)


# The code in this event is executed every time a slash command has been *successfully* executed
@bot.event
async def on_slash_command(interaction: ApplicationCommandInteraction):
async def on_slash_command(interaction: ApplicationCommandInteraction) -> None:
"""
The code in this event is executed every time a slash command has been *successfully* executed
:param interaction: The slash command that has been executed.
"""
print(
f"Executed {interaction.data.name} command in {interaction.guild.name} (ID: {interaction.guild.id}) by {interaction.author} (ID: {interaction.author.id})")


# The code in this event is executed every time a valid slash command catches an error
@bot.event
async def on_slash_command_error(interaction: ApplicationCommandInteraction, error: Exception):
async def on_slash_command_error(interaction: ApplicationCommandInteraction, error: Exception) -> None:
"""
The code in this event is executed every time a valid slash command catches an error
:param interaction: The slash command that failed executing.
:param error: The error that has been faced.
"""
if isinstance(error, exceptions.UserBlacklisted):
"""
The code here will only execute if the error is an instance of 'UserBlacklisted', which can occur when using
Expand All @@ -138,19 +163,26 @@ async def on_slash_command_error(interaction: ApplicationCommandInteraction, err
raise error


# The code in this event is executed every time a normal command has been *successfully* executed
@bot.event
async def on_command_completion(ctx):
fullCommandName = ctx.command.qualified_name
split = fullCommandName.split(" ")
executedCommand = str(split[0])
async def on_command_completion(context: Context) -> None:
"""
The code in this event is executed every time a normal command has been *successfully* executed
:param context: The context of the command that has been executed.
"""
full_command_name = context.command.qualified_name
split = full_command_name.split(" ")
executed_command = str(split[0])
print(
f"Executed {executedCommand} command in {ctx.guild.name} (ID: {ctx.message.guild.id}) by {ctx.message.author} (ID: {ctx.message.author.id})")
f"Executed {executed_command} command in {context.guild.name} (ID: {context.message.guild.id}) by {context.message.author} (ID: {context.message.author.id})")


# The code in this event is executed every time a normal valid command catches an error
@bot.event
async def on_command_error(context, error):
async def on_command_error(context: Context, error) -> None:
"""
The code in this event is executed every time a normal valid command catches an error
:param context: The normal command that failed executing.
:param error: The error that has been faced.
"""
if isinstance(error, commands.CommandOnCooldown):
minutes, seconds = divmod(error.retry_after, 60)
hours, minutes = divmod(minutes, 60)
Expand Down
Loading

0 comments on commit 5be41d3

Please sign in to comment.