Skip to content

Commit fd9dffa

Browse files
committed
🏷️ Template v5.3
* Using `aiosqlite` instead of `sqlite3` for asynchronous database operations.
1 parent 79f57ba commit fd9dffa

File tree

12 files changed

+101
-105
lines changed

12 files changed

+101
-105
lines changed

TODO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# TODO List
22

3-
## Version 4.2
3+
## Version 5.5
44

55
* [ ] Add a timeout command
66

7-
## Version 4.3
7+
## Version 5.6
88

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

UPDATES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
### Version 5.3 (17 October 2022)
6+
7+
* Using `aiosqlite` instead of `sqlite3` for asynchronous database operations.
8+
59
### Version 5.2.1 (04 October 2022)
610

711
* Added error message when subcommands are not given

bot.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
Description:
44
This is a template to create your own discord bot in python.
55
6-
Version: 5.2.1
6+
Version: 5.3
77
"""
88

99
import asyncio
1010
import json
1111
import os
1212
import platform
1313
import random
14-
import sqlite3
1514
import sys
16-
from contextlib import closing
1715

16+
import aiosqlite
1817
import discord
19-
from discord import Interaction
2018
from discord.ext import commands, tasks
2119
from discord.ext.commands import Bot, Context
2220

@@ -75,15 +73,11 @@
7573
config["prefix"]), intents=intents, help_command=None)
7674

7775

78-
def init_db():
79-
with closing(connect_db()) as db:
80-
with open("database/schema.sql", "r") as f:
81-
db.cursor().executescript(f.read())
82-
db.commit()
83-
84-
85-
def connect_db():
86-
return sqlite3.connect("database/database.db")
76+
async def init_db():
77+
async with aiosqlite.connect("database/database.db") as db:
78+
with open("database/schema.sql") as file:
79+
await db.executescript(file.read())
80+
await db.commit()
8781

8882

8983
"""
@@ -94,7 +88,6 @@ def connect_db():
9488
- self.bot.config # In cogs
9589
"""
9690
bot.config = config
97-
bot.db = connect_db()
9891

9992

10093
@bot.event
@@ -220,6 +213,6 @@ async def load_cogs() -> None:
220213
print(f"Failed to load extension {extension}\n{exception}")
221214

222215

223-
init_db()
216+
asyncio.run(init_db())
224217
asyncio.run(load_cogs())
225218
bot.run(config["token"])

cogs/fun.py

Lines changed: 2 additions & 1 deletion
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.2.1
6+
Version: 5.3
77
"""
88

99
import random
@@ -12,6 +12,7 @@
1212
import discord
1313
from discord.ext import commands
1414
from discord.ext.commands import Context
15+
1516
from helpers import checks
1617

1718

cogs/general.py

