Skip to content

Commit

Permalink
🏷️ Template v5.3
Browse files Browse the repository at this point in the history
* Using `aiosqlite` instead of `sqlite3` for asynchronous database operations.
  • Loading branch information
kkrypt0nn committed Oct 17, 2022
1 parent 79f57ba commit fd9dffa
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 105 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# TODO List

## Version 4.2
## Version 5.5

* [ ] Add a timeout command

## Version 4.3
## Version 5.6

* [ ] 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
Expand Down
4 changes: 4 additions & 0 deletions UPDATES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

### Version 5.3 (17 October 2022)

* Using `aiosqlite` instead of `sqlite3` for asynchronous database operations.

### Version 5.2.1 (04 October 2022)

* Added error message when subcommands are not given
Expand Down
23 changes: 8 additions & 15 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
Description:
This is a template to create your own discord bot in python.
Version: 5.2.1
Version: 5.3
"""

import asyncio
import json
import os
import platform
import random
import sqlite3
import sys
from contextlib import closing

import aiosqlite
import discord
from discord import Interaction
from discord.ext import commands, tasks
from discord.ext.commands import Bot, Context

Expand Down Expand Up @@ -75,15 +73,11 @@
config["prefix"]), intents=intents, help_command=None)


def init_db():
with closing(connect_db()) as db:
with open("database/schema.sql", "r") as f:
db.cursor().executescript(f.read())
db.commit()


def connect_db():
return sqlite3.connect("database/database.db")
async def init_db():
async with aiosqlite.connect("database/database.db") as db:
with open("database/schema.sql") as file:
await db.executescript(file.read())
await db.commit()


"""
Expand All @@ -94,7 +88,6 @@ def connect_db():
- self.bot.config # In cogs
"""
bot.config = config
bot.db = connect_db()


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


init_db()
asyncio.run(init_db())
asyncio.run(load_cogs())
bot.run(config["token"])
3 changes: 2 additions & 1 deletion cogs/fun.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: 5.2.1
Version: 5.3
"""

import random
Expand All @@ -12,6 +12,7 @@
import discord
from discord.ext import commands
from discord.ext.commands import Context

from helpers import checks


Expand Down
25 changes: 14 additions & 11 deletions cogs/general.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: 5.2.1
Version: 5.3
"""

import platform
Expand All @@ -26,9 +26,11 @@ def __init__(self, bot):
name="help",
description="List all commands the bot has loaded."
)
@checks.not_blacklisted()
async def help(self, context: Context) -> None:
prefix = self.bot.config["prefix"]
embed = discord.Embed(title="Help", description="List of available commands:", color=0x9C84EF)
embed = discord.Embed(
title="Help", description="List of available commands:", color=0x9C84EF)
for i in self.bot.cogs:
cog = self.bot.get_cog(i.lower())
commands = cog.get_commands()
Expand All @@ -37,7 +39,8 @@ async def help(self, context: Context) -> None:
description = command.description.partition('\n')[0]
data.append(f"{prefix}{command.name} - {description}")
help_text = "\n".join(data)
embed.add_field(name=i.capitalize(), value=f'```{help_text}```', inline=False)
embed.add_field(name=i.capitalize(),
value=f'```{help_text}```', inline=False)
await context.send(embed=embed)

@commands.hybrid_command(
Expand All @@ -48,7 +51,7 @@ async def help(self, context: Context) -> None:
async def botinfo(self, context: Context) -> None:
"""
Get some useful (or not) information about the bot.
:param context: The hybrid command context.
"""
embed = discord.Embed(
Expand Down Expand Up @@ -86,7 +89,7 @@ async def botinfo(self, context: Context) -> None:
async def serverinfo(self, context: Context) -> None:
"""
Get some useful (or not) information about the server.
:param context: The hybrid command context.
"""
roles = [role.name for role in context.guild.roles]
Expand All @@ -100,7 +103,7 @@ async def serverinfo(self, context: Context) -> None:
description=f"{context.guild}",
color=0x9C84EF
)
if context.guild.icon is not None:
if context.guild.icon is not None:
embed.set_thumbnail(
url=context.guild.icon.url
)
Expand Down Expand Up @@ -133,7 +136,7 @@ async def serverinfo(self, context: Context) -> None:
async def ping(self, context: Context) -> None:
"""
Check if the bot is alive.
:param context: The hybrid command context.
"""
embed = discord.Embed(
Expand All @@ -151,7 +154,7 @@ async def ping(self, context: Context) -> None:
async def invite(self, context: Context) -> None:
"""
Get the invite link of the bot to be able to invite it.
:param context: The hybrid command context.
"""
embed = discord.Embed(
Expand All @@ -173,7 +176,7 @@ async def invite(self, context: Context) -> None:
async def server(self, context: Context) -> None:
"""
Get the invite link of the discord server of the bot for some support.
:param context: The hybrid command context.
"""
embed = discord.Embed(
Expand All @@ -195,7 +198,7 @@ async def server(self, context: Context) -> None:
async def eight_ball(self, context: Context, *, question: str) -> None:
"""
Ask any question to the bot.
:param context: The hybrid command context.
:param question: The question that should be asked by the user.
"""
Expand All @@ -222,7 +225,7 @@ async def eight_ball(self, context: Context, *, question: str) -> None:
async def bitcoin(self, context: Context) -> None:
"""
Get the current price of bitcoin.
:param context: The hybrid command context.
"""
# 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
Expand Down
9 changes: 5 additions & 4 deletions cogs/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
Description:
This is a template to create your own discord bot in python.
Version: 5.2.1
Version: 5.3
"""

