Skip to content

Commit 70890d6

Browse files
committed
Template v5.1
* Added the `help` command once again * Created a group for the `warning` command, has following sub-commands: * `add` - Adds a warning to the user * `remove` - Removes a warning from the user * `list` - Lists all the warnings of the user
1 parent 251f4b4 commit 70890d6

File tree

11 files changed

+98
-20
lines changed

11 files changed

+98
-20
lines changed

UPDATES.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

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

5+
### Version 5.1 (12 September 2022)
6+
7+
* Added the `help` command once again
8+
* Created a group for the `warning` command, has following sub-commands:
9+
* `add` - Adds a warning to the user
10+
* `remove` - Removes a warning from the user
11+
* `list` - Lists all the warnings of the user
12+
513
### Version 5.0 (20 August 2022)
614

715
> ⚠️ **Moved to discord.py 2.0 as it is now officially released**

bot.py

+1-1
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: 5.0
6+
Version: 5.1
77
"""
88

99
import asyncio

cogs/fun.py

+1-1
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: 5.0
6+
Version: 5.1
77
"""
88

99
import random

cogs/general.py

+19-1
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: 5.0
6+
Version: 5.1
77
"""
88

99
import platform
@@ -22,6 +22,24 @@ class General(commands.Cog, name="general"):
2222
def __init__(self, bot):
2323
self.bot = bot
2424

25+
@commands.hybrid_command(
26+
name="help",
27+
description="List all commands the bot has loaded."
28+
)
29+
async def help(self, context: Context) -> None:
30+
prefix = self.bot.config["prefix"]
31+
embed = discord.Embed(title="Help", description="List of available commands:", color=0x9C84EF)
32+
for i in self.bot.cogs:
33+
cog = self.bot.get_cog(i.lower())
34+
commands = cog.get_commands()
35+
data = []
36+
for command in commands:
37+
description = command.description.partition('\n')[0]
38+
data.append(f"{prefix}{command.name} - {description}")
39+
help_text = "\n".join(data)
40+
embed.add_field(name=i.capitalize(), value=f'```{help_text}```', inline=False)
41+
await context.send(embed=embed)
42+
2543
@commands.hybrid_command(
2644
name="botinfo",
2745
description="Get some useful (or not) information about the bot.",

cogs/moderation.py

+42-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
Description:
44
This is a template to create your own discord bot in python.
55
6-
Version: 5.0
6+
Version: 5.1
77
"""
88

9-
from doctest import debug_script
109
import discord
1110
from discord import app_commands
1211
from discord.ext import commands
@@ -152,14 +151,23 @@ async def ban(self, context: Context, user: discord.User, reason: str = "Not spe
152151
)
153152
await context.send(embed=embed)
154153

