Use mysql or mariadb database for config save

This commit is contained in:
louis chauvet 2018-09-11 08:55:26 +02:00
parent ac85c4857b
commit 203be35c7d
5 changed files with 201 additions and 92 deletions

View File

@ -6,6 +6,8 @@ name = "pypi"
[packages]
"fs.dropboxfs" = {git = "https://github.com/rkhwaja/fs.dropboxfs.git"}
"discord.py" = {ref = "rewrite", git = "https://github.com/Rapptz/discord.py"}
mysql-connector-python = "*"
pymysql = "*"
[dev-packages]

141
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "0b00547c8080ca98592603203321ca0aa4c3be1d1d1d0d69ac0fb28b78619350"
"sha256": "0a45806745c14c2eb4a5190e94d4093508347d44f685b6b7262259c46b5f7cb5"
},
"pipfile-spec": 6,
"requires": {
@ -16,13 +16,150 @@
]
},
"default": {
"asn1crypto": {
"hashes": [
"sha256:2f1adbb7546ed199e3c90ef23ec95c5cf3585bac7d11fb7eb562a3fe89c64e87",
"sha256:9d5c20441baf0cb60a4ac34cc447c6c189024b6b4c6cd7877034f4965c464e49"
],
"version": "==0.24.0"
},
"cffi": {
"hashes": [
"sha256:151b7eefd035c56b2b2e1eb9963c90c6302dc15fbd8c1c0a83a163ff2c7d7743",
"sha256:1553d1e99f035ace1c0544050622b7bc963374a00c467edafac50ad7bd276aef",
"sha256:1b0493c091a1898f1136e3f4f991a784437fac3673780ff9de3bcf46c80b6b50",
"sha256:2ba8a45822b7aee805ab49abfe7eec16b90587f7f26df20c71dd89e45a97076f",
"sha256:3bb6bd7266598f318063e584378b8e27c67de998a43362e8fce664c54ee52d30",
"sha256:3c85641778460581c42924384f5e68076d724ceac0f267d66c757f7535069c93",
"sha256:3eb6434197633b7748cea30bf0ba9f66727cdce45117a712b29a443943733257",
"sha256:495c5c2d43bf6cebe0178eb3e88f9c4aa48d8934aa6e3cddb865c058da76756b",
"sha256:4c91af6e967c2015729d3e69c2e51d92f9898c330d6a851bf8f121236f3defd3",
"sha256:57b2533356cb2d8fac1555815929f7f5f14d68ac77b085d2326b571310f34f6e",
"sha256:770f3782b31f50b68627e22f91cb182c48c47c02eb405fd689472aa7b7aa16dc",
"sha256:79f9b6f7c46ae1f8ded75f68cf8ad50e5729ed4d590c74840471fc2823457d04",
"sha256:7a33145e04d44ce95bcd71e522b478d282ad0eafaf34fe1ec5bbd73e662f22b6",
"sha256:857959354ae3a6fa3da6651b966d13b0a8bed6bbc87a0de7b38a549db1d2a359",
"sha256:87f37fe5130574ff76c17cab61e7d2538a16f843bb7bca8ebbc4b12de3078596",
"sha256:95d5251e4b5ca00061f9d9f3d6fe537247e145a8524ae9fd30a2f8fbce993b5b",
"sha256:9d1d3e63a4afdc29bd76ce6aa9d58c771cd1599fbba8cf5057e7860b203710dd",
"sha256:a36c5c154f9d42ec176e6e620cb0dd275744aa1d804786a71ac37dc3661a5e95",
"sha256:a6a5cb8809091ec9ac03edde9304b3ad82ad4466333432b16d78ef40e0cce0d5",
"sha256:ae5e35a2c189d397b91034642cb0eab0e346f776ec2eb44a49a459e6615d6e2e",
"sha256:b0f7d4a3df8f06cf49f9f121bead236e328074de6449866515cea4907bbc63d6",
"sha256:b75110fb114fa366b29a027d0c9be3709579602ae111ff61674d28c93606acca",
"sha256:ba5e697569f84b13640c9e193170e89c13c6244c24400fc57e88724ef610cd31",
"sha256:be2a9b390f77fd7676d80bc3cdc4f8edb940d8c198ed2d8c0be1319018c778e1",
"sha256:ca1bd81f40adc59011f58159e4aa6445fc585a32bb8ac9badf7a2c1aa23822f2",
"sha256:d5d8555d9bfc3f02385c1c37e9f998e2011f0db4f90e250e5bc0c0a85a813085",
"sha256:e55e22ac0a30023426564b1059b035973ec82186ddddbac867078435801c7801",
"sha256:e90f17980e6ab0f3c2f3730e56d1fe9bcba1891eeea58966e89d352492cc74f4",
"sha256:ecbb7b01409e9b782df5ded849c178a0aa7c906cf8c5a67368047daab282b184",
"sha256:ed01918d545a38998bfa5902c7c00e0fee90e957ce036a4000a88e3fe2264917",
"sha256:edabd457cd23a02965166026fd9bfd196f4324fe6032e866d0f3bd0301cd486f",
"sha256:fdf1c1dc5bafc32bc5d08b054f94d659422b05aba244d6be4ddc1c72d9aa70fb"
],
"version": "==1.11.5"
},
"cryptography": {
"hashes": [
"sha256:02602e1672b62e803e08617ec286041cc453e8d43f093a5f4162095506bc0beb",
"sha256:10b48e848e1edb93c1d3b797c83c72b4c387ab0eb4330aaa26da8049a6cbede0",
"sha256:17db09db9d7c5de130023657be42689d1a5f60502a14f6f745f6f65a6b8195c0",
"sha256:227da3a896df1106b1a69b1e319dce218fa04395e8cc78be7e31ca94c21254bc",
"sha256:2cbaa03ac677db6c821dac3f4cdfd1461a32d0615847eedbb0df54bb7802e1f7",
"sha256:31db8febfc768e4b4bd826750a70c79c99ea423f4697d1dab764eb9f9f849519",
"sha256:4a510d268e55e2e067715d728e4ca6cd26a8e9f1f3d174faf88e6f2cb6b6c395",
"sha256:6a88d9004310a198c474d8a822ee96a6dd6c01efe66facdf17cb692512ae5bc0",
"sha256:76936ec70a9b72eb8c58314c38c55a0336a2b36de0c7ee8fb874a4547cadbd39",
"sha256:7e3b4aecc4040928efa8a7cdaf074e868af32c58ffc9bb77e7bf2c1a16783286",
"sha256:8168bcb08403ef144ff1fb880d416f49e2728101d02aaadfe9645883222c0aa5",
"sha256:8229ceb79a1792823d87779959184a1bf95768e9248c93ae9f97c7a2f60376a1",
"sha256:8a19e9f2fe69f6a44a5c156968d9fc8df56d09798d0c6a34ccc373bb186cee86",
"sha256:8d10113ca826a4c29d5b85b2c4e045ffa8bad74fb525ee0eceb1d38d4c70dfd6",
"sha256:be495b8ec5a939a7605274b6e59fbc35e76f5ad814ae010eb679529671c9e119",
"sha256:dc2d3f3b1548f4d11786616cf0f4415e25b0fbecb8a1d2cd8c07568f13fdde38",
"sha256:e4aecdd9d5a3d06c337894c9a6e2961898d3f64fe54ca920a72234a3de0f9cb3",
"sha256:e79ab4485b99eacb2166f3212218dd858258f374855e1568f728462b0e6ee0d9",
"sha256:f995d3667301e1754c57b04e0bae6f0fa9d710697a9f8d6712e8cca02550910f"
],
"version": "==2.3.1"
},
"discord.py": {
"git": "https://github.com/Rapptz/discord.py",
"ref": "00a659c6526b2445162b52eaf970adbd22c6d35d"
},
"fs.dropboxfs": {
"git": "https://github.com/rkhwaja/fs.dropboxfs.git",
"ref": "68988a25143a8ea760bc6dfb2a715f6f98ac31c7"
"ref": "47268ea33d6cfa91e343cb84d6d7f6d8b44db3f8"
},
"idna": {
"hashes": [
"sha256:156a6814fb5ac1fc6850fb002e0852d56c0c8d2531923a51032d1b70760e186e",
"sha256:684a38a6f903c1d71d6d5fac066b58d7768af4de2b832e426ec79c30daa94a16"
],
"version": "==2.7"
},
"mysql-connector-python": {
"hashes": [
"sha256:35a8f77b90d40cbf5bbb87bcfae02d63ca0383833187142ead963b1ad95ee958",
"sha256:364b97e225ddaab4438850e9c16495dba4c4ab0a995e7ad061bfb66c07489031",
"sha256:605d55f280b3496cf3b12c7c3751e59e69f80cd4515b39c11ee5f79dae13e407",
"sha256:6e90c766b9f144db8af3cac243e3f7b0e5f151ead6af017285d62f841e906b92",
"sha256:874a7443829183351b6f28a6a22796a738278da7d6a2cfa1cffb5bc0ceea04dd",
"sha256:95426961f4caa90960bf71776a1825e7facd9d3754511313d5c72401793addd6",
"sha256:9dee3c249f169363330a9feeb5bf60392778dbcf81f57c5265b8e75e7a4e45d5",
"sha256:a6f045807fb325369ff9520570a007952696889b6f97058b5ec820fb1462ba71",
"sha256:ce817f306eb0db50f424dc8a3f00b57a9bb78c02dcc5beb7014810e3d19356ca",
"sha256:d4ecde5a335697e240062f72ff4bcba1d51d04a8c73ea3ec69c46cde13e13082",
"sha256:e0fcb069f2916994599f2648e4b9d433087ebe8dacf13cf4d0dea4c0149b9723",
"sha256:eab88976e0cfe47a17ab625ea961ca53478788215cd000f31e22c1f3469685b4",
"sha256:f59ed1ff1b7ffb8343fecb02412628245ec802dd7ecce06f6e9b6863170bfbd1",
"sha256:f5f50d9a9e1690f541cb8c86be68dc62d5e5203ca85d8696807ef2df5e1c7fb1",
"sha256:fe895a5db555a5a5f90a195119e042e2275029012a0ca2a1a085d49b7bb954c9"
],
"index": "pypi",
"version": "==8.0.12"
},
"protobuf": {
"hashes": [
"sha256:10394a4d03af7060fa8a6e1cbf38cea44be1467053b0aea5bbfcb4b13c4b88c4",
"sha256:1489b376b0f364bcc6f89519718c057eb191d7ad6f1b395ffd93d1aa45587811",
"sha256:1931d8efce896981fe410c802fd66df14f9f429c32a72dd9cfeeac9815ec6444",
"sha256:196d3a80f93c537f27d2a19a4fafb826fb4c331b0b99110f985119391d170f96",
"sha256:46e34fdcc2b1f2620172d3a4885128705a4e658b9b62355ae5e98f9ea19f42c2",
"sha256:59cd75ded98094d3cf2d79e84cdb38a46e33e7441b2826f3838dcc7c07f82995",
"sha256:5ee0522eed6680bb5bac5b6d738f7b0923b3cafce8c4b1a039a6107f0841d7ed",
"sha256:65917cfd5da9dfc993d5684643063318a2e875f798047911a9dd71ca066641c9",
"sha256:685bc4ec61a50f7360c9fd18e277b65db90105adbf9c79938bd315435e526b90",
"sha256:92e8418976e52201364a3174e40dc31f5fd8c147186d72380cbda54e0464ee19",
"sha256:9335f79d1940dfb9bcaf8ec881fb8ab47d7a2c721fb8b02949aab8bbf8b68625",
"sha256:a7ee3bb6de78185e5411487bef8bc1c59ebd97e47713cba3c460ef44e99b3db9",
"sha256:ceec283da2323e2431c49de58f80e1718986b79be59c266bb0509cbf90ca5b9e",
"sha256:fcfc907746ec22716f05ea96b7f41597dfe1a1c088f861efb8a0d4f4196a6f10"
],
"markers": "python_version != '3.3.*' and python_version != '3.1.*' and python_version != '3.0.*' and python_version != '3.2.*' and python_version >= '2.7'",
"version": "==3.6.1"
},
"pycparser": {
"hashes": [
"sha256:99a8ca03e29851d96616ad0404b4aad7d9ee16f25c9f9708a11faf2810f7b226"
],
"version": "==2.18"
},
"pymysql": {
"hashes": [
"sha256:95f057328357e0e13a30e67857a8c694878b0175797a9a203ee7adbfb9b1ec5f",
"sha256:9ec760cbb251c158c19d6c88c17ca00a8632bac713890e465b2be01fdc30713f"
],
"index": "pypi",
"version": "==0.9.2"
},
"six": {
"hashes": [
"sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",
"sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb"
],
"version": "==1.11.0"
}
},
"develop": {}

