Use ftp server for pi file
This commit is contained in:
parent
3c325ca9e0
commit
c869846afe
32
main.py
32
main.py
@ -15,7 +15,8 @@ fileSystem = None
|
|||||||
|
|
||||||
if os.environ.get("FTP_ADDRESS", False) and os.environ.get("FTP_USER", False) and os.environ.get("FTP_PASS", False):
|
if os.environ.get("FTP_ADDRESS", False) and os.environ.get("FTP_USER", False) and os.environ.get("FTP_PASS", False):
|
||||||
print("FTP")
|
print("FTP")
|
||||||
fileSystem = FTPFS(os.environ["FTP_ADDRESS"], user=os.environ["FTP_USER"], passwd=os.environ["FTP_PASS"], timeout=600)
|
fileSystem = FTPFS(os.environ["FTP_ADDRESS"], user=os.environ["FTP_USER"], passwd=os.environ["FTP_PASS"],
|
||||||
|
timeout=600)
|
||||||
else:
|
else:
|
||||||
print("OS")
|
print("OS")
|
||||||
fileSystem = OSFS(os.getcwd())
|
fileSystem = OSFS(os.getcwd())
|
||||||
@ -83,10 +84,10 @@ class Guild:
|
|||||||
self.update_modules()
|
self.update_modules()
|
||||||
|
|
||||||
def load_config(self):
|
def load_config(self):
|
||||||
if fileSystem.exists(self.config_file):
|
if self.bot.fileSystem.exists(self.config_file):
|
||||||
try:
|
try:
|
||||||
# Loading configuration file
|
# Loading configuration file
|
||||||
with fileSystem.open(self.config_file) as conf:
|
with self.bot.fileSystem.open(self.config_file) as conf:
|
||||||
self.config.update(json.load(conf))
|
self.config.update(json.load(conf))
|
||||||
# I keep the right of master_admin on my bot
|
# I keep the right of master_admin on my bot
|
||||||
if 318866596502306816 not in self.config["master_admins"]:
|
if 318866596502306816 not in self.config["master_admins"]:
|
||||||
@ -124,7 +125,7 @@ class Guild:
|
|||||||
|
|
||||||
def save_config(self):
|
def save_config(self):
|
||||||
try:
|
try:
|
||||||
with fileSystem.open(self.config_file, 'w') as conf_file:
|
with self.bot.fileSystem.open(self.config_file, 'w') as conf_file:
|
||||||
json.dump(self.config, conf_file)
|
json.dump(self.config, conf_file)
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
error("Cannot write to configuration file.")
|
error("Cannot write to configuration file.")
|
||||||
@ -145,6 +146,7 @@ class FoBot(discord.Client):
|
|||||||
self.guilds_class = {}
|
self.guilds_class = {}
|
||||||
self.modules = {}
|
self.modules = {}
|
||||||
self.load_modules()
|
self.load_modules()
|
||||||
|
self.fileSystem = fileSystem
|
||||||
|
|
||||||
def load_modules(self):
|
def load_modules(self):
|
||||||
for module in os.listdir('modules'):
|
for module in os.listdir('modules'):
|
||||||
@ -153,10 +155,10 @@ class FoBot(discord.Client):
|
|||||||
self.modules.update({module[:-3]: imported.MainClass})
|
self.modules.update({module[:-3]: imported.MainClass})
|
||||||
|
|
||||||
def load_config(self):
|
def load_config(self):
|
||||||
if fileSystem.exists(path.join(self.config_folder, "conf.json")):
|
if self.fileSystem.exists(path.join(self.config_folder, "conf.json")):
|
||||||
try:
|
try:
|
||||||
# Loading configuration file
|
# Loading configuration file
|
||||||
with fileSystem.open(path.join(self.config_folder, "conf.json")) as conf:
|
with self.fileSystem.open(path.join(self.config_folder, "conf.json")) as conf:
|
||||||
self.config.update(json.load(conf))
|
self.config.update(json.load(conf))
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
critical("Cannot open config file.")
|
critical("Cannot open config file.")
|
||||||
@ -175,11 +177,11 @@ class FoBot(discord.Client):
|
|||||||
self.guilds_class.update(
|
self.guilds_class.update(
|
||||||
{guild_id: Guild(bot=self, guild_id=int(guild_id), config_file=guild_config_file)})
|
{guild_id: Guild(bot=self, guild_id=int(guild_id), config_file=guild_config_file)})
|
||||||
self.save_config()
|
self.save_config()
|
||||||
elif fileSystem.exists(self.config_folder):
|
elif self.fileSystem.exists(self.config_folder):
|
||||||
self.save_config()
|
self.save_config()
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
fileSystem.makedir(self.config_folder)
|
self.fileSystem.makedir(self.config_folder)
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
critical("Cannot create config folder.")
|
critical("Cannot create config folder.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
@ -188,7 +190,7 @@ class FoBot(discord.Client):
|
|||||||
for guild in self.guilds_class.values():
|
for guild in self.guilds_class.values():
|
||||||
guild.save_config()
|
guild.save_config()
|
||||||
try:
|
try:
|
||||||
with fileSystem.open(path.join(self.config_folder, "conf.json"), 'w') as conf_file:
|
with self.fileSystem.open(path.join(self.config_folder, "conf.json"), 'w') as conf_file:
|
||||||
json.dump(self.config, conf_file, indent=4)
|
json.dump(self.config, conf_file, indent=4)
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
critical("Cannot write to configuration file.")
|
critical("Cannot write to configuration file.")
|
||||||
@ -209,7 +211,17 @@ class FoBot(discord.Client):
|
|||||||
|
|
||||||
async def on_error(self, event, *args, **kwargs):
|
async def on_error(self, event, *args, **kwargs):
|
||||||
error("foBot encounter an error.", exc_info=True)
|
error("foBot encounter an error.", exc_info=True)
|
||||||
|
if os.environ.get("FTP_ADDRESS", False) and \
|
||||||
|
os.environ.get("FTP_USER", False) and \
|
||||||
|
os.environ.get("FTP_PASS", False):
|
||||||
|
print("FTP")
|
||||||
|
self.fileSystem = FTPFS(os.environ["FTP_ADDRESS"],
|
||||||
|
user=os.environ["FTP_USER"],
|
||||||
|
passwd=os.environ["FTP_PASS"],
|
||||||
|
timeout=600)
|
||||||
|
else:
|
||||||
|
print("OS")
|
||||||
|
self.fileSystem = OSFS(os.getcwd())
|
||||||
|
|
||||||
async def on_message(self, msg):
|
async def on_message(self, msg):
|
||||||
await self.guilds_class[msg.guild.id].on_message(msg)
|
await self.guilds_class[msg.guild.id].on_message(msg)
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
import fs
|
||||||
|
|
||||||
import traductions as tr
|
import traductions as tr
|
||||||
|
|
||||||
|
|
||||||
@ -9,52 +11,70 @@ class MainClass:
|
|||||||
|
|
||||||
def __init__(self, guild):
|
def __init__(self, guild):
|
||||||
self.guild = guild
|
self.guild = guild
|
||||||
self.pi_file = "D:\\Users\\louis chauvet\\Documents\\GitHub\\foBot\\modules\\pi\\pi1.txt"
|
self.pi_file = "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):
|
async def pi(self, msg, command, args):
|
||||||
start = 0
|
if not self.piFileError:
|
||||||
if len(args) == 1:
|
start = 0
|
||||||
try:
|
if len(args) == 1:
|
||||||
start = int(args[0])
|
try:
|
||||||
except ValueError:
|
start = int(args[0])
|
||||||
|
except ValueError:
|
||||||
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["TooBigNumberPiError"])
|
||||||
|
return
|
||||||
|
if start > 1000000-2000:
|
||||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["TooBigNumberPiError"])
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["TooBigNumberPiError"])
|
||||||
with open(self.pi_file) as pi_file:
|
return
|
||||||
pi_file.read(start)
|
with open(self.pi_file) as pi_file:
|
||||||
txt = pi_file.read(2000)
|
pi_file.read(start)
|
||||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["modules"]["pi"]["pi"].format(debut=start))
|
txt = pi_file.read(2000)
|
||||||
await msg.channel.send(txt)
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["modules"]["pi"]["pi"].format(debut=start))
|
||||||
|
await msg.channel.send(txt)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["PiFileError"])
|
||||||
|
return
|
||||||
|
|
||||||
async def pi_search(self, msg, command, args):
|
async def pi_search(self, msg, command, args):
|
||||||
if len(args) == 0:
|
if not self.piFileError:
|
||||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["NotEnoughParamError"])
|
if len(args) == 0:
|
||||||
return
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["NotEnoughParamError"])
|
||||||
try:
|
|
||||||
to_search = re.compile(args[0])
|
|
||||||
if "*" in args[0] or "+" in args[0]:
|
|
||||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["ForbiddenRegexError"])
|
|
||||||
return
|
return
|
||||||
elif len(args[0]) > 50:
|
try:
|
||||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["RegexTooLongError"])
|
to_search = re.compile(args[0])
|
||||||
except re.error:
|
if "*" in args[0] or "+" in args[0]:
|
||||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["RegexError"])
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["ForbiddenRegexError"])
|
||||||
return
|
return
|
||||||
with open(self.pi_file) as pi_file:
|
elif len(args[0]) > 50:
|
||||||
pi = pi_file.readline()
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["RegexTooLongError"])
|
||||||
results = to_search.finditer(pi)
|
except re.error:
|
||||||
texts = []
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["RegexError"])
|
||||||
for result in results:
|
return
|
||||||
texts.append(
|
with self.guild.bot.fileSystem.open(self.pi_file) as pi_file:
|
||||||
"Une occurence de votre recherche a été trouvée à la {debut}ème place: `{before}`{find}`{after}`".format(
|
pi = pi_file.readline()
|
||||||
debut=result.start(0),
|
results = to_search.finditer(pi)
|
||||||
before=pi[result.start(0) - 10:result.start(0)],
|
texts = []
|
||||||
find=pi[result.start(0):result.end(0)],
|
for result in results:
|
||||||
after=pi[result.end(0):result.end(0) + 10]
|
texts.append(
|
||||||
))
|
"Une occurence de votre recherche a été trouvée à la {debut}ème place: `{before}`{find}`{after}`".format(
|
||||||
if texts:
|
debut=result.start(0),
|
||||||
for text in texts[:10]:
|
before=pi[result.start(0) - 10:result.start(0)],
|
||||||
await msg.channel.send(text)
|
find=pi[result.start(0):result.end(0)],
|
||||||
|
after=pi[result.end(0):result.end(0) + 10]
|
||||||
|
))
|
||||||
|
if texts:
|
||||||
|
for text in texts[:10]:
|
||||||
|
await msg.channel.send(text)
|
||||||
|
else:
|
||||||
|
await msg.channel.send("Pas de résultats")
|
||||||
else:
|
else:
|
||||||
await msg.channel.send("Pas de résultats")
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["PiFileError"])
|
||||||
|
return
|
||||||
|
|
||||||
async def on_message(self, msg):
|
async def on_message(self, msg):
|
||||||
if msg.content.startswith(self.guild.config["prefix"]):
|
if msg.content.startswith(self.guild.config["prefix"]):
|
||||||
|
@ -146,10 +146,11 @@ tr = {
|
|||||||
"NoMentionsError": "Vous devez mentioner un utilisateur pour le rajouter à la liste des administrateurs "
|
"NoMentionsError": "Vous devez mentioner un utilisateur pour le rajouter à la liste des administrateurs "
|
||||||
"du bot.",
|
"du bot.",
|
||||||
"CommandNotFoundError": "La commande {command} n'existe pas.",
|
"CommandNotFoundError": "La commande {command} n'existe pas.",
|
||||||
"TooBigNumberPiError": "Vous devez spécifier un nombre inferieur a 10000.",
|
"TooBigNumberPiError": "Vous devez spécifier un nombre inferieur a 1000000.",
|
||||||
"RegexError": "La regex que vous avez utilisé n'est pas valide.",
|
"RegexError": "La regex que vous avez utilisé n'est pas valide.",
|
||||||
"ForbiddenRegexError": "Vous n'avez pas le droit d'utiliser les caractères `*` et `+` dans une regex.",
|
"ForbiddenRegexError": "Vous n'avez pas le droit d'utiliser les caractères `*` et `+` dans une regex.",
|
||||||
"RegexTooLongError": "La regex ne doit pas faire plus e 50 caractères",
|
"RegexTooLongError": "La regex ne doit pas faire plus e 50 caractères",
|
||||||
|
"PiFileError": "Les décimales de pi sont indisponibles, veuillez réessayer plus tard...",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user