From 705a953965ddb0e27214639fc6bdf6a5e9199917 Mon Sep 17 00:00:00 2001 From: zeragonii Date: Sat, 19 Oct 2024 20:13:22 +0000 Subject: [PATCH] test2 --- bot.py | 57 ++++++++++++++++++++++++- bot.py.bak | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 bot.py.bak diff --git a/bot.py b/bot.py index ec33aee..370b05d 100644 --- a/bot.py +++ b/bot.py @@ -4,6 +4,7 @@ from discord import app_commands import os from dotenv import load_dotenv import asyncio +import json # Load environment variables from .env file load_dotenv() @@ -15,6 +16,9 @@ intents.members = True # Ensure the bot has permission to read member statuses intents.message_content = True # Ensure the bot can read message content bot = commands.Bot(command_prefix='/', intents=intents) +# File to store the LFM logs +log_file = "lfm_logs.json" + # Define dungeon aliases dungeon_aliases = { "Ara-Kara, City of Echoes": ["ara", "city of echoes", "coe"], @@ -33,6 +37,18 @@ for full_name, aliases in dungeon_aliases.items(): for alias in aliases: dungeon_lookup[alias] = full_name +# Load or create the JSON log file +def load_lfm_logs(): + if not os.path.exists(log_file): + with open(log_file, 'w') as file: + json.dump({}, file) # Create an empty JSON structure + with open(log_file, 'r') as file: + return json.load(file) + +def save_lfm_logs(logs): + with open(log_file, 'w') as file: + json.dump(logs, file, indent=4) + @bot.event async def on_ready(): print(f'Logged in as {bot.user}') @@ -89,8 +105,13 @@ async def lfm(interaction: discord.Interaction, type: str, dungeon: str, level: embed.set_footer(text=f"Created by {interaction.user.display_name}", icon_url=interaction.user.avatar.url) # Send the embed to the original channel - await interaction.channel.send(f"@here", embed=embed) + message = await interaction.channel.send(f"@here", embed=embed) + # Log message ID and thread ID + logs = load_lfm_logs() + logs[str(message.id)] = {"thread_id": thread.id, "status": "active"} + save_lfm_logs(logs) + # Post a welcome message in the new thread welcome_message = ( f"Welcome to **{full_dungeon_name}**! 🎉\n" @@ -109,6 +130,40 @@ async def end(interaction: discord.Interaction): if interaction.channel.type in (discord.ChannelType.public_thread, discord.ChannelType.private_thread): # Lock the thread await interaction.channel.edit(locked=True) + + # Load the logs and find the corresponding message + logs = load_lfm_logs() + 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 thread_message_id: + # Retrieve the original message + try: + 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" + save_lfm_logs(logs) + + except discord.NotFound: + await interaction.response.send_message("Could not find the original message.", 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) diff --git a/bot.py.bak b/bot.py.bak new file mode 100644 index 0000000..ec33aee --- /dev/null +++ b/bot.py.bak @@ -0,0 +1,119 @@ +import discord +from discord.ext import commands +from discord import app_commands +import os +from dotenv import load_dotenv +import asyncio + +# Load environment variables from .env file +load_dotenv() +TOKEN = os.getenv('DISCORD_TOKEN') + +# Define your bot +intents = discord.Intents.default() +intents.members = True # Ensure the bot has permission to read member statuses +intents.message_content = True # Ensure the bot can read message content +bot = commands.Bot(command_prefix='/', intents=intents) + +# Define dungeon aliases +dungeon_aliases = { + "Ara-Kara, City of Echoes": ["ara", "city of echoes", "coe"], + "City of Threads": ["threads", "city of threads", "cot"], + "The Stonevault": ["stonevault", "vault"], + "The Dawnbreaker": ["dawnbreaker", "breaker"], + "Mists of Tirna Scithe": ["mists", "tirna", "scithe", "mots"], + "The Necrotic Wake": ["nw", "necrotic wake", "necrotic"], + "Siege of Boralus": ["siege", "boralus", "sob"], + "Grim Batol": ["grim", "batol", "gb"] +} + +# Reverse the dictionary for easier lookup +dungeon_lookup = {} +for full_name, aliases in dungeon_aliases.items(): + for alias in aliases: + dungeon_lookup[alias] = full_name + +@bot.event +async def on_ready(): + print(f'Logged in as {bot.user}') + try: + synced = await bot.tree.sync() + print(f"Synced {len(synced)} commands.") + except Exception as e: + print(f"Error syncing commands: {e}") + +@bot.tree.command(name="lfm", description="Start looking for members for a Mythic+ run.") +@app_commands.describe( + type="Are you wanting to push? Or clear?", + dungeon="Name of the dungeon", + level="Keystone level", + private="Should the thread be private?" +) +async def lfm(interaction: discord.Interaction, type: str, dungeon: str, level: int, private: bool = False): + if type not in ['pushing', 'completion']: + await interaction.response.send_message("Please specify the type as either 'pushing' or 'completion'.", ephemeral=True) + return + + # Correct the dungeon name using the alias lookup + dungeon_lower = dungeon.lower() + full_dungeon_name = dungeon_lookup.get(dungeon_lower) + + # Fail if the dungeon name is invalid + if full_dungeon_name is None: + await interaction.response.send_message( + f"**'{dungeon}'** is not a recognized dungeon name. Please try again with a valid dungeon name or an alias.", + ephemeral=True + ) + return # Exit the command + + # Send an initial response to acknowledge the command + await interaction.response.send_message("Creating your group... Please wait.", ephemeral=True) + + try: + # Create a message for the channel + thread_name = f"{full_dungeon_name} - +{level} - {type.capitalize()}" + + # Create the thread + thread = await interaction.channel.create_thread(name=thread_name, message=interaction.message, invitable=not private) + + # Tag the user in the message + user_mention = interaction.user.mention # This will mention the user + thread_link = f"[Join the thread here]({thread.jump_url})" + + # Create the embed + embed = discord.Embed(title="Looking for Members!", color=discord.Color.blue()) + embed.add_field(name="Dungeon", value=full_dungeon_name, inline=False) + embed.add_field(name="Keystone Level", value=f"+{level}", inline=False) + embed.add_field(name="Type", value=type.capitalize(), inline=False) + embed.add_field(name="Join the Thread", value=thread_link, inline=False) + embed.set_footer(text=f"Created by {interaction.user.display_name}", icon_url=interaction.user.avatar.url) + + # Send the embed to the original channel + await interaction.channel.send(f"@here", embed=embed) + + # Post a welcome message in the new thread + welcome_message = ( + f"Welcome to **{full_dungeon_name}**! 🎉\n" + "Don't forget to use `/end` when you're done to lock the thread!" + ) + await thread.send(welcome_message) + + except discord.Forbidden: + await interaction.followup.send("I don't have permission to create a thread in this channel.") + except Exception as e: + await interaction.followup.send(f"An error occurred: {e}") + +@bot.tree.command(name="end", description="End the current Mythic+ run.") +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): + # Lock the thread + await interaction.channel.edit(locked=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) + +# Run the bot +bot.run(TOKEN)