136
main.py
View File

@ -1,24 +1,27 @@
import ftplib
import importlib
import json
import logging
import logging.config
import os
import sys
import re
import discord
from fs.dropboxfs import DropboxFS
from fs.osfs import OSFS
from fs import path
fileSystem = None
import pymysql as mariadb
if os.environ.get("DROPBOX_ACCESS_TOKEN", False):
print("FTP")
fileSystem = DropboxFS(os.environ["DROPBOX_ACCESS_TOKEN"])
else:
print("OS")
fileSystem = OSFS(os.getcwd())
import os
# Setup database
db_connection = mariadb.connect(host='127.0.0.1',
port=3307,
user=os.environ['FOBOT_DATABASE_USER'],
password=os.environ['FOBOT_DATABASE_PASSWORD'],
db='fobot',
charset='utf8mb4',
cursorclass=mariadb.cursors.DictCursor)
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")
# json decoder for int keys
@ -69,10 +72,9 @@ critical = log_foBot.critical
class Guild:
def __init__(self, bot, guild_id, config_file):
def __init__(self, bot, guild_id):
self.id = guild_id
self.bot = bot
self.config_file = config_file
self.config = {"modules": ["modules"],
"prefix": "§",
"master_admins": [318866596502306816],
@ -84,22 +86,39 @@ class Guild:
self.save_config()
def load_config(self):
if self.bot.fileSystem.exists(self.config_file):
try:
# Loading configuration file
with self.bot.fileSystem.open(self.config_file) as conf:
self.config.update(json.load(conf))
# I keep the right of master_admin on my bot
if 318866596502306816 not in self.config["master_admins"]:
self.config["master_admins"].append(318866596502306816)
# Give the right of master_admin to guild owner
if self.bot.get_guild(self.id) is not None:
if self.bot.get_guild(self.id).owner.id not in self.config["master_admins"]:
self.config["master_admins"].append(self.bot.get_guild(self.id).owner.id)
with self.bot.database.cursor() as cursor:
# Create guild table if it not exists
sql_create = """CREATE TABLE IF NOT EXISTS {guild_id} (
id int(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(50) NOT NULL,
content JSON CHECK (JSON_VALID(content))
);""".format(guild_id=to_str(self.id))
cursor.execute(sql_create)
# Load config row
sql_content = """SELECT id,name,content FROM {guild_id} WHERE name='config';""".format(
guild_id=to_str(self.id))
cursor.execute(sql_content)
result = cursor.fetchone()
if result is None:
sql_insert = """INSERT INTO {guild_id} (name) VALUES ('config');""".format(guild_id=to_str(self.id))
cursor.execute(sql_insert)
self.save_config()
# Refetch config
sql_content = """SELECT id,name,content FROM {guild_id} WHERE name='config';""".format(
guild_id=to_str(self.id))
cursor.execute(sql_content)
result = cursor.fetchone()
except PermissionError:
error("Cannot open config file for server %s." % self.id)
self.config = json.loads(result['content'])
self.bot.database.commit()
def save_config(self):
with self.bot.database.cursor() as cursor:
sql = r"""UPDATE {guild_id} SET content='{configjson}' WHERE name='config';""".format(
guild_id=to_str(self.id),
configjson=re.escape(json.dumps(self.config)))
cursor.execute(sql)
self.bot.database.commit()
def update_modules(self):
self.modules = []
@ -126,17 +145,11 @@ class Guild:
self.modules.append(self.bot.modules[module](guild=self))
return errors
def save_config(self):
try:
with self.bot.fileSystem.open(self.config_file, 'w') as conf_file:
json.dump(self.config, conf_file)
except PermissionError:
error("Cannot write to configuration file.")
async def on_message(self, msg):
if not msg.author.bot:
for module in self.modules:
await module.on_message(msg)
print(msg.content)
return
@ -149,7 +162,7 @@ class FoBot(discord.Client):
self.guilds_class = {}
self.modules = {}
self.load_modules()
self.fileSystem = fileSystem
self.database = db_connection
def load_modules(self):
for module in os.listdir('modules'):
@ -158,46 +171,11 @@ class FoBot(discord.Client):
self.modules.update({module[:-3]: imported.MainClass})
def load_config(self):
if self.fileSystem.exists(path.join(self.config_folder, "conf.json")):
try:
# Loading configuration file
with self.fileSystem.open(path.join(self.config_folder, "conf.json")) as conf:
self.config.update(json.load(conf))
except PermissionError:
critical("Cannot open config file.")
sys.exit()
info("Configuration for foBot loaded. Check for new guilds.")
# Change all str key of guild into int ones
guilds = {int(k): v for k, v in self.config["guilds"].items()}
del self.config["guilds"]
self.config.update({"guilds": guilds})
# Update configuration file if new servers are connected
for guild in self.guilds:
if guild.id not in list(self.config["guilds"].keys()):
self.config["guilds"].update(
{guild.id: path.join(self.config_folder, str(guild.id) + ".json")})
for guild_id, guild_config_file in self.config["guilds"].items():
self.guilds_class.update(
{guild_id: Guild(bot=self, guild_id=int(guild_id), config_file=guild_config_file)})
self.save_config()
elif self.fileSystem.exists(self.config_folder):
self.save_config()
else:
try:
self.fileSystem.makedir(self.config_folder)
except PermissionError:
critical("Cannot create config folder.")
sys.exit()
self.guilds_class.update({guild.id: Guild(self, guild.id)})
def save_config(self):
for guild in self.guilds_class.values():
guild.save_config()
try:
with self.fileSystem.open(path.join(self.config_folder, "conf.json"), 'w') as conf_file:
json.dump(self.config, conf_file, indent=4)
except PermissionError:
critical("Cannot write to configuration file.")
sys.exit()
pass
async def on_connect(self):
info("foBot is connected.")
@ -219,16 +197,10 @@ class FoBot(discord.Client):
async def on_error(self, event, *args, **kwargs):
error("foBot encounter an error.", exc_info=True)
if os.environ.get("DROPBOX_ACCESS_TOKEN", False):
print("FTP")
self.fileSystem = DropboxFS(os.environ["DROPBOX_ACCESS_TOKEN"])
else:
print("OS")
self.fileSystem = OSFS(os.getcwd())
async def on_message(self, msg):
await self.guilds_class[msg.guild.id].on_message(msg)
myBot = FoBot()
myBot.run(os.environ.get("DISCORD_TOKEN", ""), max_messages=100000000)
myBot.run("NDcwNzI4NjAzMDEzNzQyNjAy.DnjwKA.2yInMZQjvKopi6Q5sqSCN216MrE", max_messages=100000000)

View File

@ -11,12 +11,9 @@ class MainClass:
def __init__(self, guild):
self.guild = guild
self.pi_file = "/pi/pi1.txt"
self.pi_file = "modules/pi/pi1.txt"
self.piFileError = False
try:
self.guild.bot.fileSystem.open(self.pi_file).close()
except fs.errors.ResourceNotFound:
self.piFileError = True
async def pi(self, msg, command, args):
if not self.piFileError:
@ -30,7 +27,7 @@ class MainClass:
if start > 1000000-2000:
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["TooBigNumberPiError"])
return
with self.guild.bot.fileSystem.open(self.pi_file) as pi_file:
with open(self.pi_file) as pi_file:
pi_file.read(start)
txt = pi_file.read(2000)
await msg.channel.send(tr.tr[self.guild.config["lang"]]["modules"]["pi"]["pi"].format(debut=start))
@ -55,7 +52,7 @@ class MainClass:
except re.error:
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["RegexError"])
return
with self.guild.bot.fileSystem.open(self.pi_file) as pi_file:
with open(self.pi_file) as pi_file:
pi = pi_file.readline()
results = to_search.finditer(pi)
texts = []

1
modules/pi/pi1.txt Normal file

File diff suppressed because one or more lines are too long