Skip to content

Commit

Permalink
Merge pull request #12 from PaulMarisOUMary/database
Browse files Browse the repository at this point in the history
merge new database implementation
  • Loading branch information
PaulMarisOUMary committed Mar 2, 2022
2 parents 723cec4 + 935765c commit 071d97c
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ cython_debug/
.vscode/

# Project
auth/auth.json
config

# MacOS (should be included in your local .gitignore)
.DS_Store
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ $ python3 -m pip install -U ".[voice]"

1. Paste your dicord bot token in the `"token"` field inside `auth/auth.json`.

2. (Optional) If you're using the database, you need to configure the database field `auth/auth.json`.
2. Configure the prefix in the `config\bot.json`.

3. (Optional) Edit line `10` in `bot.py` to change the bot prefix : `command_prefix=commands.when_mentioned_or("`*PREFIX_HERE*`")`.
3. If you're using the database, you need to configure the `config\database.json` file.
:warning: If you're NOT using any database, delete the following cogs: `fridaycake`, `me` & `birthday`.

## SQL

Expand Down
24 changes: 0 additions & 24 deletions auth/auth.json

This file was deleted.

40 changes: 25 additions & 15 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@
import json
import discord

from classes.database import DataSQL
from discord.ext import commands

intents = discord.Intents.all()

bot = commands.Bot(command_prefix=commands.when_mentioned_or("?"), description='Algobot', intents=intents, help_command=None)

base_directory = os.path.dirname(os.path.abspath(__file__))
auth_file = os.path.join(base_directory, "auth", "auth.json")

with open(auth_file, "r") as data: json_data = json.load(data)

if __name__ == '__main__':
cogs_directory = os.path.join(base_directory, "cogs")
for cog in os.listdir(cogs_directory):
actual = os.path.splitext(cog)
if actual[1] == '.py':
bot.load_extension('cogs.'+actual[0])
bot_file = os.path.join(base_directory, "config", "bot.json")
database_file = os.path.join(base_directory, "config", "database.json")

with open(bot_file, "r") as bdata, open(database_file, "r") as ddata:
bot_data, database_data = json.load(bdata), json.load(ddata)

async def initBot() -> None:
"""Initialize the bot."""
if __name__ == '__main__':
# Database connector
bot.database_data, server = database_data, database_data["server"]
bot.database = DataSQL(server["host"], server["port"])
await bot.database.auth(server["user"], server["password"], server["database"])

# Cogs loader
cogs_directory = os.path.join(base_directory, "cogs")
for cog in os.listdir(cogs_directory):
actual = os.path.splitext(cog)
if actual[1] == '.py':
bot.load_extension('cogs.'+actual[0])

bot = commands.Bot(command_prefix=commands.when_mentioned_or(bot_data["bot_prefix"]), description=bot_data["bot_description"], intents=discord.Intents.all(), help_command=None)
bot.loop.create_task(initBot())

@bot.event
async def on_ready():
print("Logged in as: "+str(bot.user)+"\nVersion: "+str(discord.__version__))

bot.run(json_data["token"], reconnect=True)
bot.run(bot_data["token"], reconnect=True)
3 changes: 2 additions & 1 deletion classes/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from datetime import datetime, date
import aiomysql
import asyncio

from datetime import datetime, date

class DataSQL():
def __init__(self, host:str = "127.0.0.1", port:int = 3306, loop:asyncio.AbstractEventLoop = None) -> None:
self.loop, self.host, self.port, self.connector = loop, host, port, None
Expand Down
4 changes: 2 additions & 2 deletions cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def help_custom(self):
description = "Show the list of admin commands."
return emoji, label, description

async def reload_views(self):
async def reload_views(self) -> list[str]:
modules, infants = [], []
for module in sys.modules.items():
if isinstance(module[1], types.ModuleType):
Expand All @@ -32,7 +32,7 @@ async def reload_views(self):

return infants

async def reload_cogs(self, cogs):
async def reload_cogs(self, cogs) -> list[str]:
victims = []
for cog in cogs:
norm_cog = self.bot.get_cog(cog[5:len(cog)])
Expand Down
29 changes: 9 additions & 20 deletions cogs/birthday.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import os
import json
import time
import random
import asyncio
import discord

from classes.database import DataSQL

from datetime import datetime, date
from discord.ext import commands, tasks

auth_directory = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "auth", "auth.json")
with open(auth_directory, "r") as data: database_data = json.load(data)["database"]
birthday_data = database_data["birthday"]

class Birthday(commands.Cog, name="birthday"):
"""I'll wish you soon a happy birthday!"""
def __init__(self, bot):
self.bot = bot

self.bot.loop.create_task(self.initBirthday())
self.birthday_data = self.bot.database_data["birthday"]

