Added "tg fouras", added fouras not being able to both answer with fouras and poilau module, fixed a few bugs with the news poilau cooldown

This commit is contained in:
Anselme FRANÇOIS 2023-07-12 00:52:42 +02:00
parent 4558fb2e1d
commit 49af236a1c
4 changed files with 43 additions and 35 deletions

View File

@ -47,8 +47,10 @@ async def on_message(message):
if isinstance( if isinstance(
message.channel, (discord.DMChannel, discord.TextChannel, discord.Thread) message.channel, (discord.DMChannel, discord.TextChannel, discord.Thread)
): ):
handled = False
for m in client.modules: for m in client.modules:
await m.handle_message(message) if not handled:
handled = await m.handle_message(message)
# Initialise le client # Initialise le client

View File

@ -12,7 +12,7 @@ class BaseModule:
def load(self): def load(self):
raise NotImplementedError raise NotImplementedError
async def handle_message(self, message): async def handle_message(self, message)-> bool:
raise NotImplementedError raise NotImplementedError
async def get_guild_name(self, guildId) -> str: async def get_guild_name(self, guildId) -> str:

View File

@ -117,9 +117,9 @@ class FourasModule(BaseModule):
current_riddle["index"] + 1, formatted_riddle current_riddle["index"] + 1, formatted_riddle
) )
async def handle_message(self, message): async def handle_message(self, message) -> bool:
if message.author == self._client.user: if message.author == self._client.user:
return return False
message_content = message.content.lower() message_content = message.content.lower()
# command fouras # command fouras
@ -135,14 +135,14 @@ class FourasModule(BaseModule):
await message.channel.send( await message.channel.send(
INVALID_ID.format(len=len(self._client.riddles)) INVALID_ID.format(len=len(self._client.riddles))
) )
return return True
if message_content == "fouras": if message_content == "fouras":
if random.random() <= 0.03: if random.random() <= 0.03:
await message.channel.send("Non") await message.channel.send("Non")
elif len(self._client.riddles) > 0: elif len(self._client.riddles) > 0:
index = random.randint(0, len(self._client.riddles) - 1) index = random.randint(0, len(self._client.riddles) - 1)
await message.channel.send(self.new_riddle(message.channel, index)) await message.channel.send(self.new_riddle(message.channel, index))
return return True
if message_content.startswith("bug"): if message_content.startswith("bug"):
author_user = await self._client.fetch_user(MAINTAINER_ID) author_user = await self._client.fetch_user(MAINTAINER_ID)
@ -150,7 +150,7 @@ class FourasModule(BaseModule):
BUG_REPORT.format(user=message.author.mention, message=message_content, json=self.save()) BUG_REPORT.format(user=message.author.mention, message=message_content, json=self.save())
) )
await message.channel.send(f'Rapport de bug envoyé à {author_user.mention}\nMerci de ton feedback !') await message.channel.send(f'Rapport de bug envoyé à {author_user.mention}\nMerci de ton feedback !')
return return True
broadcast_match = re.match(r"^broadcast\s+(\d+) (.*)", message_content) broadcast_match = re.match(r"^broadcast\s+(\d+) (.*)", message_content)
if broadcast_match and message.author.id == MAINTAINER_ID: if broadcast_match and message.author.id == MAINTAINER_ID:
@ -161,7 +161,7 @@ class FourasModule(BaseModule):
await channel.send(broadcast_message) await channel.send(broadcast_message)
else: else:
await message.channel.send(f'Invalid channel id : {index}') await message.channel.send(f'Invalid channel id : {index}')
return return True
# command reload # command reload
if message_content == "reload_riddles": if message_content == "reload_riddles":
@ -169,27 +169,28 @@ class FourasModule(BaseModule):
await message.channel.send( await message.channel.send(
"Loaded {0} riddles".format(len(self._client.riddles)) "Loaded {0} riddles".format(len(self._client.riddles))
) )
return return True
if message_content == "about fouras": if message_content == "about fouras":
author_user = await self._client.fetch_user(MAINTAINER_ID) author_user = await self._client.fetch_user(MAINTAINER_ID)
await message.channel.send( await message.channel.send(
ABOUT.format(user=author_user.mention, url=API_URL) ABOUT.format(user=author_user.mention, url=API_URL)
) )
return return True
if message_content == "debug": if message_content == "debug fouras":
dump = {} if(message.author.id == 151626081458192384):
for key, value in self._client.ongoing_riddles.items(): dump = {}
dump_channel = dict(value) for key, value in self._client.ongoing_riddles.items():
dump_channel.pop("message", None) dump_channel = dict(value)
dump_channel.pop("answer", None) dump_channel.pop("message", None)
channel_name = await self.get_channel_name(key) dump_channel.pop("answer", None)
dump[channel_name] = dump_channel channel_name = await self.get_channel_name(key)
await message.author.send( dump[channel_name] = dump_channel
"```json\n{0}```".format(json.dumps(dump, ensure_ascii=False, indent=4)) await message.author.send(
) "```json\n{0}```".format(json.dumps(dump, ensure_ascii=False, indent=4))
return )
return True
# if current channel has ongoing riddle # if current channel has ongoing riddle
if message.channel in self._client.ongoing_riddles: if message.channel in self._client.ongoing_riddles:
@ -207,7 +208,7 @@ class FourasModule(BaseModule):
content=self.format_message(current_riddle) content=self.format_message(current_riddle)
) )
self.finish_riddle(message.channel) self.finish_riddle(message.channel)
return return True
if ( if (
message_content == "repete" message_content == "repete"
@ -216,7 +217,7 @@ class FourasModule(BaseModule):
): ):
current_riddle.pop("message") current_riddle.pop("message")
await message.channel.send(self.format_message(current_riddle)) await message.channel.send(self.format_message(current_riddle))
return return True
# Commande /clue : révèle une lettre au hasard de la réponse attendue # Commande /clue : révèle une lettre au hasard de la réponse attendue
if ( if (
@ -237,4 +238,5 @@ class FourasModule(BaseModule):
) )
if nbClues >= len(answer): if nbClues >= len(answer):
self.finish_riddle(message.channel) self.finish_riddle(message.channel)
return return True
return False