155-
@commands.hybrid_command(
156-
name="warn",
157-
description="Warns a user in the server.",
154+
@commands.hybrid_group(
155+
name="warning",
156+
description="Manage warnings of a user on a server.",
158157
)
159158
@commands.has_permissions(manage_messages=True)
160159
@checks.not_blacklisted()
160+
async def warning(self, context: Context) -> None:
161+
pass
162+
163+
@warning.command(
164+
name="add",
165+
description="Adds a warning to a user in the server.",
166+
)
167+
@checks.not_blacklisted()
168+
@commands.has_permissions(manage_messages=True)
161169
@app_commands.describe(user="The user that should be warned.", reason="The reason why the user should be warned.")
162-
async def warn(self, context: Context, user: discord.User, reason: str = "Not specified") -> None:
170+
async def warning_add(self, context: Context, user: discord.User, reason: str = "Not specified") -> None:
163171
"""
164172
Warns a user in his private messages.
165173
@@ -185,14 +193,38 @@ async def warn(self, context: Context, user: discord.User, reason: str = "Not sp
185193
# Couldn't send a message in the private messages of the user
186194
await context.send(f"{member.mention}, you were warned by **{context.author}**!\nReason: {reason}")
187195

188-
@commands.hybrid_command(
189-
name="warnings",
196+
@warning.command(
197+
name="remove",
198+
description="Removes a warning from a user in the server.",
199+
)
200+
@checks.not_blacklisted()
201+
@commands.has_permissions(manage_messages=True)
202+
@app_commands.describe(user="The user that should get their warning removed.", warn_id="The ID of the warning that should be removed.")
203+
async def warning_add(self, context: Context, user: discord.User, warn_id: int) -> None:
204+
"""
205+
Warns a user in his private messages.
206+
207+
:param context: The hybrid command context.
208+
:param user: The user that should get their warning removed.
209+
:param warn_id: The ID of the warning that should be removed.
210+
"""
211+
member = context.guild.get_member(user.id) or await context.guild.fetch_member(user.id)
212+
total = db_manager.remove_warn(warn_id, user.id, context.guild.id)
213+
embed = discord.Embed(
214+
title="User Warn Removed!",
215+
description=f"I've removed the warning **#{warn_id}** from **{member}**!\nTotal warns for this user: {total}",
216+
color=0x9C84EF
217+
)
218+
await context.send(embed=embed)
219+
220+
@warning.command(
221+
name="list",
190222
description="Shows the warnings of a user in the server.",
191223
)
192224
@commands.has_guild_permissions(manage_messages=True)
193225
@checks.not_blacklisted()
194226
@app_commands.describe(user="The user you want to get the warnings of.")
195-
async def warnings(self, context: Context, user: discord.User):
227+
async def warning_list(self, context: Context, user: discord.User):
196228
"""
197229
Shows the warnings of a user in the server.
198230
@@ -209,7 +241,7 @@ async def warnings(self, context: Context, user: discord.User):
209241
description = "This user has no warnings."
210242
else:
211243
for warning in warnings_list:
212-
description += f"• Warned by <@{warning[2]}>: **{warning[3]}** (<t:{warning[4]}>)\n"
244+
description += f"• Warned by <@{warning[2]}>: **{warning[3]}** (<t:{warning[4]}>) - Warn ID #{warning[5]}\n"
213245
embed.description = description
214246
await context.send(embed=embed)
215247

cogs/owner.py

+1-1
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: 5.0
6+
Version: 5.1
77
"""
88

99
import json

cogs/template.py

+1-1
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: 5.0
6+
Version: 5.1
77
"""
88

99
from discord.ext import commands

database/schema.sql

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS `blacklist` (
44
);
55

66
CREATE TABLE IF NOT EXISTS `warns` (
7+
`id` int(11) NOT NULL,
78
`user_id` varchar(20) NOT NULL,
89
`server_id` varchar(20) NOT NULL,
910
`moderator_id` varchar(20) NOT NULL,

exceptions/__init__.py

+1-1
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: 5.0
6+
Version: 5.1
77
"""
88

99
from discord.ext import commands

helpers/checks.py

+1-1
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: 5.0
6+
Version: 5.1
77
"""
88

99
import json

helpers/db_manager.py

+22-3
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: 5.0
6+
Version: 5.1
77
"""
88

99
import sqlite3
@@ -62,13 +62,32 @@ def add_warn(user_id: int, server_id: int, moderator_id: int, reason: str) -> in
6262
"""
6363
connection = sqlite3.connect("database/database.db")
6464
cursor = connection.cursor()
65-
cursor.execute("INSERT INTO warns(user_id, server_id, moderator_id, reason) VALUES (?, ?, ?, ?)", (user_id, server_id, moderator_id, reason))
65+
# Get the last `id`
66+
rows = cursor.execute("SELECT id FROM warns WHERE user_id=? AND server_id=? ORDER BY id DESC LIMIT 1", (user_id, server_id,)).fetchone()
67+
warn_id = rows[0]+1 if rows is not None else 1
68+
cursor.execute("INSERT INTO warns(id, user_id, server_id, moderator_id, reason) VALUES (?, ?, ?, ?, ?)", (warn_id, user_id, server_id, moderator_id, reason,))
6669
connection.commit()
6770
rows = cursor.execute("SELECT COUNT(*) FROM warns WHERE user_id=? AND server_id=?", (user_id, server_id,)).fetchone()[0]
6871
connection.close()
6972
return rows
7073

7174

75+
def remove_warn(warn_id: int, user_id: int, server_id: int) -> int:
76+
"""
77+
This function will remove a warn from the database.
78+
79+
:param warn_id: The ID of the warn.
80+
:param user_id: The ID of the user that was warned.
81+
:param server_id: The ID of the server where the user has been warned
82+
"""
83+
connection = sqlite3.connect("database/database.db")
84+
cursor = connection.cursor()
85+
cursor.execute("DELETE FROM warns WHERE id=? AND user_id=? AND server_id=?", (warn_id, user_id, server_id,))
86+
connection.commit()
87+
rows = cursor.execute("SELECT COUNT(*) FROM warns WHERE user_id=? AND server_id=?", (user_id, server_id,)).fetchone()[0]
88+
connection.close()
89+
return rows
90+
7291
def get_warnings(user_id: int, server_id: int) -> list:
7392
"""
7493
This function will get all the warnings of a user.
@@ -79,7 +98,7 @@ def get_warnings(user_id: int, server_id: int) -> list:
7998
"""
8099
connection = sqlite3.connect("database/database.db")
81100
cursor = connection.cursor()
82-
cursor.execute("SELECT user_id, server_id, moderator_id, reason, strftime('%s', created_at) FROM warns WHERE user_id=? AND server_id=?", (user_id, server_id,))
101+
cursor.execute("SELECT user_id, server_id, moderator_id, reason, strftime('%s', created_at), id FROM warns WHERE user_id=? AND server_id=?", (user_id, server_id,))
83102
result = cursor.fetchall()
84103
connection.close()
85104
return result

0 commit comments

Comments
 (0)