Skip to content

Commit

Permalink
confirm_thread_creation Buttons instead of reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbndr committed Apr 3, 2023
1 parent 11e094c commit f5f25b5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
53 changes: 19 additions & 34 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 @@ -1416,30 +1419,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,
)
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 == None:
thread.cancelled = True
self.bot.loop.create_task(
destination.send(
Expand All @@ -1450,23 +1442,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 == 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
32 changes: 32 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,32 @@ 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

0 comments on commit f5f25b5

Please sign in to comment.