Compare commits

...

9 commits

View file

@ -1,11 +1,10 @@
import discord
import datetime import datetime
import re
import typing import typing
import discord
from redbot.core import Config, checks, commands from redbot.core import app_commands, Config, checks, commands
from redbot.core.utils.chat_formatting import humanize_list
from redbot.core.bot import Red from redbot.core.bot import Red
from redbot.core.utils.chat_formatting import humanize_list
class Suggestions(commands.Cog): class Suggestions(commands.Cog):
@ -66,11 +65,13 @@ class Suggestions(commands.Cog):
context = super().format_help_for_context(ctx) context = super().format_help_for_context(ctx)
return f"{context}" return f"{context}"
@commands.command() @commands.hybrid_command()
@commands.guild_only() @commands.guild_only()
@checks.bot_has_permissions(add_reactions=True) @checks.bot_has_permissions(add_reactions=True)
async def suggest(self, ctx: commands.Context, *, suggestion: str): async def suggest_cmd(self, ctx: commands.Context, *, suggestion: str):
"""Suggest something.""" """Suggest something."""
if ctx.interaction is True:
await ctx.defer()
suggest_id = await self.config.guild(ctx.guild).suggest_id() suggest_id = await self.config.guild(ctx.guild).suggest_id()
if not suggest_id: if not suggest_id:
if not await self.config.toggle(): if not await self.config.toggle():
@ -207,6 +208,14 @@ class Suggestions(commands.Cog):
await old_msg.edit(content=content, embed=embed) await old_msg.edit(content=content, embed=embed)
await ctx.tick() await ctx.tick()
@app_commands.context_menu(name="Approve Suggestion")
async def approve_context(self, interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_modal(self.SuggestionApproveModal(self, message))
@app_commands.context_menu(name="Deny Suggestion")
async def deny_context(self, interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_modal(self.SuggestionDenyModal(self, message))
@checks.admin() @checks.admin()
@checks.bot_has_permissions( @checks.bot_has_permissions(
manage_channels=True, add_reactions=True, manage_messages=True manage_channels=True, add_reactions=True, manage_messages=True
@ -463,7 +472,7 @@ class Suggestions(commands.Cog):
approved = "Approved" if approve else "Denied" approved = "Approved" if approve else "Denied"
embed.title = f"Suggestion {approved} (#{suggestion_id})" embed.title = f"Suggestion {approved} (#{suggestion_id})"
footer = [f"{approved} by {self.check_discrim(author)} • ({author.id}", footer = [f"{approved} by {self.check_discrim(author)} • ({author.id})",
author.display_avatar.replace(format="png", size=512)] author.display_avatar.replace(format="png", size=512)]
embed.set_footer( embed.set_footer(
text=footer[0], text=footer[0],
@ -510,3 +519,131 @@ class Suggestions(commands.Cog):
True True
) )
await ctx.tick() await ctx.tick()
async def _interaction_finish_suggestion(self, interaction: discord.Interaction, message: discord.Message, approve: bool, reason: str = None):
embed = message.embeds
title = embed[0].title
if not title.startswith("Suggestion #"):
await interaction.response.send_message(content="This message is not a suggestion!", ephemeral=True)
return
numbers = re.findall(r'\d+', title)
suggestion_id = ''.join(numbers)
author = interaction.user
server = interaction.guild.id
old_channel = interaction.guild.get_channel(
await self.config.guild(interaction.guild).suggest_id()
)
if approve:
channel = interaction.guild.get_channel(
await self.config.guild(interaction.guild).approve_id()
)
else:
channel = interaction.guild.get_channel(
await self.config.guild(interaction.guild).denied_id()
)
msg_id = await self.config.custom("SUGGESTION", server, suggestion_id).msg_id()
if (
msg_id != 0
and await self.config.custom("SUGGESTION", server, suggestion_id).finished()
):
return await interaction.response.send_message(content="This suggestion has been finished already.", ephemeral=True)
try:
old_msg = await old_channel.fetch_message(msg_id)
except discord.NotFound:
return await interaction.response.send_message(content="Uh oh, message with this ID doesn't exist.", ephemeral=True)
if not old_msg:
return await interaction.response.send_message(content="Uh oh, message with this ID doesn't exist.", ephemeral=True)
embed = old_msg.embeds[0]
content = old_msg.content
approved = "Approved" if approve else "Denied"
embed.title = f"Suggestion {approved} (#{suggestion_id})"
footer = [f"{approved} by {self.check_discrim(author)} • ({author.id})",
author.display_avatar.replace(format="png", size=512)]
embed.set_footer(
text=footer[0],
icon_url=footer[1]
)
embed.add_field(
name="Results", value=await self._get_results(await commands.Context.from_interaction(interaction), old_msg), inline=False
)
if reason:
embed.add_field(name="Reason", value=reason, inline=False)
await self.config.custom("SUGGESTION", server, suggestion_id).reason.set(
True
)
await self.config.custom("SUGGESTION", server, suggestion_id).rtext.set(
reason
)
if channel:
if not await self.config.guild(interaction.guild).same():
if await self.config.guild(interaction.guild).delete_suggestion():
await old_msg.delete()
nmsg = await channel.send(content=content, embed=embed)
await self.config.custom(
"SUGGESTION", server, suggestion_id
).msg_id.set(nmsg.id)
else:
await old_msg.edit(content=content, embed=embed)
else:
if not await self.config.guild(interaction.guild).same():
if await self.config.guild(interaction.guild).delete_suggestion():
await old_msg.delete()
await self.config.custom(
"SUGGESTION", server, suggestion_id
).msg_id.set(1)
else:
await old_msg.edit(content=content, embed=embed)
await self.config.custom("SUGGESTION", server, suggestion_id).finished.set(True)
if approve:
await self.config.custom("SUGGESTION", server, suggestion_id).approved.set(
True
)
else:
await self.config.custom("SUGGESTION", server, suggestion_id).denied.set(
True
)
class SuggestionApproveModal(discord.ui.Modal, title="Approving suggestion..."):
def __init__(self, cog_instance, message):
super().__init__()
self.cog_instance = cog_instance
self.message = message
reason = discord.ui.TextInput(
label="Approval Reason",
placeholder="Why are you approving this suggestion?",
style=discord.TextStyle.paragraph,
required=False,
max_length=1024
)
async def on_submit(self, interaction: discord.Interaction):
if self.reason.value != "":
await self.cog_instance.interaction_finish_suggestion(interaction, self.message, True, self.reason.value)
else:
await self.cog_instance.interaction_finish_suggestion(interaction, self.message, True, None)
await interaction.response.send_message(content="Suggestion approved!", ephemeral=True)
class SuggestionDenyModal(discord.ui.Modal, title="Denying suggestion..."):
def __init__(self, cog_instance, message):
super().__init__()
self.cog_instance = cog_instance
self.message = message
reason = discord.ui.TextInput(
label="Denial Reason",
placeholder="Why are you denying this suggestion?",
style=discord.TextStyle.paragraph,
required=False,
max_length=1024
)
async def on_submit(self, interaction: discord.Interaction):
if self.reason.value != "":
await self.cog_instance.interaction_finish_suggestion(interaction, self.message, False, self.reason.value)
else:
await self.cog_instance.interaction_finish_suggestion(interaction, self.message, False, None)
await interaction.response.send_message(content="Suggestion denied!", ephemeral=True)