self.daily_birthday.start()

def help_custom(self):
Expand All @@ -28,20 +21,16 @@ def help_custom(self):
description = "Maybe I'll wish you soon a happy birthday!"
return emoji, label, description

async def initBirthday(self):
self.database = DataSQL(database_data["host"], database_data["port"])
await self.database.auth(database_data["user"], database_data["password"], birthday_data["database"])

def cog_unload(self):
self.daily_birthday.cancel()

@tasks.loop(hours=1)
async def daily_birthday(self):
if datetime.now().hour == 9:
guild = self.bot.get_guild(int(birthday_data["guild_id"]))
channel = guild.get_channel(int(birthday_data["channel_id"]))
guild = self.bot.get_guild(int(self.birthday_data["guild_id"]))
channel = guild.get_channel(int(self.birthday_data["channel_id"]))

response = await self.database.select(birthday_data["table"], "*")
response = await self.bot.database.select(self.birthday_data["table"], "*")
for data in response:
user_id, user_birth = data[0], data[1]

Expand All @@ -62,7 +51,7 @@ async def daily_birthday(self):
@daily_birthday.before_loop
async def before_daily_birthday(self):
await self.bot.wait_until_ready()
while self.database.connector is None: await asyncio.sleep(0.01) #wait_for initBirthday
while self.bot.database.connector is None: await asyncio.sleep(0.01) #wait_for initBirthday

@commands.command(name='birthday', aliases=['bd', 'setbirthday', 'setbirth', 'birth'])
@commands.cooldown(1, 10, commands.BucketType.user)
Expand All @@ -73,9 +62,9 @@ async def birthday(self, ctx, date: str = None):
dataDate = datetime.strptime(date, "%d/%m/%Y").date()
if dataDate.year > datetime.now().year - 15 or dataDate.year < datetime.now().year - 99: raise commands.CommandError("Please provide your real year of birth.")
# Insert
await self.database.insert(birthday_data["table"], {"user_id": ctx.author.id, "user_birth": dataDate})
await self.bot.database.insert(self.birthday_data["table"], {"user_id": ctx.author.id, "user_birth": dataDate})
# Update
await self.database.update(birthday_data["table"], "user_birth", dataDate, "user_id = "+str(ctx.author.id))
await self.bot.database.update(self.birthday_data["table"], "user_birth", dataDate, "user_id = "+str(ctx.author.id))

await self.show_birthday_message(ctx, ctx.author)
except ValueError:
Expand All @@ -93,7 +82,7 @@ async def show_birthday(self, ctx, user:discord.Member = None):
await self.show_birthday_message(ctx, user)

async def show_birthday_message(self, ctx, user:discord.Member) -> None:
response = await self.database.lookup(birthday_data["table"], "user_birth", "user_id", str(user.id))
response = await self.bot.database.lookup(self.birthday_data["table"], "user_birth", "user_id", str(user.id))
if response:
dataDate : date = response[0][0]
timestamp = round(time.mktime(dataDate.timetuple()))
Expand Down
3 changes: 0 additions & 3 deletions cogs/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import discord

from discord.ext import commands
from discord.ext.commands import errors

class Errors(commands.Cog, name="errors", command_attrs=dict(hidden=True)):
"""Errors handler"""
Expand Down
30 changes: 10 additions & 20 deletions cogs/fridaycake.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import os
import json
import random
import discord
import asyncio

from views import fridaycake
from classes.database import DataSQL

from datetime import date, timedelta, datetime
from discord.ext import commands
from copy import deepcopy

holidays = [(date(2022, 2, 12), date(2022, 2, 19)), (date(2022, 4, 10), date(2022, 4, 23))] #Saturday -> Saturday
start = date(2022, 2, 4)#date(2021, 10, 8)#date(2021, 2, 7) #year #month #day (first friday)
seed = 2

auth_directory = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "auth", "auth.json")
with open(auth_directory, "r") as data: database_data = json.load(data)["database"]
fridaycake_data = database_data["fridaycake"]

