Untitled
unknown
plain_text
a year ago
8.5 kB
12
Indexable
class AnnouncementView(discord.ui.View): def __init__(self, bot, sender: discord.User, sender_id: int, target_channel_id: int): super().__init__() self.bot = bot # Store the bot instance self.sender = sender # Store the sender self.sender_id = sender_id self.target_channel_id = target_channel_id # Store the target channel ID self.embed = discord.Embed(color=discord.Color.blue()) # Initialize the embed with a specified color self.embed.add_field(name="Title", value="Not provided", inline=False) self.embed.add_field(name="Description", value="Not provided", inline=False) self.embed.add_field(name="Date", value="Not provided", inline=False) self.embed.title = "Announcement! 📢" self.add_item(self.title_button) self.add_item(self.description_button) self.add_item(self.date_button) self.add_item(ConfirmButton(self)) # Add the ConfirmButton def add_items(self): title_button = title_button description_button = description_button date_button = date_button confirm_button = ConfirmButton(self) self.add_item(title_button) self.add_item(description_button) self.add_item(date_button) self.add_item(confirm_button) def set_thumbnail(self, image_url): # Set the thumbnail for the embed using the sender's circular avatar circular_image = create_circular_image(image_url) self.file = discord.File(circular_image, filename="circular_avatar.png") self.embed.set_thumbnail(url="attachment://circular_avatar.png") # Function to update the embed with new title, description, and date def update_embed(self, title=None, description=None, date=None): if title: self.embed.set_field_at(0, name="Title", value=title, inline=False) if description: self.embed.set_field_at(1, name="Description", value=description, inline=False) if date: self.embed.set_field_at(2, name="Date", value=date, inline=False) async def interaction_check(self, interaction: discord.Interaction) -> bool: if interaction.user.id != self.sender_id: await interaction.response.send_message("You are not allowed to interact with this embed.", ephemeral=True) return False return True # Define a button for confirming the announcement class ConfirmButton(discord.ui.Button): def __init__(self, parent_view): super().__init__(label="Confirm", style=discord.ButtonStyle.success) self.parent_view = parent_view # Callback function to handle button click async def callback(self, interaction: discord.Interaction): # Get the target channel to send the announcement target_channel = self.parent_view.bot.get_channel(self.parent_view.target_channel_id) if target_channel is None: # Notify if the channel is invalid await interaction.response.send_message("Invalid channel ID", ephemeral=True) return # Set the thumbnail before sending the embed self.parent_view.set_thumbnail(self.parent_view.sender.avatar.url) # Send the embed to the target channel await target_channel.send(embed=self.parent_view.embed, file=self.parent_view.file) await interaction.response.send_message("Announcement sent for review!", ephemeral=True) # Define a command for creating an announcement @bot.tree.command(name="announcement", description="📌 • Create an Announcement!") async def announcement_command(interaction: discord.Interaction): try: await interaction.response.defer(ephemeral=True) # Defer the initial response to allow interaction # Initialize an empty embed embed = discord.Embed(color=discord.Color.blue()) embed.title = "Announcement! 📢" embed.add_field(name="Title", value="Not provided", inline=False) embed.add_field(name="Description", value="Not provided", inline=False) embed.add_field(name="Date", value="Not provided", inline=False) embed.set_footer(text=f"Sent by: @{interaction.user.name}") # Send the message with the initial embed and user's circular profile picture as thumbnail circular_image = create_circular_image(interaction.user.avatar.url) file = discord.File(circular_image, filename="circular_avatar.png") embed.set_thumbnail(url="attachment://circular_avatar.png") prompt_message = await interaction.followup.send(embed=embed, file=file, ephemeral=True) # Add buttons for title, description, date, and confirm title_button = discord.ui.Button(label="Enter Title", style=discord.ButtonStyle.secondary, custom_id="title_button") description_button = discord.ui.Button(label="Enter Description", style=discord.ButtonStyle.secondary, custom_id="description_button") date_button = discord.ui.Button(label="Enter Date", style=discord.ButtonStyle.secondary, custom_id="date_button") confirm_button = discord.ui.Button(label="Confirm", style=discord.ButtonStyle.success, custom_id="confirm_button") view = discord.ui.View() view.add_item(title_button) view.add_item(description_button) view.add_item(date_button) view.add_item(confirm_button) # Update the message with the view containing buttons await prompt_message.edit(view=view) while True: try: interaction = await bot.wait_for("button_click", timeout=60, check=lambda i: i.user == interaction.user) except asyncio.TimeoutError: await prompt_message.edit(content="Timed out. Please try again.", view=None) return # Handle button clicks to update the embed if interaction.component.custom_id == "title_button": title = await input_value(interaction, "Title") if title: embed.set_field_at(0, name="Title", value=title, inline=False) elif interaction.component.custom_id == "description_button": description = await input_value(interaction, "Description") if description: embed.set_field_at(1, name="Description", value=description, inline=False) elif interaction.component.custom_id == "date_button": date = await input_value(interaction, "Date (e.g., 05/19/2024)") if date: embed.set_field_at(2, name="Date", value=date, inline=False) elif interaction.component.custom_id == "confirm_button": break # Break the loop when the user confirms the announcement await interaction.response.edit_message(embed=embed, view=view) # Send the final announcement to the target channel target_channel_id = 1243960674015772772 # Specify the target channel ID message = await bot.get_channel(target_channel_id).send(embed=embed) # Add approve and decline buttons as components to the sent message approve_button = discord.ui.Button(label="Approve", style=discord.ButtonStyle.success, custom_id="approve_button") decline_button = discord.ui.Button(label="Decline", style=discord.ButtonStyle.danger, custom_id="decline_button") approve_view = discord.ui.View() approve_view.add_item(approve_button) approve_view.add_item(decline_button) await message.edit(view=approve_view) except Exception as e: error_message = f"An error occurred while creating the announcement: {e}" print(error_message) await interaction.response.send_message(error_message, ephemeral=True) async def input_value(interaction, prompt): """Prompt the user to input a value.""" await interaction.respond( content=f"Please enter the {prompt}:", ephemeral=True ) try: response = await bot.wait_for("message", timeout=60, check=lambda m: m.author == interaction.user) return response.content except asyncio.TimeoutError: await interaction.followup.send("Timed out. Please try again.", ephemeral=True) return None
Editor is loading...
Leave a Comment