fix editing

This commit is contained in:
mudkipdev
2023-09-29 18:35:01 -06:00
parent f91debeb90
commit d0a938a8a6
3 changed files with 74 additions and 48 deletions

View File

@@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "scnewsbot" name = "scnewsbot"
version = "1.0.1" version = "1.0.2"
description = "A bot for the r/starcitizen Discord server." description = "A bot for the r/starcitizen Discord server."
authors = ["mudkipdev <mudkip@mudkip.dev>"] authors = ["mudkipdev <mudkip@mudkip.dev>"]
license = "MIT" license = "MIT"

View File

@@ -3,7 +3,7 @@ from discord.ext import commands
import discord import discord
from utils import Config from utils import Config
VERSION = "1.0.1" VERSION = "1.0.2"
INTENTS = discord.Intents.default() INTENTS = discord.Intents.default()
INTENTS.message_content = True INTENTS.message_content = True
INTENTS.members = True INTENTS.members = True

View File

@@ -25,13 +25,35 @@ async def get_follow_up_message(
) -> Optional[discord.Message]: ) -> Optional[discord.Message]:
index = 0 index = 0
async for message in announcement.channel.history(after=announcement.created_at): if limit == 0:
return announcement
elif limit < 0:
async for message in announcement.channel.history(
before=announcement.created_at
):
index -= 1
if message.author.id == announcement._state.user.id and index == limit:
return message
elif limit > 0:
async for message in announcement.channel.history(
after=announcement.created_at
):
index += 1 index += 1
if message.author.id == announcement._state.user.id and index == limit: if message.author.id == announcement._state.user.id and index == limit:
return message return message
def is_announcement(announcement: discord.Message, /) -> bool:
return (
announcement.author == announcement.guild.me
and len(announcement.embeds) == 1
and not announcement.content
and not announcement.components
)
def reformat_description(description: str) -> str: def reformat_description(description: str) -> str:
split_description = description.split("\n") split_description = description.split("\n")
@@ -97,11 +119,12 @@ class AnnouncementCog(commands.Cog, name="Announcements"):
@commands.check(can_publish_announcements) @commands.check(can_publish_announcements)
@announcements.command(brief="Deletes an announcement.") @announcements.command(brief="Deletes an announcement.")
async def delete(self, ctx: commands.Context, message: discord.Message) -> None: async def delete(self, ctx: commands.Context, message: discord.Message) -> None:
if message.author != self.bot.user and not message.embeds: if not is_announcement(message):
await ctx.reply("That is not an announcement.") await ctx.reply("That is not an announcement!")
return
video_message = await get_follow_up_message(message, limit=1) video_message = await get_follow_up_message(message, limit=-1)
ping_message = await get_follow_up_message(message, limit=2) ping_message = await get_follow_up_message(message, limit=1)
if video_message: if video_message:
await video_message.delete() await video_message.delete()
@@ -227,30 +250,27 @@ class Announcement:
async def from_message( async def from_message(
cls, message: discord.Message, /, *, bot: commands.Bot cls, message: discord.Message, /, *, bot: commands.Bot
) -> Any: ) -> Any:
if message.author != message.guild.me: if not is_announcement(message):
raise InvalidAnnouncementException("This is not a bot message.") raise InvalidAnnouncementException("That is not an announcement!")
return
if not len(message.embeds) == 1:
raise InvalidAnnouncementException("This message does not have any embeds.")
return
if message.components:
raise InvalidAnnouncementException("This message has not been posted yet.")
return return
embed = message.embeds[0] embed = message.embeds[0]
author = None
if embed.footer.text:
parsed_footer = embed.footer.text.replace("This post was written by ", "")
author = discord.utils.find( author = discord.utils.find(
lambda member: str(member) == embed.author.name, message.guild.members lambda member: str(member) == parsed_footer, message.guild.members
) )
ping = None ping = None
ping_preview = None ping_preview = None
ping_message = await get_follow_up_message(message, limit=2) ping_message = await get_follow_up_message(message, limit=1)
if ping_message: if ping_message:
role_converter = commands.RoleConverter()
split_message = ping_message.content.split(" - ") split_message = ping_message.content.split(" - ")
ping = await commands.RoleConverter.convert( ping = await role_converter.convert(
await bot.get_context(ping_message), split_message[0] await bot.get_context(ping_message), split_message[0]
) )
@@ -259,7 +279,7 @@ class Announcement:
url = None url = None
description = embed.description description = embed.description
video_message = await get_follow_up_message(message, limit=1) video_message = await get_follow_up_message(message, limit=-1)
if embed.description is not None: if embed.description is not None:
if len(embed.description.split("\n\n")) > 0: if len(embed.description.split("\n\n")) > 0:
@@ -309,6 +329,13 @@ class AnnouncementBuilder:
self.add_option(Option(id="ping_preview", name="Ping Preview", row=1)) self.add_option(Option(id="ping_preview", name="Ping Preview", row=1))
for item in _items: for item in _items:
if (
isinstance(item, discord.ui.Button)
and self.edit
and item.label == "Post"
):
item.label = "Edit"
self.view.add_item(item) self.view.add_item(item)
async def get_embed(self, bot: commands.Bot) -> discord.Embed: async def get_embed(self, bot: commands.Bot) -> discord.Embed:
@@ -327,10 +354,10 @@ class AnnouncementBuilderView(discord.ui.View):
super().__init__(timeout=ANNOUNCEMENT_BUILDER_TIMEOUT) super().__init__(timeout=ANNOUNCEMENT_BUILDER_TIMEOUT)
self.announcement_builder = announcement_builder self.announcement_builder = announcement_builder
def _has_permission(self, user: discord.User) -> bool: def has_permission(self, user: discord.User) -> bool:
return user == self.announcement_builder.owner return user == self.announcement_builder.owner
async def _update(self, interaction: discord.Interaction, /) -> None: async def update(self, interaction: discord.Interaction, /) -> None:
if ( if (
self.announcement_builder.announcement.channel self.announcement_builder.announcement.channel
and self.announcement_builder.announcement.channel.type and self.announcement_builder.announcement.channel.type
@@ -352,7 +379,7 @@ class AnnouncementBuilderView(discord.ui.View):
async def button_callback( async def button_callback(
self, interaction: discord.Interaction, button: discord.ui.Button self, interaction: discord.Interaction, button: discord.ui.Button
) -> None: ) -> None:
if not self._has_permission(interaction.user): if not self.has_permission(interaction.user):
await interaction.response.send_message( await interaction.response.send_message(
"You cannot use this menu.", ephemeral=True "You cannot use this menu.", ephemeral=True
) )
@@ -373,7 +400,7 @@ class AnnouncementBuilderView(discord.ui.View):
async def cancel( async def cancel(
self, interaction: discord.Interaction, button: discord.ui.Button self, interaction: discord.Interaction, button: discord.ui.Button
) -> None: ) -> None:
if not self._has_permission(interaction.user): if not self.has_permission(interaction.user):
await interaction.response.send_message( await interaction.response.send_message(
"You cannot use this menu.", ephemeral=True "You cannot use this menu.", ephemeral=True
) )
@@ -388,7 +415,7 @@ class AnnouncementBuilderView(discord.ui.View):
async def toggle_anonymous( async def toggle_anonymous(
self, interaction: discord.Interaction, button: discord.ui.Button self, interaction: discord.Interaction, button: discord.ui.Button
) -> None: ) -> None:
if not self._has_permission(interaction.user): if not self.has_permission(interaction.user):
await interaction.response.send_message( await interaction.response.send_message(
"You cannot use this menu.", ephemeral=True "You cannot use this menu.", ephemeral=True
) )
@@ -402,7 +429,7 @@ class AnnouncementBuilderView(discord.ui.View):
else: else:
button.style = discord.ButtonStyle.gray button.style = discord.ButtonStyle.gray
await self._update(interaction) await self.update(interaction)
@discord.ui.button( @discord.ui.button(
custom_id="notification", label="Published?", row=2, disabled=True custom_id="notification", label="Published?", row=2, disabled=True
@@ -410,7 +437,7 @@ class AnnouncementBuilderView(discord.ui.View):
async def toggle_notification( async def toggle_notification(
self, interaction: discord.Interaction, button: discord.ui.Button self, interaction: discord.Interaction, button: discord.ui.Button
) -> None: ) -> None:
if not self._has_permission(interaction.user): if not self.has_permission(interaction.user):
await interaction.response.send_message( await interaction.response.send_message(
"You cannot use this menu.", ephemeral=True "You cannot use this menu.", ephemeral=True
) )
@@ -424,7 +451,7 @@ class AnnouncementBuilderView(discord.ui.View):
else: else:
button.style = discord.ButtonStyle.gray button.style = discord.ButtonStyle.gray
await self._update(interaction) await self.update(interaction)
@discord.ui.button( @discord.ui.button(
custom_id="publish", label="Post", style=discord.ButtonStyle.blurple, row=2 custom_id="publish", label="Post", style=discord.ButtonStyle.blurple, row=2
@@ -432,7 +459,7 @@ class AnnouncementBuilderView(discord.ui.View):
async def publish( async def publish(
self, interaction: discord.Interaction, button: discord.ui.Button self, interaction: discord.Interaction, button: discord.ui.Button
) -> None: ) -> None:
if not self._has_permission(interaction.user): if not self.has_permission(interaction.user):
await interaction.response.send_message( await interaction.response.send_message(
"You cannot use this menu.", ephemeral=True "You cannot use this menu.", ephemeral=True
) )
@@ -455,7 +482,7 @@ class AnnouncementBuilderView(discord.ui.View):
await self.announcement_builder.message.edit(embed=embed) await self.announcement_builder.message.edit(embed=embed)
video_message = await get_follow_up_message( video_message = await get_follow_up_message(
self.announcement_builder.message, limit=1 self.announcement_builder.message, limit=-1
) )
await video_message.edit( await video_message.edit(
content=self.announcement_builder.announcement.video_url content=self.announcement_builder.announcement.video_url
@@ -483,22 +510,21 @@ class AnnouncementBuilderView(discord.ui.View):
bot = interaction.client bot = interaction.client
video_message = None video_message = None
message = await announcement.channel.send(
embed=await announcement.get_embed(bot=bot),
)
await message.add_reaction(ANNOUNCEMENT_EMOJI)
if announcement.video_url: if announcement.video_url:
video_message = await announcement.channel.send(announcement.video_url) video_message = await announcement.channel.send(announcement.video_url)
message = await announcement.channel.send(
embed=await announcement.get_embed(bot=bot),
)
await message.add_reaction(ANNOUNCEMENT_EMOJI)
for channel_id in bot.config.repost_channels: for channel_id in bot.config.repost_channels:
repost_channel = await bot.fetch_channel(channel_id) repost_channel = await bot.fetch_channel(channel_id)
await repost_channel.send(announcement.video_url)
await repost_channel.send( await repost_channel.send(
embed=await announcement.get_embed(bot=bot, show_author=True) embed=await announcement.get_embed(bot=bot, show_author=True)
) )
await repost_channel.send(announcement.video_url) await repost_channel.send(message.jump_url)
await repost_channel.send(f"(posted in {announcement.channel.mention})")
if announcement.ping and announcement.ping_preview: if announcement.ping and announcement.ping_preview:
await announcement.channel.send( await announcement.channel.send(
@@ -512,10 +538,10 @@ class AnnouncementBuilderView(discord.ui.View):
if announcement.will_notify: if announcement.will_notify:
try: try:
await message.publish()
if video_message: if video_message:
await video_message.publish() await video_message.publish()
await message.publish()
except discord.Forbidden: except discord.Forbidden:
pass pass
@@ -550,10 +576,10 @@ class ChangeOptionModal(discord.ui.Modal):
if conversion_success and not option_value: if conversion_success and not option_value:
item.style = discord.ButtonStyle.gray item.style = discord.ButtonStyle.gray
await self.announcement_builder.view._update(interaction) await self.announcement_builder.view.update(interaction)
elif conversion_success: elif conversion_success:
item.style = discord.ButtonStyle.green item.style = discord.ButtonStyle.green
await self.announcement_builder.view._update(interaction) await self.announcement_builder.view.update(interaction)
else: else:
await interaction.response.send_message( await interaction.response.send_message(
"Could not find that role or channel.", ephemeral=True "Could not find that role or channel.", ephemeral=True