2018-08-31 19:19:44 +02:00
|
|
|
import os
|
2018-09-01 17:39:14 +02:00
|
|
|
import re
|
|
|
|
|
2018-09-01 17:57:51 +02:00
|
|
|
import fs
|
|
|
|
|
2018-09-01 11:27:48 +02:00
|
|
|
import traductions as tr
|
2018-08-31 19:19:44 +02:00
|
|
|
|
|
|
|
|
2018-08-28 18:06:29 +02:00
|
|
|
class MainClass:
|
|
|
|
name = "pi"
|
|
|
|
|
|
|
|
def __init__(self, guild):
|
|
|
|
self.guild = guild
|
2018-09-11 08:55:26 +02:00
|
|
|
self.pi_file = "modules/pi/pi1.txt"
|
2018-09-01 17:57:51 +02:00
|
|
|
self.piFileError = False
|
2018-09-11 08:55:26 +02:00
|
|
|
|
2018-08-28 18:06:29 +02:00
|
|
|
|
|
|
|
async def pi(self, msg, command, args):
|
2018-09-01 17:57:51 +02:00
|
|
|
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:
|
2018-09-01 17:39:14 +02:00
|
|
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["TooBigNumberPiError"])
|
2018-09-01 17:57:51 +02:00
|
|
|
return
|
2018-09-11 08:55:26 +02:00
|
|
|
with open(self.pi_file) as pi_file:
|
2018-09-01 17:57:51 +02:00
|
|
|
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
|
2018-08-28 18:06:29 +02:00
|
|
|
|
|
|
|
async def pi_search(self, msg, command, args):
|
2018-09-01 17:57:51 +02:00
|
|
|
if not self.piFileError:
|
|
|
|
if len(args) == 0:
|
|
|
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["NotEnoughParamError"])
|
2018-09-01 17:39:14 +02:00
|
|
|
return
|
2018-09-01 17:57:51 +02:00
|
|
|
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
|
|
|
|
elif len(args[0]) > 50:
|
|
|
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["RegexTooLongError"])
|
|
|
|
except re.error:
|
|
|
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["RegexError"])
|
|
|
|
return
|
2018-09-11 08:55:26 +02:00
|
|
|
with open(self.pi_file) as pi_file:
|
2018-09-01 17:57:51 +02:00
|
|
|
pi = pi_file.readline()
|
|
|
|
results = to_search.finditer(pi)
|
|
|
|
texts = []
|
|
|
|
for result in results:
|
|
|
|
texts.append(
|
|
|
|
"Une occurence de votre recherche a été trouvée à la {debut}ème place: `{before}`{find}`{after}`".format(
|
|
|
|
debut=result.start(0),
|
|
|
|
before=pi[result.start(0) - 10:result.start(0)],
|
|
|
|
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")
|
2018-09-01 17:39:14 +02:00
|
|
|
else:
|
2018-09-01 17:57:51 +02:00
|
|
|
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["PiFileError"])
|
|
|
|
return
|
2018-08-28 18:06:29 +02:00
|
|
|
|
|
|
|
async def on_message(self, msg):
|
|
|
|
if msg.content.startswith(self.guild.config["prefix"]):
|
|
|
|
command, *args = msg.content.lstrip(self.guild.config["prefix"]).split(" ")
|
|
|
|
if command == "pi":
|
2018-08-31 19:19:44 +02:00
|
|
|
await self.pi(msg, command, args)
|
2018-09-01 17:39:14 +02:00
|
|
|
elif command == "fpi":
|
|
|
|
await self.pi_search(msg, command, args)
|
2018-08-31 19:19:44 +02:00
|
|
|
return
|