[GENERAL] Refactoring for DB

This commit is contained in:
Alexis LEBEL
2023-03-31 16:11:03 +02:00
parent 64df09b248
commit ddbeea3ae6
5 changed files with 88 additions and 88 deletions

View File

@@ -1,6 +1,6 @@
import discord
from discord.ext import commands
from config import debug, c, max_uses, cp, conn, connp, webhook_url
from config import debug, curs_data, max_uses, curs_premium, con_data, con_premium, webhook_url
import makeprompt as mp
import aiohttp

View File

@@ -1,7 +1,7 @@
import discord
import re
import os
from config import debug, c
from config import debug, curs_data
class ManageChat(discord.Cog):
@@ -17,8 +17,8 @@ class ManageChat(discord.Cog):
f"The user {ctx.author} ran the cancel command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
# check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if c.fetchone() is None:
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if curs_data.fetchone() is None:
await ctx.respond(
"This server is not setup, please run /setup", ephemeral=True
)

View File

@@ -1,7 +1,7 @@
import discord
from discord import default_permissions
import os
from config import debug, c, conn
from config import debug, curs_data, con_data
import openai
import requests
import toxicity as tox # this is a file called toxicity.py, which contains the toxicity function that allows you to check if a message is toxic or not (it uses the perspective api)
@@ -81,8 +81,8 @@ class Moderation(discord.Cog):
ephemeral=True,
)
if enable == False:
c.execute("DELETE FROM moderation WHERE guild_id = ?", (str(ctx.guild.id),))
conn.commit()
curs_data.execute("DELETE FROM moderation WHERE guild_id = ?", (str(ctx.guild.id),))
con_data.commit()
await ctx.respond("Moderation disabled!", ephemeral=True)
return
@@ -91,12 +91,12 @@ class Moderation(discord.Cog):
if message.author == self.bot.user:
return
try:
c.execute(
curs_data.execute(
"SELECT * FROM moderation WHERE guild_id = ?", (str(message.guild.id),)
)
except:
return
data = c.fetchone()
data = curs_data.fetchone()
if data is None:
return
channel = self.bot.get_channel(int(data[1]))

View File

@@ -1,5 +1,5 @@
import discord
from config import debug, conn, c, moderate
from config import debug, con_data, curs_data, moderate
from discord import default_permissions
import openai
@@ -38,8 +38,8 @@ class Settings(discord.Cog):
f"The user {ctx.author} ran the advanced command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
# check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if c.fetchone() is None:
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if curs_data.fetchone() is None:
await ctx.respond("This server is not setup", ephemeral=True)
return
# check if the user has entered at least one argument
@@ -74,40 +74,40 @@ class Settings(discord.Cog):
return
if max_tokens is None:
if (
c.execute(
curs_data.execute(
"SELECT max_tokens FROM data WHERE guild_id = ?", (ctx.guild.id,)
).fetchone()[0]
is not None
and max_tokens is None
):
max_tokens = c.execute(
max_tokens = curs_data.execute(
"SELECT max_tokens FROM data WHERE guild_id = ?", (ctx.guild.id,)
).fetchone()[0]
else:
max_tokens = 64
if temperature is None:
if (
c.execute(
curs_data.execute(
"SELECT temperature FROM data WHERE guild_id = ?", (ctx.guild.id,)
).fetchone()[0]
is not None
and temperature is None
):
temperature = c.execute(
temperature = curs_data.execute(
"SELECT temperature FROM data WHERE guild_id = ?", (ctx.guild.id,)
).fetchone()[0]
else:
temperature = 0.9
if frequency_penalty is None:
if (
c.execute(
curs_data.execute(
"SELECT frequency_penalty FROM data WHERE guild_id = ?",
(ctx.guild.id,),
).fetchone()[0]
is not None
and frequency_penalty is None
):
frequency_penalty = c.execute(
frequency_penalty = curs_data.execute(
"SELECT frequency_penalty FROM data WHERE guild_id = ?",
(ctx.guild.id,),
).fetchone()[0]
@@ -115,14 +115,14 @@ class Settings(discord.Cog):
frequency_penalty = 0.0
if presence_penalty is None:
if (
c.execute(
curs_data.execute(
"SELECT presence_penalty FROM data WHERE guild_id = ?",
(ctx.guild.id,),
).fetchone()[0]
is not None
and presence_penalty is None
):
presence_penalty = c.execute(
presence_penalty = curs_data.execute(
"SELECT presence_penalty FROM data WHERE guild_id = ?",
(ctx.guild.id,),
).fetchone()[0]
@@ -130,19 +130,19 @@ class Settings(discord.Cog):
presence_penalty = 0.0
if prompt_size is None:
if (
c.execute(
curs_data.execute(
"SELECT prompt_size FROM data WHERE guild_id = ?", (ctx.guild.id,)
).fetchone()[0]
is not None
and prompt_size is None
):
prompt_size = c.execute(
prompt_size = curs_data.execute(
"SELECT prompt_size FROM data WHERE guild_id = ?", (ctx.guild.id,)
).fetchone()[0]
else:
prompt_size = 1
# update the database
c.execute(
curs_data.execute(
"UPDATE data SET max_tokens = ?, temperature = ?, frequency_penalty = ?, presence_penalty = ?, prompt_size = ? WHERE guild_id = ?",
(
max_tokens,
@@ -153,7 +153,7 @@ class Settings(discord.Cog):
ctx.guild.id,
),
)
conn.commit()
con_data.commit()
await ctx.respond("Advanced settings updated", ephemeral=True)
# create a command called "delete" that only admins can use wich deletes the guild id, the api key, the channel id and the advanced settings from the database
@@ -164,18 +164,18 @@ class Settings(discord.Cog):
f"The user {ctx.author} ran the default command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
# check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if c.fetchone() is None:
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if curs_data.fetchone() is None:
await ctx.respond(
"This server is not setup, please run /setup", ephemeral=True
)
return
# set the advanced settings (max_tokens, temperature, frequency_penalty, presence_penalty, prompt_size) and also prompt_prefix to their default values
c.execute(
curs_data.execute(
"UPDATE data SET max_tokens = ?, temperature = ?, frequency_penalty = ?, presence_penalty = ?, prompt_size = ? WHERE guild_id = ?",
(64, 0.9, 0.0, 0.0, 5, ctx.guild.id),
)
conn.commit()
con_data.commit()
await ctx.respond(
"The advanced settings have been set to their default values",
ephemeral=True,
@@ -194,16 +194,16 @@ class Settings(discord.Cog):
# this command sends all the data about the guild, including the api key, the channel id, the advanced settings and the uses_count_today
# check if the guild is in the database
try:
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = curs_data.fetchone()
except:
data = None
if data[2] is None:
await ctx.respond("This server is not setup", ephemeral=True)
return
try:
c.execute("SELECT * FROM model WHERE guild_id = ?", (ctx.guild.id,))
model = c.fetchone()[1]
curs_data.execute("SELECT * FROM model WHERE guild_id = ?", (ctx.guild.id,))
model = curs_data.fetchone()[1]
except:
model = None
if model is None:
@@ -233,8 +233,8 @@ class Settings(discord.Cog):
f"The user {ctx.author.name} ran the prefix command command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
try:
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = curs_data.fetchone()
api_key = data[2]
except:
await ctx.respond("This server is not setup", ephemeral=True)
@@ -251,11 +251,11 @@ class Settings(discord.Cog):
)
return
await ctx.respond("Prefix changed !", ephemeral=True, delete_after=5)
c.execute(
curs_data.execute(
"UPDATE data SET prompt_prefix = ? WHERE guild_id = ?",
(prefix, ctx.guild.id),
)
conn.commit()
con_data.commit()
# when someone mentions the bot, check if the guild is in the database and if the bot is enabled. If it is, send a message answering the mention
@discord.slash_command(
@@ -272,8 +272,8 @@ class Settings(discord.Cog):
)
# check if the guild is in the database
try:
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = curs_data.fetchone()
api_key = data[2]
except:
await ctx.respond("This server is not setup", ephemeral=True)
@@ -291,28 +291,28 @@ class Settings(discord.Cog):
return
if pretend_to_be == "":
pretend_to_be = ""
c.execute(
curs_data.execute(
"UPDATE data SET pretend_enabled = 0 WHERE guild_id = ?",
(ctx.guild.id,),
)
conn.commit()
con_data.commit()
await ctx.respond("Pretend mode disabled", ephemeral=True, delete_after=5)
await ctx.guild.me.edit(nick=None)
return
else:
c.execute(
curs_data.execute(
"UPDATE data SET pretend_enabled = 1 WHERE guild_id = ?",
(ctx.guild.id,),
)
conn.commit()
con_data.commit()
await ctx.respond("Pretend mode enabled", ephemeral=True, delete_after=5)
# change the bots name on the server wit ctx.guild.me.edit(nick=pretend_to_be)
await ctx.guild.me.edit(nick=pretend_to_be)
c.execute(
curs_data.execute(
"UPDATE data SET pretend_to_be = ? WHERE guild_id = ?",
(pretend_to_be, ctx.guild.id),
)
conn.commit()
con_data.commit()
# if the usename is longer than 32 characters, shorten it
if len(pretend_to_be) > 31:
pretend_to_be = pretend_to_be[:32]
@@ -325,8 +325,8 @@ class Settings(discord.Cog):
guild_id = ctx.guild.id
# connect to the database
# update the tts value in the database
c.execute("UPDATE data SET tts = 1 WHERE guild_id = ?", (guild_id,))
conn.commit()
curs_data.execute("UPDATE data SET tts = 1 WHERE guild_id = ?", (guild_id,))
con_data.commit()
# send a message
await ctx.respond("TTS has been enabled", ephemeral=True)
@@ -336,8 +336,8 @@ class Settings(discord.Cog):
guild_id = ctx.guild.id
# connect to the database
# update the tts value in the database
c.execute("UPDATE data SET tts = 0 WHERE guild_id = ?", (guild_id,))
conn.commit()
curs_data.execute("UPDATE data SET tts = 0 WHERE guild_id = ?", (guild_id,))
con_data.commit()
# send a message
await ctx.respond("TTS has been disabled", ephemeral=True)
@@ -355,18 +355,18 @@ class Settings(discord.Cog):
@default_permissions(administrator=True)
async def model(self, ctx: discord.ApplicationContext, model: str = "davinci"):
try:
c.execute("SELECT * FROM model WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()[1]
curs_data.execute("SELECT * FROM model WHERE guild_id = ?", (ctx.guild.id,))
data = curs_data.fetchone()[1]
except:
data = None
if data is None:
c.execute("INSERT INTO model VALUES (?, ?)", (ctx.guild.id, model))
curs_data.execute("INSERT INTO model VALUES (?, ?)", (ctx.guild.id, model))
else:
c.execute(
curs_data.execute(
"UPDATE model SET model_name = ? WHERE guild_id = ?",
(model, ctx.guild.id),
)
conn.commit()
con_data.commit()
await ctx.respond("Model changed !", ephemeral=True)
async def images_recognition_autocomplete(ctx: discord.AutocompleteContext):
@@ -383,8 +383,8 @@ class Settings(discord.Cog):
@default_permissions(administrator=True)
async def images(self, ctx: discord.ApplicationContext, enable_disable: str):
try:
c.execute("SELECT * FROM images WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()
curs_data.execute("SELECT * FROM images WHERE guild_id = ?", (ctx.guild.id,))
data = curs_data.fetchone()
except:
data = None
if enable_disable == "enable":
@@ -392,15 +392,15 @@ class Settings(discord.Cog):
elif enable_disable == "disable":
enable_disable = 0
if data is None:
c.execute(
curs_data.execute(
"INSERT INTO images VALUES (?, ?, ?)", (ctx.guild.id, 0, enable_disable)
)
else:
c.execute(
curs_data.execute(
"UPDATE images SET is_enabled = ? WHERE guild_id = ?",
(enable_disable, ctx.guild.id),
)
conn.commit()
con_data.commit()
await ctx.respond(
"Images recognition has been "
+ ("enabled" if enable_disable == 1 else "disabled"),

View File

@@ -1,5 +1,5 @@
import discord
from config import debug, conn, c, connp, cp
from config import debug, con_data, curs_data, con_premium, curs_premium
class Setup(discord.Cog):
@@ -26,26 +26,26 @@ class Setup(discord.Cog):
return
# check if the guild is already in the database bi checking if there is a key for the guild
try:
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = curs_data.fetchone()
if data[3] == None:
data = None
except:
data = None
if data != None:
c.execute(
curs_data.execute(
"UPDATE data SET channel_id = ?, api_key = ? WHERE guild_id = ?",
(channel.id, api_key, ctx.guild.id),
)
# c.execute("UPDATE data SET is_active = ?, max_tokens = ?, temperature = ?, frequency_penalty = ?, presence_penalty = ?, prompt_size = ? WHERE guild_id = ?", (False, 64, 0.9, 0.0, 0.0, 5, ctx.guild.id))
conn.commit()
con_data.commit()
await ctx.respond(
"The channel id and the api key have been updated", ephemeral=True
)
else:
# in this case, the guild is not in the database, so we add it
c.execute(
curs_data.execute(
"INSERT INTO data VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
ctx.guild.id,
@@ -64,7 +64,7 @@ class Setup(discord.Cog):
False,
),
)
conn.commit()
con_data.commit()
await ctx.respond(
"The channel id and the api key have been added", ephemeral=True
)
@@ -78,16 +78,16 @@ class Setup(discord.Cog):
f"The user {ctx.author} ran the delete command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
# check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if c.fetchone() is None:
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if curs_data.fetchone() is None:
await ctx.respond("This server is not setup", ephemeral=True)
return
# delete the guild from the database, except the guild id and the uses_count_today
c.execute(
curs_data.execute(
"UPDATE data SET api_key = ?, channel_id = ?, is_active = ?, max_tokens = ?, temperature = ?, frequency_penalty = ?, presence_penalty = ?, prompt_size = ? WHERE guild_id = ?",
(None, None, False, 50, 0.9, 0.0, 0.0, 0, ctx.guild.id),
)
conn.commit()
con_data.commit()
await ctx.respond("Deleted", ephemeral=True)
# create a command called "enable" that only admins can use
@@ -104,15 +104,15 @@ class Setup(discord.Cog):
debug(
f"The user {ctx.author} ran the enable command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if c.fetchone() is None:
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if curs_data.fetchone() is None:
await ctx.respond("This server is not setup", ephemeral=True)
return
# enable the guild
c.execute(
curs_data.execute(
"UPDATE data SET is_active = ? WHERE guild_id = ?", (True, ctx.guild.id)
)
conn.commit()
con_data.commit()
await ctx.respond("Enabled", ephemeral=True)
# create a command called "disable" that only admins can use
@@ -123,15 +123,15 @@ class Setup(discord.Cog):
f"The user {ctx.author} ran the disable command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
# check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if c.fetchone() is None:
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if curs_data.fetchone() is None:
await ctx.respond("This server is not setup", ephemeral=True)
return
# disable the guild
c.execute(
curs_data.execute(
"UPDATE data SET is_active = ? WHERE guild_id = ?", (False, ctx.guild.id)
)
conn.commit()
con_data.commit()
await ctx.respond("Disabled", ephemeral=True)
# create a command calles "add channel" that can only be used in premium servers
@@ -152,8 +152,8 @@ class Setup(discord.Cog):
f"The user {ctx.author} ran the add_channel command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
# check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if c.fetchone() is None:
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if curs_data.fetchone() is None:
await ctx.respond("This server is not setup", ephemeral=True)
return
# check if the guild is premium
@@ -168,8 +168,8 @@ class Setup(discord.Cog):
if channel is None:
channel = ctx.channel
# check if the channel is already in the list
c.execute("SELECT channel_id FROM data WHERE guild_id = ?", (ctx.guild.id,))
if str(channel.id) == c.fetchone()[0]:
curs_data.execute("SELECT channel_id FROM data WHERE guild_id = ?", (ctx.guild.id,))
if str(channel.id) == curs_data.fetchone()[0]:
await ctx.respond(
"This channel is already set as the main channel", ephemeral=True
)
@@ -182,7 +182,7 @@ class Setup(discord.Cog):
"INSERT INTO channels VALUES (?, ?, ?, ?, ?, ?)",
(ctx.guild.id, channel.id, None, None, None, None),
)
connp.commit()
con_premium.commit()
await ctx.respond(f"Added channel **{channel.name}**", ephemeral=True)
return
channels = guild_channels[1:]
@@ -195,7 +195,7 @@ class Setup(discord.Cog):
f"UPDATE channels SET channel{i} = ? WHERE guild_id = ?",
(channel.id, ctx.guild.id),
)
connp.commit()
con_premium.commit()
await ctx.respond(f"Added channel **{channel.name}**", ephemeral=True)
return
await ctx.respond("You can only add 5 channels", ephemeral=True)
@@ -218,8 +218,8 @@ class Setup(discord.Cog):
f"The user {ctx.author} ran the remove_channel command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}"
)
# check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if c.fetchone() is None:
curs_data.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
if curs_data.fetchone() is None:
await ctx.respond("This server is not setup", ephemeral=True)
return
# check if the guild is premium
@@ -236,8 +236,8 @@ class Setup(discord.Cog):
# check if the channel is in the list
cp.execute("SELECT * FROM channels WHERE guild_id = ?", (ctx.guild.id,))
guild_channels = cp.fetchone()
c.execute("SELECT channel_id FROM data WHERE guild_id = ?", (ctx.guild.id,))
if str(channel.id) == c.fetchone()[0]:
curs_data.execute("SELECT channel_id FROM data WHERE guild_id = ?", (ctx.guild.id,))
if str(channel.id) == curs_data.fetchone()[0]:
await ctx.respond(
"This channel is set as the main channel and therefore cannot be removed. Type /setup to change the main channel.",
ephemeral=True,
@@ -261,6 +261,6 @@ class Setup(discord.Cog):
f"UPDATE channels SET channel{i} = ? WHERE guild_id = ?",
(None, ctx.guild.id),
)
connp.commit()
con_premium.commit()
await ctx.respond(f"Removed channel **{channel.name}**", ephemeral=True)
return