Skip to content

Commit

Permalink
Add modals and bring back new lines (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
eibex authored Feb 16, 2022
1 parent 4cfbfed commit 3252af4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.2
3.0.3
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Reaction Light - Changelog
### 3.0.3
- Allow (once again) new lines via modals ([#101](https://github.com/eibex/reaction-light/issues/101) closed by [#103](https://github.com/eibex/reaction-light/pull/103) by [eibex](https://github.com/eibex))
- Bump disnake requirement to v2.4.0 (necessary to update disnake manually)


### 3.0.2
- Fix `/bot version`
- Bump disnake requirement to v2.3.2 (necessary to update disnake manually)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Reaction Light - Discord Role Bot
[![Reaction Light Discord Server](https://img.shields.io/discord/914952998109716531?color=5865f2&logo=discord&logoColor=ffffff)](https://discord.gg/cqxZQkhhHm)
[![Reaction Light 3.0.2](https://img.shields.io/badge/Reaction%20Light-3.0.2-yellow.svg)](https://github.com/eibex/reaction-light/blob/master/CHANGELOG.md)
[![Reaction Light 3.0.3](https://img.shields.io/badge/Reaction%20Light-3.0.3-yellow.svg)](https://github.com/eibex/reaction-light/blob/master/CHANGELOG.md)
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](#)
[![disnake 2.3.2+](https://img.shields.io/badge/disnake-2.3.2+-blue.svg)](#)
[![disnake 2.4.0+](https://img.shields.io/badge/disnake-2.4.0+-blue.svg)](#)

![Reaction Light Embed Example](https://i.imgur.com/f4b9Qye.png)

Expand Down
84 changes: 58 additions & 26 deletions cogs/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,28 +340,24 @@ async def edit_selector(
self,
inter,
channel: disnake.TextChannel = commands.Param(description=response.get("message-edit-option-channel")),
number: int = commands.Param(description=response.get("message-edit-option-number")),
message: str = commands.Param(description=response.get("message-edit-option-message"), default="none"),
title: str = commands.Param(description=response.get("message-edit-option-title"), default="none"),
description: str = commands.Param(description=response.get("message-edit-option-description"), default="none"),
number: int = commands.Param(description=response.get("message-edit-option-number"))
):
if not self.bot.isadmin(inter.author, inter.guild.id):
await inter.send(response.get("not-admin"))
return

await inter.response.defer()
all_messages = await self.formatted_channel_list(channel)
if number == 0:
if len(all_messages) == 1:
await inter.edit_original_message(content=response.get("edit-reactionrole-one").format(channel_name=channel.name))
await inter.send(content=response.get("edit-reactionrole-one").format(channel_name=channel.name))
elif len(all_messages) > 1:
await inter.edit_original_message(
await inter.send(
content=response.get("edit-reactionrole-instructions").format(
num_messages=len(all_messages), channel_name=channel.name, message_list="\n".join(all_messages)
)
)
else:
await inter.edit_original_message(content=response.get("no-reactionrole-messages"))
await inter.send(content=response.get("no-reactionrole-messages"))
else:
try:
# Tries to edit the reaction-role message
Expand All @@ -371,9 +367,6 @@ async def edit_selector(
except DatabaseError as error:
await self.bot.report(response.get("db-error-fetching-messages").format(message_ids=error), inter.guild.id)
return
message = message if message.lower() != "none" else None
title = title if title.lower() != "none" else None
description = description if description.lower() != "none" else None
counter = 1
if all_messages:
message_to_edit_id = None
Expand All @@ -394,40 +387,79 @@ async def edit_selector(
await inter.send(response.get("select-valid-reactionrole"))
return
await old_msg.edit(suppress=False)
selector_msg_new_body = message

await inter.response.send_modal(
title=response.get("modal-edit-title"),
custom_id=("edit_reactionrole"),
components=[
disnake.ui.TextInput(
label=response.get("message-edit-option-message"),
required=False,
custom_id="message",
style=disnake.TextInputStyle.paragraph
),
disnake.ui.TextInput(
label=response.get("message-edit-option-title"),
required=False,
custom_id="title",
style=disnake.TextInputStyle.paragraph
),
disnake.ui.TextInput(
label=response.get("message-edit-option-description"),
required=False,
custom_id="description",
style=disnake.TextInputStyle.paragraph
),
]
)

try:
selector_modal_inter: disnake.ModalInteraction = await self.bot.wait_for("modal_submit",
check=lambda i: i.custom_id == "edit_reactionrole" and i.author.id == inter.author.id,
timeout=300,
)
except asyncio.TimeoutError:
# The user didn't submit the modal in the specified period of time.
# This is done since Discord doesn't dispatch any event for when a modal is closed/dismissed.
return

await selector_modal_inter.response.defer()
selector_embed = disnake.Embed()
selector_msg_new_body = None
for custom_id, value in selector_modal_inter.text_values.items():
if custom_id == "title" and value:
selector_embed.title = value
selector_embed.colour = self.bot.config.botcolour
selector_embed.set_footer(text=f"{self.bot.config.botname}", icon_url=self.bot.config.logo)

if title:
selector_embed.title = title
selector_embed.colour = self.bot.config.botcolour
selector_embed.set_footer(text=f"{self.bot.config.botname}", icon_url=self.bot.config.logo)
elif custom_id == "description" and value:
selector_embed.description = value
selector_embed.colour = self.bot.config.botcolour
selector_embed.set_footer(text=f"{self.bot.config.botname}", icon_url=self.bot.config.logo)

if description:
selector_embed.description = description
selector_embed.colour = self.bot.config.botcolour
selector_embed.set_footer(text=f"{self.bot.config.botname}", icon_url=self.bot.config.logo)
elif custom_id == "message" and value:
selector_msg_new_body = value

try:

if selector_embed.title or selector_embed.description:
await old_msg.edit(content=selector_msg_new_body, embed=selector_embed)
else:
await old_msg.edit(content=selector_msg_new_body, embed=None)

await inter.edit_original_message(content=response.get("message-edited"))
await selector_modal_inter.edit_original_message(content=response.get("message-edited"))
except disnake.Forbidden:
await inter.edit_original_message(content=response.get("other-author-error"))
await selector_modal_inter.edit_original_message(content=response.get("other-author-error"))
return
except disnake.HTTPException as e:
if e.code == 50006:
await inter.edit_original_message(content=response.get("empty-message-error"))
await selector_modal_inter.edit_original_message(content=response.get("empty-message-error"))
else:
guild_id = inter.guild.id
await self.bot.report(str(e), guild_id)
except IndexError:
await inter.edit_original_message(content=response.get("invalid-target-channel"))
await inter.send(content=response.get("invalid-target-channel"))
except disnake.Forbidden:
await inter.edit_original_message(content=response.get("edit-permission-error"))
await inter.send(content=response.get("edit-permission-error"))

@message_group.sub_command(name="reaction", description=response.get("brief-message-reaction"))
async def edit_reaction(
Expand Down
7 changes: 4 additions & 3 deletions files/i18n/en-gb.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@
"brief-update": "Updates the bot",
"message-edit-option-channel": "The channel in which the message you want to edit is located",
"message-edit-option-number": "The number of the message in the channel (enter 0 for explanation)",
"message-edit-option-message": "The message of the reaction-role message (enter 'none' to skip)",
"message-edit-option-title": "The title of the reaction-role message (enter 'none' to skip)",
"message-edit-option-description": "The description of the reaction-role message (enter 'none' to skip)",
"message-edit-option-message": "The message of the reaction-role (optional)",
"message-edit-option-title": "The title of the embed (optional)",
"message-edit-option-description": "The description of the embed (optional)",
"modal-edit-title": "Message Editing",
"message-reaction-option-channel": "The channel in which the message you want to add/remove the reaction is located",
"message-reaction-option-action": "Use 'add' or 'remove' to add/remove a reaction to an existing message",
"message-reaction-option-number": "The number of the message in the channel (enter 0 for explanation)",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
disnake>=2.3.2
disnake>=2.4.0

0 comments on commit 3252af4

Please sign in to comment.