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):
|
||||
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:
|
||||
print("OS")
|
||||
fileSystem = OSFS(os.getcwd())
|
||||
@ -83,10 +84,10 @@ class Guild:
|
||||
self.update_modules()
|
||||
|
||||
def load_config(self):
|
||||
if fileSystem.exists(self.config_file):
|
||||
if self.bot.fileSystem.exists(self.config_file):
|
||||
try:
|
||||
# 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))
|
||||
# I keep the right of master_admin on my bot
|
||||
if 318866596502306816 not in self.config["master_admins"]:
|
||||
@ -124,7 +125,7 @@ class Guild:
|
||||
|
||||
def save_config(self):
|
||||
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)
|
||||
except PermissionError:
|
||||
error("Cannot write to configuration file.")
|
||||
@ -145,6 +146,7 @@ class FoBot(discord.Client):
|
||||
self.guilds_class = {}
|
||||
self.modules = {}
|
||||
self.load_modules()
|
||||
self.fileSystem = fileSystem
|
||||
|
||||
def load_modules(self):
|
||||
for module in os.listdir('modules'):
|
||||
@ -153,10 +155,10 @@ class FoBot(discord.Client):
|
||||
self.modules.update({module[:-3]: imported.MainClass})
|
||||
|
||||
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:
|
||||
# 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))
|
||||
except PermissionError:
|
||||
critical("Cannot open config file.")
|
||||
@ -175,11 +177,11 @@ class FoBot(discord.Client):
|
||||
self.guilds_class.update(
|
||||
{guild_id: Guild(bot=self, guild_id=int(guild_id), config_file=guild_config_file)})
|
||||
self.save_config()
|
||||
elif fileSystem.exists(self.config_folder):
|
||||
elif self.fileSystem.exists(self.config_folder):
|
||||
self.save_config()
|
||||
else:
|
||||
try:
|
||||
fileSystem.makedir(self.config_folder)
|
||||
self.fileSystem.makedir(self.config_folder)
|
||||
except PermissionError:
|
||||
critical("Cannot create config folder.")
|
||||
sys.exit()
|
||||
@ -188,7 +190,7 @@ class FoBot(discord.Client):
|
||||
for guild in self.guilds_class.values():
|
||||
guild.save_config()
|
||||
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)
|
||||
except PermissionError:
|
||||
critical("Cannot write to configuration file.")
|
||||
@ -209,7 +211,17 @@ class FoBot(discord.Client):
|
||||
|
||||
async def on_error(self, event, *args, **kwargs):
|
||||
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):
|
||||
await self.guilds_class[msg.guild.id].on_message(msg)
|
||||
|
@ -1,6 +1,8 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
import fs
|
||||
|
||||
import traductions as tr
|
||||
|
||||
|
||||
@ -9,22 +11,37 @@ class MainClass:
|
||||
|
||||
def __init__(self, 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):
|
||||
if not self.piFileError:
|
||||
start = 0
|
||||
if len(args) == 1:
|
||||
try:
|
||||
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"])
|
||||
return
|
||||
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))
|
||||
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):
|
||||
if not self.piFileError:
|
||||
if len(args) == 0:
|
||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["NotEnoughParamError"])
|
||||
return
|
||||
@ -38,7 +55,7 @@ class MainClass:
|
||||
except re.error:
|
||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["RegexError"])
|
||||
return
|
||||
with open(self.pi_file) as pi_file:
|
||||
with self.guild.bot.fileSystem.open(self.pi_file) as pi_file:
|
||||
pi = pi_file.readline()
|
||||
results = to_search.finditer(pi)
|
||||
texts = []
|
||||
@ -55,6 +72,9 @@ class MainClass:
|
||||
await msg.channel.send(text)
|
||||
else:
|
||||
await msg.channel.send("Pas de résultats")
|
||||
else:
|
||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["PiFileError"])
|
||||
return
|
||||
|
||||
async def on_message(self, msg):
|
||||
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 "
|
||||
"du bot.",
|
||||
"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.",
|
||||
"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",
|
||||
"PiFileError": "Les décimales de pi sont indisponibles, veuillez réessayer plus tard...",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user