Skip to content

Commit

Permalink
confirm_thread_creation Buttons instead of reactions (#3273)
Browse files Browse the repository at this point in the history
* Reminder Plugin

Created a reminder plugin

* Fix indentations

* confirm_thread_creation Buttons instead of reactions

* Changelog+Small fixes

* Updated the react to confirm message, and removed changelog entry

* Code linting with black

* Update changelog

---------

Co-authored-by: Taku <45324516+Taaku18@users.noreply.github.com>
  • Loading branch information
martinbndr and Taaku18 authored Jul 15, 2023
1 parent 1adbacf commit 77fbb69
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ however, insignificant breaking changes do not guarantee a major version bump, s
- Repo moved to https://github.com/modmail-dev/modmail.
- Guild icons in embed footers and author urls now have a fixed size of 128. ([PR #3261](https://github.com/modmail-dev/modmail/pull/3261))
- Discord.py internal logging is now enabled by default. ([PR #3216](https://github.com/modmail-dev/Modmail/pull/3216))
- The confirm-thread-creation dialog now uses buttons instead of reactions. ([PR #3273](https://github.com/modmail-dev/Modmail/pull/3273))

### Internal
- Renamed `Bot.log_file_name` to `Bot.log_file_path`. Log files are now created at `temp/logs/modmail.log`. ([PR #3216](https://github.com/modmail-dev/Modmail/pull/3216))
Expand Down
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

0 comments on commit 77fbb69

Please sign in to comment.