diff --git a/bot/cogs/sticky.py b/bot/cogs/sticky.py index 03c8e2c..f43ea62 100644 --- a/bot/cogs/sticky.py +++ b/bot/cogs/sticky.py @@ -30,6 +30,7 @@ async def on_connect(self) -> None: data["message_id"], data["message"], data["delay_time"], + data["ignore_self"], ] @commands.Cog.listener() @@ -38,10 +39,11 @@ async def on_message(self, message: discord.Message) -> None: if message.channel.id in self.sticky_data: res = self.sticky_data[message.channel.id] - if res and message.author != self.bot.user: + if res and (message.author != self.bot.user or not res[3]): sticky_message_id = res[0] sticky_message = res[1] delay_time = res[2] + ignore_self = res[3] try: sticky = await message.channel.fetch_message(sticky_message_id) except discord.errors.NotFound: @@ -58,7 +60,12 @@ async def on_message(self, message: discord.Message) -> None: msg.id, ) - self.sticky_data[message.channel.id] = [msg.id, sticky_message, delay_time] + self.sticky_data[message.channel.id] = [ + msg.id, + sticky_message, + delay_time, + ignore_self, + ] @app_commands.command(name="list", description="List channel with sticky message.") async def list_sticky_messages(self, interaction: Interaction) -> None: @@ -75,6 +82,7 @@ async def list_sticky_messages(self, interaction: Interaction) -> None: message="Sticky message.", channel="Target channel.", delay_time="Delay after new message is sent on a channel (in seconds). Default is 2 seconds.", + ignore_self="Ignore messages sent by this bot.", ) @app_guard( manage_channel=True, @@ -85,6 +93,7 @@ async def add_sticky_message( message: app_commands.Range[str, 0, 2000], channel: discord.TextChannel | discord.Thread, delay_time: app_commands.Range[int, 2, 1800] | None, + ignore_self: bool = True, # noqa: FBT002 ) -> None: await interaction.response.defer() if interaction.guild is None: @@ -116,20 +125,22 @@ async def add_sticky_message( async with self.db_pool.acquire() as conn: await conn.execute( - "INSERT INTO sticky (channel_id,message_id,message,delay_time) VALUES ($1,$2,$3,$4);", + "INSERT INTO sticky (channel_id,message_id,message,delay_time,ignore_self) VALUES ($1,$2,$3,$4,$5);", channel.id, msg.id, message, delay_time, + ignore_self, ) - self.sticky_data[channel.id] = [msg.id, message, delay_time] + self.sticky_data[channel.id] = [msg.id, message, delay_time, ignore_self] logger.info( "NEW STICKY MESSSAGE HAS BEEN ADDED", extra={ "channel_id": channel.id, "msgs": message, "delay_time": delay_time, + "ignore_self": ignore_self, }, ) @@ -149,6 +160,7 @@ async def add_sticky_message( message="New sticky message.", channel="Channel name.", delay_time="New delay time after new message is sent on a channel (in seconds).", + ignore_self="Ignore messages sent by this bot.", ) @app_guard( manage_channel=True, @@ -159,6 +171,7 @@ async def edit_sticky_message( message: app_commands.Range[str, 0, 2000], channel: discord.TextChannel | discord.Thread, delay_time: app_commands.Range[int, 2, 1800] | None, + ignore_self: bool = True, # noqa: FBT002 ) -> None: await interaction.response.defer() if interaction.guild is None: @@ -197,13 +210,19 @@ async def edit_sticky_message( async with self.db_pool.acquire() as conn: await conn.execute( - "UPDATE sticky SET message=$2, delay_time=$3 WHERE channel_id=$1;", + "UPDATE sticky SET message=$2, delay_time=$3, ignore_self=$4 WHERE channel_id=$1;", channel.id, message, delay_time, + ignore_self, ) - self.sticky_data[channel.id] = [sticky_data.id, message, delay_time] + self.sticky_data[channel.id] = [ + sticky_data.id, + message, + delay_time, + ignore_self, + ] return await self._send_interaction( interaction, @@ -332,6 +351,7 @@ async def resend_sticky_message( msg.id, data["message"], data["delay_time"], + data["ignore_self"], ] return await self._send_interaction( diff --git a/bot/data/db.sql b/bot/data/db.sql index 41844b7..c70457e 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -7,6 +7,7 @@ CREATE TABLE sticky( message_id BIGINT NOT NULL, message TEXT NOT NULL, delay_time INT DEFAULT 2 NOT NULL, + ignore_self BOOLEAN DEFAULT TRUE NOT NULL, -- When TRUE, sticky messages won't be reposted for self messages PRIMARY KEY(channel_id), UNIQUE(channel_id), UNIQUE(message_id) @@ -21,7 +22,7 @@ CREATE TABLE scheduled_message( guild_id bigint NOT NULL, channel_id bigint NOT NULL, message text NOT NULL, - date_trigger TIMESTAMP WITH TIME ZONE NOT NULL, + date_trigger TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY(id), UNIQUE(id) );