Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

confirm_thread_creation Buttons instead of reactions #3273

Merged
merged 11 commits into from
Jul 15, 2023
2 changes: 1 addition & 1 deletion core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class ConfigManager:
# confirm thread creation
"confirm_thread_creation": False,
"confirm_thread_creation_title": "Confirm thread creation",
"confirm_thread_response": "React to confirm thread creation which will directly contact the moderators",
"confirm_thread_response": "Click the button to confirm thread creation which will directly contact the moderators.",
"confirm_thread_creation_accept": "\N{WHITE HEAVY CHECK MARK}",
"confirm_thread_creation_deny": "\N{NO ENTRY SIGN}",
# regex
Expand Down
4 changes: 2 additions & 2 deletions core/config_help.json
Original file line number Diff line number Diff line change
Expand Up @@ -1014,10 +1014,10 @@
]
},
"confirm_thread_response": {
"default": "React to confirm thread creation which will directly contact the moderators",
"default": "Click the button to confirm thread creation which will directly contact the moderators.",
"description": "Description for the embed message sent to users to confirm a thread creation",
"examples":[
"`{prefix}config set confirm_thread_response React to confirm`"
"`{prefix}config set confirm_thread_response Click to confirm`"
],
"notes": [
"See also: `confirm_thread_creation`, `confirm_thread_creation_title`, `confirm_thread_creation_accept`, `confirm_thread_creation_deny`"
Expand Down
51 changes: 18 additions & 33 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
get_top_role,
create_thread_channel,
get_joint_id,
AcceptButton,
DenyButton,
ConfirmThreadCreationView,
)

logger = getLogger(__name__)
Expand Down Expand Up @@ -1418,30 +1421,19 @@ async def create(
destination = recipient
else:
destination = message.channel
view = ConfirmThreadCreationView()
view.add_item(AcceptButton(self.bot.config["confirm_thread_creation_accept"]))
view.add_item(DenyButton(self.bot.config["confirm_thread_creation_deny"]))
confirm = await destination.send(
embed=discord.Embed(
title=self.bot.config["confirm_thread_creation_title"],
description=self.bot.config["confirm_thread_response"],
color=self.bot.main_color,
)
),
view=view,
)
accept_emoji = self.bot.config["confirm_thread_creation_accept"]
deny_emoji = self.bot.config["confirm_thread_creation_deny"]
emojis = [accept_emoji, deny_emoji]
for emoji in emojis:
await confirm.add_reaction(emoji)
await asyncio.sleep(0.2)

try:
r, _ = await self.bot.wait_for(
"reaction_add",
check=lambda r, u: u.id == recipient.id
and r.message.id == confirm.id
and r.message.channel.id == confirm.channel.id
and str(r.emoji) in (accept_emoji, deny_emoji),
timeout=20,
)
except asyncio.TimeoutError:
await view.wait()
if view.value is None:
thread.cancelled = True
self.bot.loop.create_task(
destination.send(
Expand All @@ -1452,23 +1444,16 @@ async def create(
)
)
)
else:
if str(r.emoji) == deny_emoji:
thread.cancelled = True
self.bot.loop.create_task(
destination.send(
embed=discord.Embed(
title=self.bot.config["thread_cancelled"], color=self.bot.error_color
)
await confirm.edit(view=None)
if view.value is False:
thread.cancelled = True
self.bot.loop.create_task(
destination.send(
embed=discord.Embed(
title=self.bot.config["thread_cancelled"], color=self.bot.error_color
)
)

async def remove_reactions():
for emoji in emojis:
await confirm.remove_reaction(emoji, self.bot.user)
await asyncio.sleep(0.2)

self.bot.loop.create_task(remove_reactions())
)
if thread.cancelled:
del self.cache[recipient.id]
return thread
Expand Down
29 changes: 29 additions & 0 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"get_top_role",
"get_joint_id",
"extract_block_timestamp",
"AcceptButton",
"DenyButton",
"ConfirmThreadCreationView",
]


Expand Down Expand Up @@ -559,3 +562,29 @@ def extract_block_timestamp(reason, id_):
raise

return end_time, after


class AcceptButton(discord.ui.Button):
def __init__(self, emoji):
super().__init__(style=discord.ButtonStyle.gray, emoji=emoji)

async def callback(self, interaction: discord.Interaction):
self.view.value = True
await interaction.response.edit_message(view=None)
self.view.stop()


class DenyButton(discord.ui.Button):
def __init__(self, emoji):
super().__init__(style=discord.ButtonStyle.gray, emoji=emoji)

async def callback(self, interaction: discord.Interaction):
self.view.value = False
await interaction.response.edit_message(view=None)
self.view.stop()


class ConfirmThreadCreationView(discord.ui.View):
def __init__(self):
super().__init__(timeout=20)
self.value = None