View File

@ -51,7 +51,7 @@ class RhymesModule(BaseModule):
return random.choice(rhyme["rhymes"]) return random.choice(rhyme["rhymes"])
return "" return ""
async def handle_message(self, message): async def handle_message(self, message) -> bool:
message_content = message.content.lower() message_content = message.content.lower()
if message_content == "debug poilau": if message_content == "debug poilau":
@ -59,28 +59,32 @@ class RhymesModule(BaseModule):
dump = {} dump = {}
for key, value in self.guild_config.items(): for key, value in self.guild_config.items():
channel_name = await self.get_guild_name(key) channel_name = await self.get_guild_name(key)
dump[channel_name] = value - time.time() sleeping_time = "{:.2f} s".format(min(0, value["cooldown"] - time.time()))
dump[channel_name] = {"cooldown": sleeping_time, "self-control": value["self-control"]}
await message.author.send( await message.author.send(
"```json\n{0}```".format(json.dumps(dump, ensure_ascii=False, indent=4)) "```json\n{0}```".format(json.dumps(dump, ensure_ascii=False, indent=4))
) )
return return True
if message_content == "tg fouras" and message.guild: if message_content == "tg fouras" and message.guild:
self.guild_config[message.guild.id] = {43000,1.0} self.guild_config[message.guild.id] = {"cooldown": time.time() + 40000, "self-control": 2.0}
await message.channel.send("ok :'(") await message.channel.send("ok :'(")
return return True
last_word = self.get_last_word(message_content) last_word = self.get_last_word(message_content)
if message.author != self._client.user and message.guild and last_word: if message.author != self._client.user and message.guild and last_word:
poil = self.poil_auquel(last_word) poil = self.poil_auquel(last_word)
guild_config = self.guild_config.get(message.guild.id, {"cooldown": 0, "self-control": 1.0}) guild_config = self.guild_config.get(message.guild.id, {"cooldown": 0, "self-control": 1.0})
if poil and time.time() - guild_config["cooldown"] > 0: if poil and time.time() - guild_config["cooldown"] > 0:
if random.random() < guild_config["self-control"]: self_control = guild_config["self-control"]
self.guild_config[message.guild.id]["self-control"] *= 0.9 if random.random() < self_control:
return guild_config["self-control"] = self_control * 0.9
self.guild_config[message.guild.id] = guild_config
return False
wait_time = random.randint(0, 900) wait_time = random.randint(0, 900)
if bool(random.getrandbits(1)): if bool(random.getrandbits(1)):
wait_time = random.randint(900, 10800) wait_time = random.randint(900, 10800)
self.guild_config[message.guild.id] = {"cooldown": time.time() + wait_time, "self-control": 1.0} self.guild_config[message.guild.id] = {"cooldown": time.time() + wait_time, "self-control": self_control + 1.0}
await message.channel.send(poil) await message.channel.send(poil)
return return True
return False