From 4483c87659401a6f90c6868be640774e9a65b3b2 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Tue, 21 Oct 2025 09:21:12 +0700 Subject: [PATCH 1/6] add ignore bot flag --- bot/cogs/sticky.py | 19 ++++++++++++++----- bot/data/db.sql | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bot/cogs/sticky.py b/bot/cogs/sticky.py index 03c8e2c..790572d 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_bot"], ] @commands.Cog.listener() @@ -38,7 +39,7 @@ 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 and (not res[3] or not message.author.bot)): sticky_message_id = res[0] sticky_message = res[1] delay_time = res[2] @@ -75,6 +76,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_bot="Ignore messages sent by bots.", ) @app_guard( manage_channel=True, @@ -85,6 +87,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_bot: bool = True, ) -> None: await interaction.response.defer() if interaction.guild is None: @@ -116,20 +119,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_bot) VALUES ($1,$2,$3,$4,$5);", channel.id, msg.id, message, delay_time, + ignore_bot, ) - self.sticky_data[channel.id] = [msg.id, message, delay_time] + self.sticky_data[channel.id] = [msg.id, message, delay_time, ignore_bot] logger.info( "NEW STICKY MESSSAGE HAS BEEN ADDED", extra={ "channel_id": channel.id, "msgs": message, "delay_time": delay_time, + "ignore_bot": ignore_bot, }, ) @@ -149,6 +154,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_bot="Ignore messages sent by bots.", ) @app_guard( manage_channel=True, @@ -159,6 +165,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_bot: bool = True, ) -> None: await interaction.response.defer() if interaction.guild is None: @@ -197,13 +204,14 @@ 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_bot=$4 WHERE channel_id=$1;", channel.id, message, delay_time, + ignore_bot, ) - self.sticky_data[channel.id] = [sticky_data.id, message, delay_time] + self.sticky_data[channel.id] = [sticky_data.id, message, delay_time, ignore_bot] return await self._send_interaction( interaction, @@ -332,6 +340,7 @@ async def resend_sticky_message( msg.id, data["message"], data["delay_time"], + data["ignore_bot"] ] return await self._send_interaction( diff --git a/bot/data/db.sql b/bot/data/db.sql index 41844b7..134d4bd 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -22,6 +22,7 @@ CREATE TABLE scheduled_message( channel_id bigint NOT NULL, message text NOT NULL, date_trigger TIMESTAMP WITH TIME ZONE NOT NULL, + ignore_bot BOOLEAN DEFAULT TRUE NOT NULL, PRIMARY KEY(id), UNIQUE(id) ); From 670c2e48ac19d7866a5d740d9414ef3c4b8dba1f Mon Sep 17 00:00:00 2001 From: hilmoo Date: Tue, 21 Oct 2025 09:25:10 +0700 Subject: [PATCH 2/6] refactor bit --- bot/cogs/sticky.py | 42 ++++++++++++++++++++++-------------------- bot/data/db.sql | 3 ++- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/bot/cogs/sticky.py b/bot/cogs/sticky.py index 790572d..7185979 100644 --- a/bot/cogs/sticky.py +++ b/bot/cogs/sticky.py @@ -39,27 +39,29 @@ 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 and (not res[3] or not message.author.bot)): - sticky_message_id = res[0] - sticky_message = res[1] - delay_time = res[2] - try: - sticky = await message.channel.fetch_message(sticky_message_id) - except discord.errors.NotFound: - return + if res: + should_ignore_message = res[3] and message.author.bot + if message.author != self.bot.user and not should_ignore_message: + sticky_message_id = res[0] + sticky_message = res[1] + delay_time = res[2] + try: + sticky = await message.channel.fetch_message(sticky_message_id) + except discord.errors.NotFound: + return - await sticky.delete() - await asyncio.sleep(delay_time) - msg = await message.channel.send(sticky_message) + await sticky.delete() + await asyncio.sleep(delay_time) + msg = await message.channel.send(sticky_message) - async with self.db_pool.acquire() as conn: - await conn.execute( - "UPDATE sticky SET message_id=$2 WHERE channel_id=$1;", - message.channel.id, - msg.id, - ) + async with self.db_pool.acquire() as conn: + await conn.execute( + "UPDATE sticky SET message_id=$2 WHERE channel_id=$1;", + message.channel.id, + 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] @app_commands.command(name="list", description="List channel with sticky message.") async def list_sticky_messages(self, interaction: Interaction) -> None: @@ -87,7 +89,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_bot: bool = True, + ignore_bot: bool = True, # noqa: FBT002 ) -> None: await interaction.response.defer() if interaction.guild is None: @@ -165,7 +167,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_bot: bool = True, + ignore_bot: bool = True, # noqa: FBT002 ) -> None: await interaction.response.defer() if interaction.guild is None: diff --git a/bot/data/db.sql b/bot/data/db.sql index 134d4bd..197c5f8 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -21,7 +21,8 @@ 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, -- When TRUE, sticky messages won't be reposted for bot messages + -- When TRUE, sticky messages won't be reposted for bot messages ignore_bot BOOLEAN DEFAULT TRUE NOT NULL, PRIMARY KEY(id), UNIQUE(id) From d2b661b679561f5ed72c2057345078c35e6ddc24 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Tue, 21 Oct 2025 09:28:07 +0700 Subject: [PATCH 3/6] fix suggestion --- bot/cogs/sticky.py | 8 ++++++-- bot/data/db.sql | 5 ++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/bot/cogs/sticky.py b/bot/cogs/sticky.py index 7185979..8d3453f 100644 --- a/bot/cogs/sticky.py +++ b/bot/cogs/sticky.py @@ -61,7 +61,11 @@ 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, + ] @app_commands.command(name="list", description="List channel with sticky message.") async def list_sticky_messages(self, interaction: Interaction) -> None: @@ -342,7 +346,7 @@ async def resend_sticky_message( msg.id, data["message"], data["delay_time"], - data["ignore_bot"] + data["ignore_bot"], ] return await self._send_interaction( diff --git a/bot/data/db.sql b/bot/data/db.sql index 197c5f8..a921253 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -21,9 +21,8 @@ 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, -- When TRUE, sticky messages won't be reposted for bot messages - -- When TRUE, sticky messages won't be reposted for bot messages - ignore_bot BOOLEAN DEFAULT TRUE NOT NULL, + date_trigger TIMESTAMP WITH TIME ZONE NOT NULL, + ignore_bot BOOLEAN DEFAULT TRUE NOT NULL, -- When TRUE, sticky messages won't be reposted for bot messages PRIMARY KEY(id), UNIQUE(id) ); From ae9a601357dce38b88759752d614ac51f6e2a075 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Tue, 21 Oct 2025 09:36:55 +0700 Subject: [PATCH 4/6] Rename ignore_bot to ignore_self for clarity in sticky message handling --- bot/cogs/sticky.py | 77 ++++++++++++++++++++++++---------------------- bot/data/db.sql | 2 +- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/bot/cogs/sticky.py b/bot/cogs/sticky.py index 8d3453f..3563a53 100644 --- a/bot/cogs/sticky.py +++ b/bot/cogs/sticky.py @@ -30,7 +30,7 @@ async def on_connect(self) -> None: data["message_id"], data["message"], data["delay_time"], - data["ignore_bot"], + data["ignore_self"], ] @commands.Cog.listener() @@ -39,33 +39,31 @@ 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: - should_ignore_message = res[3] and message.author.bot - if message.author != self.bot.user and not should_ignore_message: - sticky_message_id = res[0] - sticky_message = res[1] - delay_time = res[2] - try: - sticky = await message.channel.fetch_message(sticky_message_id) - except discord.errors.NotFound: - return - - await sticky.delete() - await asyncio.sleep(delay_time) - msg = await message.channel.send(sticky_message) + if res and (message.author == self.bot.user and res[3]): + sticky_message_id = res[0] + sticky_message = res[1] + delay_time = res[2] + try: + sticky = await message.channel.fetch_message(sticky_message_id) + except discord.errors.NotFound: + return - async with self.db_pool.acquire() as conn: - await conn.execute( - "UPDATE sticky SET message_id=$2 WHERE channel_id=$1;", - message.channel.id, - msg.id, - ) + await sticky.delete() + await asyncio.sleep(delay_time) + msg = await message.channel.send(sticky_message) - self.sticky_data[message.channel.id] = [ + async with self.db_pool.acquire() as conn: + await conn.execute( + "UPDATE sticky SET message_id=$2 WHERE channel_id=$1;", + message.channel.id, msg.id, - sticky_message, - delay_time, - ] + ) + + self.sticky_data[message.channel.id] = [ + msg.id, + sticky_message, + delay_time, + ] @app_commands.command(name="list", description="List channel with sticky message.") async def list_sticky_messages(self, interaction: Interaction) -> None: @@ -82,7 +80,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_bot="Ignore messages sent by bots.", + ignore_self="Ignore messages sent by bots.", ) @app_guard( manage_channel=True, @@ -93,7 +91,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_bot: bool = True, # noqa: FBT002 + ignore_self: bool = True, # noqa: FBT002 ) -> None: await interaction.response.defer() if interaction.guild is None: @@ -125,22 +123,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,ignore_bot) VALUES ($1,$2,$3,$4,$5);", + "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_bot, + ignore_self, ) - self.sticky_data[channel.id] = [msg.id, message, delay_time, ignore_bot] + 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_bot": ignore_bot, + "ignore_self": ignore_self, }, ) @@ -160,7 +158,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_bot="Ignore messages sent by bots.", + ignore_self="Ignore messages sent by bots.", ) @app_guard( manage_channel=True, @@ -171,7 +169,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_bot: bool = True, # noqa: FBT002 + ignore_self: bool = True, # noqa: FBT002 ) -> None: await interaction.response.defer() if interaction.guild is None: @@ -210,14 +208,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, ignore_bot=$4 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_bot, + ignore_self, ) - self.sticky_data[channel.id] = [sticky_data.id, message, delay_time, ignore_bot] + self.sticky_data[channel.id] = [ + sticky_data.id, + message, + delay_time, + ignore_self, + ] return await self._send_interaction( interaction, @@ -346,7 +349,7 @@ async def resend_sticky_message( msg.id, data["message"], data["delay_time"], - data["ignore_bot"], + data["ignore_self"], ] return await self._send_interaction( diff --git a/bot/data/db.sql b/bot/data/db.sql index a921253..55023e2 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -22,7 +22,7 @@ CREATE TABLE scheduled_message( channel_id bigint NOT NULL, message text NOT NULL, date_trigger TIMESTAMP WITH TIME ZONE NOT NULL, - ignore_bot BOOLEAN DEFAULT TRUE NOT NULL, -- When TRUE, sticky messages won't be reposted for bot messages + ignore_self BOOLEAN DEFAULT TRUE NOT NULL, -- When TRUE, sticky messages won't be reposted for self messages PRIMARY KEY(id), UNIQUE(id) ); From 4944483546e9b080dbc7018e445dc03b6e018ab6 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Tue, 21 Oct 2025 09:40:34 +0700 Subject: [PATCH 5/6] apply suggestion --- bot/cogs/sticky.py | 8 +++++--- bot/data/db.sql | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bot/cogs/sticky.py b/bot/cogs/sticky.py index 3563a53..9104e69 100644 --- a/bot/cogs/sticky.py +++ b/bot/cogs/sticky.py @@ -39,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 and res[3]): + 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: @@ -63,6 +64,7 @@ async def on_message(self, message: discord.Message) -> None: msg.id, sticky_message, delay_time, + ignore_self, ] @app_commands.command(name="list", description="List channel with sticky message.") @@ -80,7 +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 bots.", + ignore_self="Ignore messages sent by this bots.", ) @app_guard( manage_channel=True, @@ -158,7 +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 bots.", + ignore_self="Ignore messages sent by this bots.", ) @app_guard( manage_channel=True, diff --git a/bot/data/db.sql b/bot/data/db.sql index 55023e2..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) @@ -22,7 +23,6 @@ CREATE TABLE scheduled_message( channel_id bigint NOT NULL, message text NOT NULL, date_trigger TIMESTAMP WITH TIME ZONE NOT NULL, - ignore_self BOOLEAN DEFAULT TRUE NOT NULL, -- When TRUE, sticky messages won't be reposted for self messages PRIMARY KEY(id), UNIQUE(id) ); From 8726812ba3b5b70f3449237a5b9b60d629ea17c8 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Tue, 21 Oct 2025 09:43:49 +0700 Subject: [PATCH 6/6] apply suggestion --- bot/cogs/sticky.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/sticky.py b/bot/cogs/sticky.py index 9104e69..f43ea62 100644 --- a/bot/cogs/sticky.py +++ b/bot/cogs/sticky.py @@ -82,7 +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 bots.", + ignore_self="Ignore messages sent by this bot.", ) @app_guard( manage_channel=True, @@ -160,7 +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 bots.", + ignore_self="Ignore messages sent by this bot.", ) @app_guard( manage_channel=True,