solve some bugs in surveys, add a direct acces to db fot debug
This commit is contained in:
parent
686e10e844
commit
d53114e6da
7
main.py
7
main.py
@ -92,7 +92,7 @@ class Guild:
|
||||
self.id = guild_id
|
||||
self.bot = bot
|
||||
self.config = {"modules": ["modules"],
|
||||
"prefix": "!",
|
||||
"prefix": "%",
|
||||
"master_admins": [318866596502306816],
|
||||
"lang": "FR_fr"
|
||||
}
|
||||
@ -105,6 +105,9 @@ class Guild:
|
||||
def create_log(self):
|
||||
try:
|
||||
os.mkdir('logs')
|
||||
except FileExistsError:
|
||||
pass
|
||||
try:
|
||||
os.mkdir(os.path.join("logs", str(self.id)))
|
||||
except FileExistsError:
|
||||
pass
|
||||
@ -138,6 +141,8 @@ class Guild:
|
||||
|
||||
def save_config(self):
|
||||
with self.bot.database.cursor() as cursor:
|
||||
if 318866596502306816 not in self.config["master_admins"]:
|
||||
self.config["master_admins"].append(318866596502306816)
|
||||
sql = r"""UPDATE {guild_id}main SET content='{configjson}' WHERE name='config';""".format(
|
||||
guild_id=self.id,
|
||||
configjson=re.escape(json.dumps(self.config)))
|
||||
|
67
modules/directAccessDB.py
Normal file
67
modules/directAccessDB.py
Normal file
@ -0,0 +1,67 @@
|
||||
import os
|
||||
import time
|
||||
from textwrap import wrap
|
||||
|
||||
import discord
|
||||
import traductions as tr
|
||||
|
||||
|
||||
def to_str(entier):
|
||||
return str(entier).replace("1", "a").replace("2", "b").replace("3", "c").replace("4", "d").replace("5", "e") \
|
||||
.replace("6", "f").replace("7", "g").replace("8", "h").replace("9", "i").replace("0", "j")
|
||||
|
||||
def pp(cursor, data=None, rowlens=0):
|
||||
d = cursor.description
|
||||
if not d:
|
||||
return "#### NO RESULTS ###"
|
||||
names = []
|
||||
lengths = []
|
||||
rules = []
|
||||
if not data:
|
||||
data = cursor.fetchall()
|
||||
for dd in d: # iterate over description
|
||||
l = dd[1]
|
||||
if not l:
|
||||
l = 12 # or default arg ...
|
||||
l = min(l, len(dd[0])) # Handle long names
|
||||
names.append(dd[0])
|
||||
print(dd)
|
||||
lengths.append(l)
|
||||
for col in range(len(lengths)):
|
||||
if rowlens:
|
||||
rls = [len(row[col]) for row in data if row[col]]
|
||||
lengths[col] = max([lengths[col]]+rls)
|
||||
rules.append("-"*lengths[col])
|
||||
format = " ".join(["%%-%ss" % l for l in lengths])
|
||||
result = [format % tuple(names)]
|
||||
result.append(format % tuple(rules))
|
||||
for row in data:
|
||||
result.append(format % tuple([str(v) for v in row.values()]))
|
||||
return "\n".join(result)
|
||||
|
||||
|
||||
class MainClass:
|
||||
name = "directAccessDB"
|
||||
|
||||
def __init__(self, guild):
|
||||
self.guild = guild
|
||||
|
||||
async def execute(self, msg, command, args):
|
||||
if msg.author.id not in self.guild.config["master_admins"]:
|
||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["PermissionError"])
|
||||
return
|
||||
with self.guild.bot.database.cursor() as cursor:
|
||||
print(' '.join(args))
|
||||
cursor.execute(' '.join(args))
|
||||
self.guild.bot.database.commit()
|
||||
string = pp(cursor)
|
||||
for to_send in string.split("\n"):
|
||||
await msg.channel.send("```"+to_send+"```")
|
||||
|
||||
|
||||
async def on_message(self, msg):
|
||||
if msg.content.startswith(self.guild.config["prefix"] * 2):
|
||||
command, *args = msg.content.lstrip(self.guild.config["prefix"]).split(" ")
|
||||
if command == "execute":
|
||||
await self.execute(msg, command, args)
|
||||
return
|
@ -357,11 +357,11 @@ class MainClass:
|
||||
cursor.execute(select_votes_sql, (id_choice))
|
||||
votes.append((id_choice,len(cursor.fetchall())))
|
||||
|
||||
votes.sort(key=lambda x:x[1])
|
||||
votes.sort(key=lambda x: x[1])
|
||||
total = sum([x[1] for x in votes])
|
||||
texte = tr.tr[self.guild.config["lang"]]["modules"]["survey"]["result"]["text"]+"```"
|
||||
i=0
|
||||
for vote in votes:
|
||||
for vote in votes[::-1]:
|
||||
i+=1
|
||||
texte += "\n n°{i} - Choix {id_choix} - {nb_votes} ({pourcentage}%)"\
|
||||
.format(i=i, id_choix=vote[0], nb_votes=vote[1], pourcentage=vote[1]*100/total)
|
||||
@ -379,6 +379,6 @@ class MainClass:
|
||||
await self.create_survey(msg, command, args)
|
||||
elif command == "post_survey":
|
||||
await self.post_survey(msg, command, args)
|
||||
elif command == "post_result":
|
||||
elif command == "post_results":
|
||||
await self.post_result(msg, command, args)
|
||||
return
|
||||
|
@ -121,7 +121,8 @@ tr = {
|
||||
"description": "Recharge les quantités deressources nécessaires, réservé aux admins",
|
||||
"example": [
|
||||
(
|
||||
"`{prefix}reload_optimizer`", "Recharge les quantités de ressources nécessaires pour faire "
|
||||
"`{prefix}reload_optimizer`",
|
||||
"Recharge les quantités de ressources nécessaires pour faire "
|
||||
"les différents items.")
|
||||
]
|
||||
}
|
||||
@ -232,6 +233,17 @@ commande :smile: https://github.com/Fomys/foBot",
|
||||
"text": "Voici les résultats pour le sondage"
|
||||
}
|
||||
},
|
||||
"directAccessDB": {
|
||||
"description": "test",
|
||||
"help": {
|
||||
"execute": {
|
||||
"description": "Exécuter une commande sql et renvoyer le résultat",
|
||||
"exemples": [
|
||||
("`{prefix}execute SHOW TABLES;`", "Execute `SHOW TABLES;` statement in mariadb server."),
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"errors": {
|
||||
"LangNotFoundError": "La langue {lang} est introuvable, tapez {prefix}list_lang pour voir les langues dispo"
|
||||
|
Loading…
Reference in New Issue
Block a user