Skip to content

Commit be8938a

Browse files
committed
add: edit post app command and modal
1 parent d7d1785 commit be8938a

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/cogs/admin/edit_post.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from discord import Interaction, Message, NotFound
2+
from discord.app_commands import command
3+
from discord.app_commands.commands import describe
4+
from discord.ext.commands.bot import Bot
5+
from discord.ext.commands.cog import Cog, GroupCog
6+
7+
from data.admin.config_auto import Config
8+
from ui.modals.edit_post import EditPostModal
9+
from utils.decorators import is_staff
10+
11+
12+
class EditPost(Cog):
13+
def __init__(self, bot: Bot):
14+
self.bot = bot
15+
self.config = Config(self.bot.pool)
16+
17+
@is_staff()
18+
@command(
19+
name="post-edit",
20+
description="A command to allow staff to edit an existing post created by the PPH bot",
21+
)
22+
@describe(message_id="The ID post/message to edit")
23+
async def edit_post(self, interaction: Interaction, message_id: str):
24+
25+
if not interaction.channel_id:
26+
return
27+
28+
try:
29+
message = await interaction.channel.fetch_message(int(message_id))
30+
except (ValueError, NotFound):
31+
await interaction.response.send_message(
32+
"Message not found in this channel.", ephemeral=True
33+
)
34+
return
35+
36+
if self.bot.user is not None and message.author.id != self.bot.user.id:
37+
await interaction.response.send_message(
38+
"Cannot edit message that isn't from PPH bot!", ephemeral=True
39+
)
40+
return
41+
42+
post_id = message.id
43+
content = message.content
44+
edit_post_modal = EditPostModal(
45+
channel_id=interaction.channel_id,
46+
post_id=post_id,
47+
original_content=content,
48+
bot=self.bot,
49+
)
50+
51+
await interaction.response.send_modal(edit_post_modal)
52+
53+
54+
async def setup(bot: Bot):
55+
await bot.add_cog(EditPost(bot))

src/ui/modals/edit_post.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from discord import CategoryChannel, ForumChannel, Interaction
2+
from discord.enums import TextStyle
3+
from discord.ext.commands.bot import Bot
4+
from discord.ui.modal import Modal
5+
from discord.ui.text_input import TextInput
6+
7+
8+
class EditPostModal(Modal, title="Edit post"):
9+
message = TextInput(
10+
label="Content",
11+
placeholder="...",
12+
required=True,
13+
style=TextStyle.long,
14+
max_length=4000,
15+
)
16+
17+
def __init__(self, channel_id: int, post_id: int, original_content: str, bot: Bot):
18+
super().__init__()
19+
20+
self.bot = bot
21+
self.channel_id = channel_id
22+
self.post_id = post_id
23+
self.message.default = original_content
24+
25+
async def on_submit(self, interaction: Interaction, /) -> None:
26+
channel = await self.bot.fetch_channel(self.channel_id)
27+
message = await channel.fetch_message(self.post_id)
28+
await message.edit(content=self.message.value)
29+
await interaction.response.send_message("Post edited.", ephemeral=True)

0 commit comments

Comments
 (0)