Skip to content

Commit a3718d0

Browse files
committed
[style](discord-bot): choices msg replaced instead of flooding
1 parent e46f212 commit a3718d0

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

discord-bot/major_bot.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
Majority Judgment Bar Chart Distribution Discord Bot
55
"""
6-
6+
import asyncio
77
import sys
88
import os
99

@@ -39,6 +39,10 @@
3939
# custom_id => str(UUID) + "button_" + GRADES[i] + "_" + str(user) + "_" + choice
4040
BUTTONS = {}
4141

42+
# Keep the original interaction to edit (ephemeral trick)
43+
# ORIGINAL_INTER {"user1": inter, "user2": inter...}
44+
ORIGINAL_INTER = {}
45+
4246
# Validations storage allow a global button deactivation
4347
# Keep in memory [user1, user2 ...]
4448
VALIDATIONS = []
@@ -106,6 +110,8 @@ async def major_update(inter: disnake.MessageInteraction):
106110
global CHOICES
107111
global RESULTS
108112
global VALIDATIONS
113+
global ORIGINAL_INTER
114+
109115
user = inter.author.id
110116
user_name = inter.author.name
111117

@@ -180,25 +186,31 @@ async def major_update(inter: disnake.MessageInteraction):
180186
next_choice = CHOICES[c_index + 1]
181187
except:
182188
logging.info("User %s has finished '%s'", user_name, QUESTION)
183-
await inter.response.send_message(
184-
"Vous avez répondu à toutes les questions, merci pour votre participation !\n\n Résume de vos choix :\n"
189+
await ORIGINAL_INTER[user].edit_original_response(
190+
content="Vous avez répondu à toutes les questions, merci pour votre participation !\n\n Résume de vos choix :\n"
185191
+ str(RESULTS[user]) + "\n"
186192
+ "\nCommande pour afficher les résultats : **/major_display**\n"
187193
+ "\nVoulez-vous valider vos choix ou recommencer le jugement majoritaire ?",
188-
ephemeral=True,
189194
components=[disnake.ui.Button(label="Valider", style=disnake.ButtonStyle.success,
190195
custom_id=str(UUID) + "button_validate_" + str(user)),
191196
disnake.ui.Button(label="Recommencer", style=disnake.ButtonStyle.primary,
192197
custom_id=str(UUID) + "button_reset_" + str(user))]
193198
)
199+
await inter.response.defer()
194200
return
195201
break # do not continue, break loop for the next "choice"
196202

197203
# Display buttons
198-
await inter.response.send_message(
199-
"Que pensez-vous de **" + next_choice + "** ?",
204+
content_msg = "Que pensez-vous de **" + next_choice + "** ?"
205+
if ORIGINAL_INTER.get(user) is not None:
206+
await ORIGINAL_INTER[user].edit_original_response(content=content_msg, components=BUTTONS[user][next_choice])
207+
await inter.response.defer()
208+
else:
209+
await inter.response.send_message(
210+
content_msg,
200211
ephemeral=True,
201212
components=BUTTONS[user][next_choice])
213+
ORIGINAL_INTER[user] = inter
202214

203215
def dict_to_csv(data, filename="output.csv"):
204216
# Filter out any entries where a sub-dictionary contains None values

0 commit comments

Comments
 (0)