Lines changed: 14 additions & 11 deletions
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.2.1
6+
Version: 5.3
77
"""
88

99
import platform
@@ -26,9 +26,11 @@ def __init__(self, bot):
2626
name="help",
2727
description="List all commands the bot has loaded."
2828
)
29+
@checks.not_blacklisted()
2930
async def help(self, context: Context) -> None:
3031
prefix = self.bot.config["prefix"]
31-
embed = discord.Embed(title="Help", description="List of available commands:", color=0x9C84EF)
32+
embed = discord.Embed(
33+
title="Help", description="List of available commands:", color=0x9C84EF)
3234
for i in self.bot.cogs:
3335
cog = self.bot.get_cog(i.lower())
3436
commands = cog.get_commands()
@@ -37,7 +39,8 @@ async def help(self, context: Context) -> None:
3739
description = command.description.partition('\n')[0]
3840
data.append(f"{prefix}{command.name} - {description}")
3941
help_text = "\n".join(data)
40-
embed.add_field(name=i.capitalize(), value=f'```{help_text}```', inline=False)
42+
embed.add_field(name=i.capitalize(),
43+
value=f'```{help_text}```', inline=False)
4144
await context.send(embed=embed)
4245

4346
@commands.hybrid_command(
@@ -48,7 +51,7 @@ async def help(self, context: Context) -> None:
4851
async def botinfo(self, context: Context) -> None:
4952
"""
5053
Get some useful (or not) information about the bot.
51-
54+
5255
:param context: The hybrid command context.
5356
"""
5457
embed = discord.Embed(
@@ -86,7 +89,7 @@ async def botinfo(self, context: Context) -> None:
8689
async def serverinfo(self, context: Context) -> None:
8790
"""
8891
Get some useful (or not) information about the server.
89-
92+
9093
:param context: The hybrid command context.
9194
"""
9295
roles = [role.name for role in context.guild.roles]
@@ -100,7 +103,7 @@ async def serverinfo(self, context: Context) -> None:
100103
description=f"{context.guild}",
101104
color=0x9C84EF
102105
)
103-
if context.guild.icon is not None:
106+
if context.guild.icon is not None:
104107
embed.set_thumbnail(
105108
url=context.guild.icon.url
106109
)
@@ -133,7 +136,7 @@ async def serverinfo(self, context: Context) -> None:
133136
async def ping(self, context: Context) -> None:
134137
"""
135138
Check if the bot is alive.
136-
139+
137140
:param context: The hybrid command context.
138141
"""
139142
embed = discord.Embed(
@@ -151,7 +154,7 @@ async def ping(self, context: Context) -> None:
151154
async def invite(self, context: Context) -> None:
152155
"""
153156
Get the invite link of the bot to be able to invite it.
154-
157+
155158
:param context: The hybrid command context.
156159
"""
157160
embed = discord.Embed(
@@ -173,7 +176,7 @@ async def invite(self, context: Context) -> None:
173176
async def server(self, context: Context) -> None:
174177
"""
175178
Get the invite link of the discord server of the bot for some support.
176-
179+
177180
:param context: The hybrid command context.
178181
"""
179182
embed = discord.Embed(
@@ -195,7 +198,7 @@ async def server(self, context: Context) -> None:
195198
async def eight_ball(self, context: Context, *, question: str) -> None:
196199
"""
197200
Ask any question to the bot.
198-
201+
199202
:param context: The hybrid command context.
200203
:param question: The question that should be asked by the user.
201204
"""
@@ -222,7 +225,7 @@ async def eight_ball(self, context: Context, *, question: str) -> None:
222225
async def bitcoin(self, context: Context) -> None:
223226
"""
224227
Get the current price of bitcoin.
225-
228+
226229
:param context: The hybrid command context.
227230
"""
228231
# This will prevent your bot from stopping everything when doing a web request - see: https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-make-a-web-request

cogs/moderation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
Description:
44
This is a template to create your own discord bot in python.
55
6-
Version: 5.2.1
6+
Version: 5.3
77
"""
88

99
import discord
1010
from discord import app_commands
1111
from discord.ext import commands
1212
from discord.ext.commands import Context
13+
1314
from helpers import checks, db_manager
1415

1516