import discord
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context

from helpers import checks, db_manager


Expand Down Expand Up @@ -185,7 +186,7 @@ async def warning_add(self, context: Context, user: discord.User, *, reason: str
:param reason: The reason for the warn. Default is "Not specified".
"""
member = context.guild.get_member(user.id) or await context.guild.fetch_member(user.id)
total = db_manager.add_warn(
total = await db_manager.add_warn(
user.id, context.guild.id, context.author.id, reason)
embed = discord.Embed(
title="User Warned!",
Expand Down Expand Up @@ -219,7 +220,7 @@ async def warning_remove(self, context: Context, user: discord.User, warn_id: in
:param warn_id: The ID of the warning that should be removed.
"""
member = context.guild.get_member(user.id) or await context.guild.fetch_member(user.id)
total = db_manager.remove_warn(warn_id, user.id, context.guild.id)
total = await db_manager.remove_warn(warn_id, user.id, context.guild.id)
embed = discord.Embed(
title="User Warn Removed!",
description=f"I've removed the warning **#{warn_id}** from **{member}**!\nTotal warns for this user: {total}",
Expand All @@ -241,7 +242,7 @@ async def warning_list(self, context: Context, user: discord.User):
:param context: The hybrid command context.
:param user: The user you want to get the warnings of.
"""
warnings_list = db_manager.get_warnings(user.id, context.guild.id)
warnings_list = await db_manager.get_warnings(user.id, context.guild.id)
embed = discord.Embed(
title=f"Warnings of {user}",
color=0x9C84EF
Expand Down
11 changes: 6 additions & 5 deletions cogs/owner.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: 5.2.1
Version: 5.3
"""

import json
Expand All @@ -14,6 +14,7 @@
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context

from helpers import checks, db_manager


Expand Down Expand Up @@ -278,15 +279,15 @@ async def blacklist_add(self, context: Context, user: discord.User) -> None:
:param user: The user that should be added to the blacklist.
"""
user_id = user.id
if db_manager.is_blacklisted(user_id):
if await db_manager.is_blacklisted(user_id):
embed = discord.Embed(
title="Error!",
description=f"**{user.name}** is not in the blacklist.",
color=0xE02B2B
)
await context.send(embed=embed)
return
total = db_manager.add_user_to_blacklist(user_id)
total = await db_manager.add_user_to_blacklist(user_id)
embed = discord.Embed(
title="User Blacklisted",
description=f"**{user.name}** has been successfully added to the blacklist",
Expand All @@ -312,15 +313,15 @@ async def blacklist_remove(self, context: Context, user: discord.User) -> None:
:param user: The user that should be removed from the blacklist.
"""
user_id = user.id
if not db_manager.is_blacklisted(user_id):
if not await db_manager.is_blacklisted(user_id):
embed = discord.Embed(
title="Error!",
description=f"**{user.name}** is already in the blacklist.",
color=0xE02B2B
)
await context.send(embed=embed)
return
total = db_manager.remove_user_from_blacklist(user_id)
total = await db_manager.remove_user_from_blacklist(user_id)
embed = discord.Embed(
title="User removed from blacklist",
description=f"**{user.name}** has been successfully removed from the blacklist",
Expand Down
3 changes: 2 additions & 1 deletion cogs/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
Description:
This is a template to create your own discord bot in python.
Version: 5.2.1
Version: 5.3
"""

from discord.ext import commands
from discord.ext.commands import Context

from helpers import checks


Expand Down
2 changes: 1 addition & 1 deletion exceptions/__init__.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: 5.2.1
Version: 5.3
"""

from discord.ext import commands
Expand Down
8 changes: 3 additions & 5 deletions helpers/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
Description:
This is a template to create your own discord bot in python.
Version: 5.2.1
Version: 5.3
"""

import json
from typing import Callable, TypeVar

from discord.ext import commands
from exceptions import *

from exceptions import *
from helpers import db_manager

T = TypeVar("T")
Expand All @@ -21,7 +21,6 @@ def is_owner() -> Callable[[T], T]:
"""
This is a custom check to see if the user executing the command is an owner of the bot.
"""

async def predicate(context: commands.Context) -> bool:
with open("config.json") as file:
data = json.load(file)
Expand All @@ -36,9 +35,8 @@ def not_blacklisted() -> Callable[[T], T]:
"""
This is a custom check to see if the user executing the command is blacklisted.
"""

async def predicate(context: commands.Context) -> bool:
if db_manager.is_blacklisted(context.author.id):
if await db_manager.is_blacklisted(context.author.id):
raise UserBlacklisted
return True

Expand Down
Loading

0 comments on commit fd9dffa

Please sign in to comment.