Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions bot/cogs/sticky.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async def on_connect(self) -> None:
data["message_id"],
data["message"],
data["delay_time"],
data["ignore_self"],
]

@commands.Cog.listener()
Expand All @@ -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:
Expand All @@ -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,
Comment thread
hilmoo marked this conversation as resolved.
ignore_self,
]

@app_commands.command(name="list", description="List channel with sticky message.")
async def list_sticky_messages(self, interaction: Interaction) -> None:
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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,
},
)

Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -332,6 +351,7 @@ async def resend_sticky_message(
msg.id,
data["message"],
data["delay_time"],
data["ignore_self"],
]

return await self._send_interaction(
Expand Down
3 changes: 2 additions & 1 deletion bot/data/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
);
Expand Down
Loading