def isDateInHole(date : datetime, holes : list) -> bool:
for hole in holes:
Expand Down Expand Up @@ -52,28 +44,26 @@ def mix_participants(participants : list, seed : int, n_group : int) -> list[lis
class FridayCake(commands.Cog, name="fridaycake", command_attrs=dict(hidden=False)):
"""FridayCake's event commands."""
def __init__(self, bot):
self.bot, self.seed = bot, seed
self.bot = bot
self.cakes = ['🎂', '🥮', '🥧', '🥯', '🧁', '🫓', '🧇', '🍞', '🍮', '🍰', '🥐']

self.fridaycake_data = self.bot.database_data["fridaycake"]
self.seed = self.fridaycake_data["seed"]

self.bot.loop.create_task(self.initFridaycake())

async def initFridaycake(self):
participants = await self.bot.database.select(self.fridaycake_data["table"], "user_id, user_name", "user_isin = 1")
participants = [[*row] for row in participants] #convert tuple of tuples to list of lists
self.participants = mix_participants(participants, self.seed, 2)
self.nparticipants = len(participants) #mandatory in fridaycake view

def help_custom(self):
emoji = random.choice(self.cakes)
label = "FridayCake"
description = "Commands relative to the FridayCake event !"
return emoji, label, description

def cog_unload(self) -> None:
self.database.close()

async def initFridaycake(self):
self.database = DataSQL(database_data["host"], database_data["port"])
await self.database.auth(database_data["user"], database_data["password"], database_data["fridaycake"]["database"])

participants = await self.database.select(fridaycake_data["table"], "user_id, user_name", "user_isin = 1")
participants = [[*row] for row in participants] #convert tuple of tuples to list of lists
self.participants = mix_participants(participants, seed, 2)
self.nparticipants = len(participants) #mandatory in fridaycake view

def all(self, ctx):
author = ctx.message.author
Expand Down
29 changes: 7 additions & 22 deletions cogs/me.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
import os
import json
import discord

from classes.database import DataSQL

from discord.ext import commands

auth_directory = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "auth", "auth.json")
with open(auth_directory, "r") as data: database_data = json.load(data)["database"]
me_data = database_data["me"]
max_lenght_me = 255

class Me(commands.Cog, name="me", command_attrs=dict(hidden=False)):
"""FridayCake's event commands."""
def __init__(self, bot):
self.bot = bot

self.bot.loop.create_task(self.initMe())

self.me_data = self.bot.database_data["me"]
self.max_lenght_me = self.me_data["max_length"]

def help_custom(self):
emoji = '🤸'
label = "Me"
description = "Set and show a brief description of yourself."
return emoji, label, description

def cog_unload(self) -> None:
self.database.close()

async def initMe(self):
self.database = DataSQL(database_data["host"], database_data["port"])
await self.database.auth(database_data["user"], database_data["password"], database_data["fridaycake"]["database"])

@commands.command(name='me', aliases=['description'])
@commands.cooldown(1, 10, commands.BucketType.user)
async def me(self, ctx, *args:str):
"""Allows you to set or show a brief description of yourself."""
if len(args):
try:
text = " ".join(args).replace("'", "''")
if len(text) > max_lenght_me: raise commands.CommandError("The max-lenght of your *me* is set to: __"+str(max_lenght_me)+"__ (yours is "+str(len(text))+").")
if len(text) > self.max_lenght_me: raise commands.CommandError("The max-lenght of your *me* is set to: __"+str(self.max_lenght_me)+"__ (yours is "+str(len(text))+").")
# Insert
await self.database.insert(me_data["table"], {"user_id": ctx.author.id, "user_me": text})
await self.bot.database.insert(self.me_data["table"], {"user_id": ctx.author.id, "user_me": text})
# Update
await self.database.update(me_data["table"], "user_me", text, "user_id = "+str(ctx.author.id))
await self.bot.database.update(self.me_data["table"], "user_me", text, "user_id = "+str(ctx.author.id))
await self.show_me_message(ctx, ctx.author)
except Exception as e:
raise commands.CommandError(str(e))
Expand All @@ -57,7 +42,7 @@ async def show_me(self, ctx, user:discord.Member = None):
await self.show_me_message(ctx, user)

async def show_me_message(self, ctx, user:discord.Member) -> None:
response = await self.database.lookup(me_data["table"], "user_me", "user_id", str(user.id))
response = await self.bot.database.lookup(self.me_data["table"], "user_me", "user_id", str(user.id))
message = " ".join(response[0]) if len(response) else "No description provided.."
await ctx.send("• **"+ user.display_name + "** " + message)

Expand Down
2 changes: 0 additions & 2 deletions cogs/usefull.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import discord

from discord.ext import commands

class Usefull(commands.Cog, name="usefull", command_attrs=dict(hidden=False)):
Expand Down
5 changes: 5 additions & 0 deletions config/bot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"token": "*************************************.******.**********************",
"bot_description": "Algobot",
"bot_prefix": "?"
}
25 changes: 25 additions & 0 deletions config/database.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"server": {
"host": "127.0.0.1",
"port": 3306,
"user": "user",
"password": "********",
"database": "database"
},
"birthday": {
"table": "table_birthday",

"guild_id": 332234497078853644,
"channel_id": 332234497078853644
},
"fridaycake": {
"table": "table_fridaycake",

"seed": 2
},
"me": {
"table": "table_me",

"max_length": 255
}
}

0 comments on commit 071d97c

Please sign in to comment.