@@ -185,7 +186,7 @@ async def warning_add(self, context: Context, user: discord.User, *, reason: str
185186
:param reason: The reason for the warn. Default is "Not specified".
186187
"""
187188
member = context.guild.get_member(user.id) or await context.guild.fetch_member(user.id)
188-
total = db_manager.add_warn(
189+
total = await db_manager.add_warn(
189190
user.id, context.guild.id, context.author.id, reason)
190191
embed = discord.Embed(
191192
title="User Warned!",
@@ -219,7 +220,7 @@ async def warning_remove(self, context: Context, user: discord.User, warn_id: in
219220
:param warn_id: The ID of the warning that should be removed.
220221
"""
221222
member = context.guild.get_member(user.id) or await context.guild.fetch_member(user.id)
222-
total = db_manager.remove_warn(warn_id, user.id, context.guild.id)
223+
total = await db_manager.remove_warn(warn_id, user.id, context.guild.id)
223224
embed = discord.Embed(
224225
title="User Warn Removed!",
225226
description=f"I've removed the warning **#{warn_id}** from **{member}**!\nTotal warns for this user: {total}",
@@ -241,7 +242,7 @@ async def warning_list(self, context: Context, user: discord.User):
241242
:param context: The hybrid command context.
242243
:param user: The user you want to get the warnings of.
243244
"""
244-
warnings_list = db_manager.get_warnings(user.id, context.guild.id)
245+
warnings_list = await db_manager.get_warnings(user.id, context.guild.id)
245246
embed = discord.Embed(
246247
title=f"Warnings of {user}",
247248
color=0x9C84EF

cogs/owner.py

Lines changed: 6 additions & 5 deletions
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.2.1
6+
Version: 5.3
77
"""
88

99
import json
@@ -14,6 +14,7 @@
1414
from discord import app_commands
1515
from discord.ext import commands
1616
from discord.ext.commands import Context
17+
1718
from helpers import checks, db_manager
1819

1920

@@ -278,15 +279,15 @@ async def blacklist_add(self, context: Context, user: discord.User) -> None:
278279
:param user: The user that should be added to the blacklist.
279280
"""
280281
user_id = user.id
281-
if db_manager.is_blacklisted(user_id):
282+
if await db_manager.is_blacklisted(user_id):
282283
embed = discord.Embed(
283284
title="Error!",
284285
description=f"**{user.name}** is not in the blacklist.",
285286
color=0xE02B2B
286287
)
287288
await context.send(embed=embed)
288289
return
289-
total = db_manager.add_user_to_blacklist(user_id)
290+
total = await db_manager.add_user_to_blacklist(user_id)
290291
embed = discord.Embed(
291292
title="User Blacklisted",
292293
description=f"**{user.name}** has been successfully added to the blacklist",
@@ -312,15 +313,15 @@ async def blacklist_remove(self, context: Context, user: discord.User) -> None:
312313
:param user: The user that should be removed from the blacklist.
313314
"""
314315
user_id = user.id
315-
if not db_manager.is_blacklisted(user_id):
316+
if not await db_manager.is_blacklisted(user_id):
316317
embed = discord.Embed(
317318
title="Error!",
318319
description=f"**{user.name}** is already in the blacklist.",
319320
color=0xE02B2B
320321
)
321322
await context.send(embed=embed)
322323
return
323-
total = db_manager.remove_user_from_blacklist(user_id)
324+
total = await db_manager.remove_user_from_blacklist(user_id)
324325
embed = discord.Embed(
325326
title="User removed from blacklist",
326327
description=f"**{user.name}** has been successfully removed from the blacklist",

cogs/template.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
Description:
44
This is a template to create your own discord bot in python.
55
6-
Version: 5.2.1
6+
Version: 5.3
77
"""
88

99
from discord.ext import commands
1010
from discord.ext.commands import Context
11+
1112
from helpers import checks
1213

1314

exceptions/__init__.py

Lines changed: 1 addition & 1 deletion
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.2.1
6+
Version: 5.3
77
"""
88

99
from discord.ext import commands

helpers/checks.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
Description:
44
This is a template to create your own discord bot in python.
55
6-
Version: 5.2.1
6+
Version: 5.3
77
"""
88

99
import json
1010
from typing import Callable, TypeVar
1111

1212
from discord.ext import commands
13-
from exceptions import *
1413

14+
from exceptions import *
1515
from helpers import db_manager
1616

1717
T = TypeVar("T")
@@ -21,7 +21,6 @@ def is_owner() -> Callable[[T], T]:
2121
"""
2222
This is a custom check to see if the user executing the command is an owner of the bot.
2323
"""
24-
2524
async def predicate(context: commands.Context) -> bool:
2625
with open("config.json") as file:
2726
data = json.load(file)
@@ -36,9 +35,8 @@ def not_blacklisted() -> Callable[[T], T]:
3635
"""
3736
This is a custom check to see if the user executing the command is blacklisted.
3837
"""
39-
4038
async def predicate(context: commands.Context) -> bool:
41-
if db_manager.is_blacklisted(context.author.id):
39+
if await db_manager.is_blacklisted(context.author.id):
4240
raise UserBlacklisted
4341
return True
4442

0 commit comments

Comments
 (0)