fix for /end

This commit is contained in:
zeragonii 2024-10-20 10:20:12 +00:00
parent cfdb8fc83a
commit a6e39a47f4
1 changed files with 33 additions and 25 deletions

58
bot.py
View File

@ -137,38 +137,46 @@ async def lfm(interaction: discord.Interaction, type: str, dungeon: str, level:
async def end(interaction: discord.Interaction):
# Check if the command is executed within a thread
if interaction.channel.type in (discord.ChannelType.public_thread, discord.ChannelType.private_thread):
thread_id = interaction.channel.id
# Lock the thread
await interaction.channel.edit(locked=True)
# Load the current logs
# Load the logs and find the corresponding message
logs = load_lfm_logs()
# Check if the thread ID is in the logs
found_message_id = None
for message_id, log_entry in logs.items():
if log_entry.get("thread_id") == thread_id:
found_message_id = message_id
thread_message_id = None
for message_id, info in logs.items():
if info.get("thread_id") == interaction.channel.id:
thread_message_id = int(message_id)
break
if found_message_id is not None:
# Lock the thread
await interaction.channel.edit(locked=True)
# Get the original message object and edit it to show that the run has ended
if thread_message_id:
# Retrieve the original message
try:
original_message = await interaction.channel.fetch_message(int(found_message_id))
await original_message.edit(content="This run has now ended.", embed=None)
original_message = await interaction.channel.parent.fetch_message(thread_message_id)
if original_message:
# Edit the original message to indicate the run has ended
embed = original_message.embeds[0]
# Update the field that says "Join the thread here"
embed.set_field_at(
3, # The index of the field to replace (Join the Thread field is the 4th, so index is 3)
name="Status",
value="This run has now ended", # Replace "Join the thread here" with this message
inline=False
)
await original_message.edit(embed=embed)
# Mark the run as ended in the logs
logs[str(thread_message_id)]["status"] = "ended"
del logs[thread_message_id]
save_lfm_logs(logs)
except discord.NotFound:
await interaction.followup.send(f"Original message not found.", ephemeral=True)
await interaction.response.send_message("Could not find the original message.", ephemeral=True)
# Remove the entry from the logs
del logs[found_message_id]
save_lfm_logs(logs)
# Send a message indicating the run has ended
await interaction.channel.send("This run has now ended.")
await interaction.response.send_message("The run has been successfully ended and the logs have been cleared.", ephemeral=True)
else:
await interaction.response.send_message("This thread was not found in the logs.", ephemeral=True)
# Send a message indicating the run has ended
await interaction.channel.send("This run has now ended.")
await interaction.response.send_message("The run has been successfully ended.", ephemeral=True)
else:
await interaction.response.send_message("This command can only be used in a thread.", ephemeral=True)