Merge branch 'master' of moriya.zapto.org:PDBA/bot-base
This commit is contained in:
commit
52c26fa9d0
90
config/Types.py
Normal file
90
config/Types.py
Normal file
@ -0,0 +1,90 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
|
||||
class Config:
|
||||
name: Optional[str]
|
||||
parent: Optional[Config]
|
||||
config: Dict[Any, Any]
|
||||
|
||||
def __init__(self, parent: Config = None, name: str = None, client: LBI = None):
|
||||
"""Create Config
|
||||
|
||||
:param parent: Parent configuration
|
||||
:param name: Configuration name
|
||||
:type parent: Config
|
||||
:type name: str"""
|
||||
self.parent = parent
|
||||
self.config = dict()
|
||||
self.name = None
|
||||
self.client = client
|
||||
if self.parent:
|
||||
self.name = name
|
||||
self.client = self.parent.client
|
||||
|
||||
def init(self, config):
|
||||
"""Load default configuration
|
||||
|
||||
:param config: Default configuration
|
||||
:type config: dict
|
||||
:return: None"""
|
||||
# Load data from config file before initialisation
|
||||
self.load()
|
||||
# Get data from parent
|
||||
if self.parent is not None:
|
||||
self.parent.config[self.name] = self.parent.config.get(self.name) if self.parent.config.get(
|
||||
self.name) is not None else self.config
|
||||
self.config = self.parent.config[self.name]
|
||||
# Set config only if not already defined
|
||||
for k, v in config.items():
|
||||
self.config[k] = self.config.get(k) if self.config.get(k) is not None else v
|
||||
# Save new datas
|
||||
self.save()
|
||||
|
||||
def _save(self):
|
||||
"""Internal function for save
|
||||
|
||||
Must be overridden by all type of config to handle saving"""
|
||||
# Call parent save if necessary
|
||||
if self.parent:
|
||||
self.parent.save()
|
||||
|
||||
def save(self):
|
||||
"""Public save function
|
||||
|
||||
Do not override"""
|
||||
self._save()
|
||||
|
||||
def _load(self):
|
||||
"""Internal function for load
|
||||
|
||||
Mus be overridden by all type of config to handle loading"""
|
||||
# Load parent if necessary
|
||||
if self.parent:
|
||||
self.parent.load()
|
||||
self.config = self.parent.config.get(self.name)
|
||||
# Initialize parent if necessary
|
||||
if self.config is None:
|
||||
self.parent.config[self.name] = {}
|
||||
self.config = {}
|
||||
|
||||
def load(self):
|
||||
"""Public load function
|
||||
|
||||
Do not override"""
|
||||
self._load()
|
||||
|
||||
def __getattr__(self, item):
|
||||
return self.config.get(item)
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self.config.get(item)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if self.parent:
|
||||
self.parent[self.name][key] = value
|
||||
self.config = self.parent[self.name]
|
||||
else:
|
||||
self.config[key] = value
|
||||
self.save()
|
@ -167,7 +167,7 @@ class BaseClass:
|
||||
"""Override this function to handle all messages starting with `{prefix}{command_text}`
|
||||
|
||||
Function which is executed for all command_text doesn't match with a `com_{subcommand}` function"""
|
||||
pass
|
||||
await self.send_help(message.channel)
|
||||
|
||||
async def com_help(self, message, args, kwargs):
|
||||
await self.send_help(message.channel)
|
||||
|
@ -30,6 +30,9 @@ class MainClass(BaseClassPython):
|
||||
self.config.register("channel", factory(config.config_types.Channel, self.client))
|
||||
self.history = {}
|
||||
|
||||
async def on_ready(self):
|
||||
await self.fill_history()
|
||||
|
||||
async def on_message(self, message: discord.Message):
|
||||
# Fill history
|
||||
if message.author.bot:
|
||||
@ -114,83 +117,84 @@ class MainClass(BaseClassPython):
|
||||
|
||||
async def com_stats(self, message: discord.Message, args, kwargs):
|
||||
# TODO: Finir sum
|
||||
if not ((not False or (not False or not ("sum" in args))) or not True):
|
||||
if message.mentions:
|
||||
top = self.get_top(only_users=[mention.id for mention in message.mentions] + [message.author.id])
|
||||
else:
|
||||
# TOP 5 + auteur
|
||||
top = self.get_top(top=5, with_user=[message.author.id])
|
||||
dates = []
|
||||
new_top = {}
|
||||
for t in top:
|
||||
for date, _ in t[1]:
|
||||
dates.append(date)
|
||||
dates.sort()
|
||||
dates.append(datetime.datetime.today() + datetime.timedelta(days=1))
|
||||
for t in top:
|
||||
user = t[0]
|
||||
new_top.update({user: ([dates[0]], [0])})
|
||||
i = 0
|
||||
for date, _ in t[1]:
|
||||
while date < dates[i]:
|
||||
new_top[user][0].append(dates[i])
|
||||
new_top[user][1].append(new_top[user][1][-1])
|
||||
i += 1
|
||||
new_top[user][0].append(date)
|
||||
new_top[user][1].append(new_top[user][1][-1] + 1)
|
||||
|
||||
to_plot = [t[1][1:] for t in new_top.values()]
|
||||
np.xlabel("Temps", fontsize=30)
|
||||
np.ylabel("Score", fontsize=30)
|
||||
np.title("Évolution du nombre de perdu au cours du temps.", fontsize=40)
|
||||
np.legend()
|
||||
file_name = f"/tmp/{time.time()}.png"
|
||||
np.savefig(file_name, bbox_inches='tight')
|
||||
await message.channel.send(file=discord.File(file_name))
|
||||
|
||||
if "history" in args:
|
||||
since = datetime.datetime(year=1, month=1, day=1)
|
||||
debut_message = "la création du salon"
|
||||
top = 5
|
||||
if "s" in [k[0] for k in kwargs]:
|
||||
try:
|
||||
d = [k[1] for k in kwargs if k[0] == "s"][0]
|
||||
since = datetime.datetime.now() - datetime.timedelta(days=float(d))
|
||||
debut_message = humanize.naturalday(since.date(), format='le %d %b')
|
||||
except ValueError:
|
||||
pass
|
||||
if "t" in [k[0] for k in kwargs]:
|
||||
try:
|
||||
top = int([k[1] for k in kwargs if k[0] == "t"][0])
|
||||
except ValueError:
|
||||
pass
|
||||
# Si mention, alors uniquement les mentions
|
||||
if message.mentions:
|
||||
top = self.get_top(since=since,
|
||||
only_users=[mention.id for mention in message.mentions])
|
||||
else:
|
||||
# TOP 5 + auteur
|
||||
top = self.get_top(since=since, top=top, with_user=[message.author.id])
|
||||
new_top = {}
|
||||
for t in top:
|
||||
c = 0
|
||||
counts = []
|
||||
async with message.channel.typing():
|
||||
if not ((not False or (not False or not ("sum" in args))) or not True):
|
||||
if message.mentions:
|
||||
top = self.get_top(only_users=[mention.id for mention in message.mentions] + [message.author.id])
|
||||
else:
|
||||
# TOP 5 + auteur
|
||||
top = self.get_top(top=5, with_user=[message.author.id])
|
||||
dates = []
|
||||
for date, _ in t[1]:
|
||||
c += 1
|
||||
counts.append(c)
|
||||
dates.append(date)
|
||||
new_top.update({t[0]: (dates, counts)})
|
||||
np.figure(num=None, figsize=(25, 15), dpi=120, facecolor='w', edgecolor='k')
|
||||
for user, (dates, counts) in new_top.items():
|
||||
np.plot_date(dates, counts, linestyle='-', label=str(self.client.get_user(user).name))
|
||||
np.xlabel("Temps", fontsize=30)
|
||||
np.ylabel("Score", fontsize=30)
|
||||
np.legend(fontsize=20)
|
||||
np.title(f"Évolution du nombre de perdu au cours du temps depuis {debut_message}.", fontsize=35)
|
||||
file_name = f"/tmp/{time.time()}.png"
|
||||
np.savefig(file_name, bbox_inches='tight')
|
||||
await message.channel.send(file=discord.File(file_name))
|
||||
new_top = {}
|
||||
for t in top:
|
||||
for date, _ in t[1]:
|
||||
dates.append(date)
|
||||
dates.sort()
|
||||
dates.append(datetime.datetime.today() + datetime.timedelta(days=1))
|
||||
for t in top:
|
||||
user = t[0]
|
||||
new_top.update({user: ([dates[0]], [0])})
|
||||
i = 0
|
||||
for date, _ in t[1]:
|
||||
while date < dates[i]:
|
||||
new_top[user][0].append(dates[i])
|
||||
new_top[user][1].append(new_top[user][1][-1])
|
||||
i += 1
|
||||
new_top[user][0].append(date)
|
||||
new_top[user][1].append(new_top[user][1][-1] + 1)
|
||||
|
||||
to_plot = [t[1][1:] for t in new_top.values()]
|
||||
np.xlabel("Temps", fontsize=30)
|
||||
np.ylabel("Score", fontsize=30)
|
||||
np.title("Évolution du nombre de perdu au cours du temps.", fontsize=40)
|
||||
np.legend()
|
||||
file_name = f"/tmp/{time.time()}.png"
|
||||
np.savefig(file_name, bbox_inches='tight')
|
||||
await message.channel.send(file=discord.File(file_name))
|
||||
|
||||
if "history" in args:
|
||||
since = datetime.datetime(year=1, month=1, day=1)
|
||||
debut_message = "la création du salon"
|
||||
top = 5
|
||||
if "s" in [k[0] for k in kwargs]:
|
||||
try:
|
||||
d = [k[1] for k in kwargs if k[0] == "s"][0]
|
||||
since = datetime.datetime.now() - datetime.timedelta(days=float(d))
|
||||
debut_message = humanize.naturalday(since.date(), format='le %d %b')
|
||||
except ValueError:
|
||||
pass
|
||||
if "t" in [k[0] for k in kwargs]:
|
||||
try:
|
||||
top = int([k[1] for k in kwargs if k[0] == "t"][0])
|
||||
except ValueError:
|
||||
pass
|
||||
# Si mention, alors uniquement les mentions
|
||||
if message.mentions:
|
||||
top = self.get_top(since=since,
|
||||
only_users=[mention.id for mention in message.mentions])
|
||||
else:
|
||||
# TOP 5 + auteur
|
||||
top = self.get_top(since=since, top=top, with_user=[message.author.id])
|
||||
new_top = {}
|
||||
for t in top:
|
||||
c = 0
|
||||
counts = []
|
||||
dates = []
|
||||
for date, _ in t[1]:
|
||||
c += 1
|
||||
counts.append(c)
|
||||
dates.append(date)
|
||||
new_top.update({t[0]: (dates, counts)})
|
||||
np.figure(num=None, figsize=(25, 15), dpi=120, facecolor='w', edgecolor='k')
|
||||
for user, (dates, counts) in new_top.items():
|
||||
np.plot_date(dates, counts, linestyle='-', label=str(self.client.get_user(user).name))
|
||||
np.xlabel("Temps", fontsize=30)
|
||||
np.ylabel("Score", fontsize=30)
|
||||
np.legend(fontsize=20)
|
||||
np.title(f"Évolution du nombre de perdu au cours du temps depuis {debut_message}.", fontsize=35)
|
||||
file_name = f"/tmp/{time.time()}.png"
|
||||
np.savefig(file_name, bbox_inches='tight')
|
||||
await message.channel.send(file=discord.File(file_name))
|
||||
|
||||
async def command(self, message, args, kwargs):
|
||||
if message.mentions:
|
||||
|
Loading…
Reference in New Issue
Block a user