From b920eae1a66964615a6acc8b313421eeb7e36ac7 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sun, 21 Jul 2024 19:52:23 +0700 Subject: [PATCH 01/14] feat(temp-role): add temporary role system --- bot/cogs/ext/temprole/time.py | 18 ++++++ bot/cogs/temporary.py | 108 ++++++++++++++++++++++++++++++++++ bot/data/db.sql | 11 ++++ 3 files changed, 137 insertions(+) create mode 100644 bot/cogs/ext/temprole/time.py create mode 100644 bot/cogs/temporary.py diff --git a/bot/cogs/ext/temprole/time.py b/bot/cogs/ext/temprole/time.py new file mode 100644 index 0000000..2cb9457 --- /dev/null +++ b/bot/cogs/ext/temprole/time.py @@ -0,0 +1,18 @@ +import re + + +def parse_time_string(time_string): + time_units = {'d': 86400, 'h': 3600, 'm': 60, 's': 1} + pattern = re.compile(r'(\d+d|\d+h|\d+m|\d+s)') + matches = pattern.findall(time_string) + + if len(''.join(matches)) != len(time_string): + raise ValueError("Invalid time format") + + total_seconds = 0 + for match in matches: + number = int(match[:-1]) + unit = match[-1] + total_seconds += number * time_units[unit] + + return total_seconds diff --git a/bot/cogs/temporary.py b/bot/cogs/temporary.py new file mode 100644 index 0000000..a04f360 --- /dev/null +++ b/bot/cogs/temporary.py @@ -0,0 +1,108 @@ +import logging +from datetime import datetime, timedelta, timezone + +import discord +from discord import app_commands, Interaction +from discord.ext import commands, tasks + +from bot.bot import WarnetBot +from bot.cogs.ext.temprole.time import parse_time_string +from bot.config import GUILD_ID + +logger = logging.getLogger(__name__) + + +@commands.guild_only() +class Temporary(commands.GroupCog, group_name='warnet-temp'): + def __init__(self, bot: WarnetBot) -> None: + self.bot = bot + self.db_pool = bot.get_db_pool() + + @commands.Cog.listener() + async def on_connect(self) -> None: + self._check_temprole.start() + + @app_commands.command(name='add', description='Adds a temprole to user') + @app_commands.describe( + user='User to add temprole to', + duration='Duration of temprole', + role='Role to add', + ) + async def give_role_on_poll( + self, + interaction: Interaction, + user: discord.Member, + duration: str, + role: discord.Role, + ) -> None: + await interaction.response.defer() + try: + duration_seconds = parse_time_string(duration) + if duration_seconds < 60: + return await interaction.followup.send( + 'Duration must be at least 1 minute', ephemeral=True + ) + await user.add_roles(role) + except ValueError: + return await interaction.followup.send('Invalid duration format', ephemeral=True) + except: + return await interaction.followup.send(f'Something went wrong', ephemeral=True) + + logger.info(f'Added role {role} to user {user.id} for {duration_seconds} seconds') + total_duration = datetime.now(timezone.utc) + timedelta(seconds=duration_seconds) + + async with self.db_pool.acquire() as conn: + await conn.execute( + 'INSERT INTO temp_role (user_id, role_id, end_time) VALUES ($1, $2, $3)', + user.id, + role.id, + total_duration, + ) + + embed = discord.Embed( + title="Role Added", + description=f"Successfully added the role {role.mention} to the {user.mention}\nremove ", + color=discord.Color.green(), + ) + await interaction.followup.send(embed=embed) + + @tasks.loop(seconds=60) + async def _check_temprole(self) -> None: + current_time = datetime.now(timezone.utc) + guild = self.bot.get_guild(GUILD_ID) + user_success = [] + + async with self.db_pool.acquire() as conn: + records = await conn.fetch( + 'SELECT user_id, role_id FROM temp_role WHERE end_time <= $1', + current_time, + ) + + for record in records: + try: + user = guild.get_member(int(record['user_id'])) + role = guild.get_role(int(record['role_id'])) + + if user and role: + await user.remove_roles(role) + user_success.append(user.id) + except Exception: + pass + + for user in user_success: + async with self.db_pool.acquire() as conn: + await conn.execute( + 'DELETE FROM temp_role WHERE user_id = $1', + user, + ) + logger.info( + f'Removed role {role.id} from {user} users' + ) + + @_check_temprole.before_loop + async def _before_check_temprole(self): + await self.bot.wait_until_ready() + + +async def setup(bot: WarnetBot) -> None: + await bot.add_cog(Temporary(bot)) diff --git a/bot/data/db.sql b/bot/data/db.sql index e506818..7b60e92 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -62,3 +62,14 @@ CREATE TABLE custom_role( UNIQUE(role_id) ); CREATE INDEX IF NOT EXISTS custom_role_role_id_idx ON custom_role (role_id); + +----- TEMPORARY ROLE FEATURE ------ +----------------------------------- +CREATE TABLE temp_role( + user_id BIGINT NOT NULL, + role_id BIGINT NOT NULL, + end_time TIMESTAMP NOT NULL + PRIMARY KEY(user_id), + UNIQUE(user_id) +); +CREATE INDEX IF NOT EXISTS temp_role_role_id_idx ON temp_role (user_id); From 8fb7c819c1f33fc7c09427f5f61f0c585a1a47ca Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sun, 21 Jul 2024 20:02:09 +0700 Subject: [PATCH 02/14] change description duration in the command --- bot/cogs/temporary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/temporary.py b/bot/cogs/temporary.py index a04f360..a9e1947 100644 --- a/bot/cogs/temporary.py +++ b/bot/cogs/temporary.py @@ -25,7 +25,7 @@ async def on_connect(self) -> None: @app_commands.command(name='add', description='Adds a temprole to user') @app_commands.describe( user='User to add temprole to', - duration='Duration of temprole', + duration='Example: 1d2h3m4s', role='Role to add', ) async def give_role_on_poll( From 65293e64be876fea17519f112b4a93530b2a7edc Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sun, 21 Jul 2024 21:34:09 +0700 Subject: [PATCH 03/14] add permission to add and remove temp roles --- bot/cogs/temporary.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bot/cogs/temporary.py b/bot/cogs/temporary.py index a9e1947..6b97b36 100644 --- a/bot/cogs/temporary.py +++ b/bot/cogs/temporary.py @@ -36,6 +36,11 @@ async def give_role_on_poll( role: discord.Role, ) -> None: await interaction.response.defer() + if not interaction.user.guild_permissions.manage_roles: + return await interaction.followup.send( + 'You do not have permission to manage roles', ephemeral=True + ) + try: duration_seconds = parse_time_string(duration) if duration_seconds < 60: @@ -95,9 +100,7 @@ async def _check_temprole(self) -> None: 'DELETE FROM temp_role WHERE user_id = $1', user, ) - logger.info( - f'Removed role {role.id} from {user} users' - ) + logger.info(f'Removed role {role.id} from {user} users') @_check_temprole.before_loop async def _before_check_temprole(self): From 4f2fdbde5d4f2644ec2690c1ad5bd020e14609c7 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sun, 21 Jul 2024 22:14:36 +0700 Subject: [PATCH 04/14] fix error logic --- bot/cogs/temporary.py | 13 ++++++++----- bot/config/__init__.py | 5 +++++ bot/data/db.sql | 9 +++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/bot/cogs/temporary.py b/bot/cogs/temporary.py index 6b97b36..2e4dc11 100644 --- a/bot/cogs/temporary.py +++ b/bot/cogs/temporary.py @@ -7,7 +7,7 @@ from bot.bot import WarnetBot from bot.cogs.ext.temprole.time import parse_time_string -from bot.config import GUILD_ID +from bot.config import GUILD_ID, GiveawayConfig logger = logging.getLogger(__name__) @@ -36,6 +36,11 @@ async def give_role_on_poll( role: discord.Role, ) -> None: await interaction.response.defer() + if role.id is not GiveawayConfig.GIVEAWAY_ROLE_ID: + return await interaction.followup.send( + 'cannot add blacklist giveaway role!!', ephemeral=True + ) + if not interaction.user.guild_permissions.manage_roles: return await interaction.followup.send( 'You do not have permission to manage roles', ephemeral=True @@ -87,10 +92,8 @@ async def _check_temprole(self) -> None: try: user = guild.get_member(int(record['user_id'])) role = guild.get_role(int(record['role_id'])) - - if user and role: - await user.remove_roles(role) - user_success.append(user.id) + await user.remove_roles(role) + user_success.append(user.id) except Exception: pass diff --git a/bot/config/__init__.py b/bot/config/__init__.py index 6798b55..a27983d 100644 --- a/bot/config/__init__.py +++ b/bot/config/__init__.py @@ -72,3 +72,8 @@ class TCGConfig: TCG_TITLE_ROLE_ID[2]: '<:MasterDuelist:1052440400822018078>', TCG_TITLE_ROLE_ID[3]: '<:ImmortalDuelist:1052440404135518228>', } + + +class GiveawayConfig: + GIVEAWAY_ROLE_ID = 1058433863333978172 + BIG_GIVEAWAY = 50000 diff --git a/bot/data/db.sql b/bot/data/db.sql index 7b60e92..d9d7951 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -66,10 +66,11 @@ CREATE INDEX IF NOT EXISTS custom_role_role_id_idx ON custom_role (role_id); ----- TEMPORARY ROLE FEATURE ------ ----------------------------------- CREATE TABLE temp_role( + id SERIAL NOT NULL, user_id BIGINT NOT NULL, role_id BIGINT NOT NULL, - end_time TIMESTAMP NOT NULL - PRIMARY KEY(user_id), - UNIQUE(user_id) + end_time TIMESTAMP NOT NULL, + PRIMARY KEY(id), + UNIQUE(id) ); -CREATE INDEX IF NOT EXISTS temp_role_role_id_idx ON temp_role (user_id); +CREATE INDEX IF NOT EXISTS temp_role_role_id_idx ON temp_role (id); \ No newline at end of file From 7349362bbe2a060f755f9ff584b93f524b0f7ee3 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sun, 21 Jul 2024 22:20:17 +0700 Subject: [PATCH 05/14] fix small bug --- bot/cogs/temporary.py | 6 +++--- bot/config/__init__.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/temporary.py b/bot/cogs/temporary.py index 2e4dc11..854fbb1 100644 --- a/bot/cogs/temporary.py +++ b/bot/cogs/temporary.py @@ -7,7 +7,7 @@ from bot.bot import WarnetBot from bot.cogs.ext.temprole.time import parse_time_string -from bot.config import GUILD_ID, GiveawayConfig +from bot.config import GiveawayConfig, GUILD_ID logger = logging.getLogger(__name__) @@ -36,9 +36,9 @@ async def give_role_on_poll( role: discord.Role, ) -> None: await interaction.response.defer() - if role.id is not GiveawayConfig.GIVEAWAY_ROLE_ID: + if role.id is GiveawayConfig.BLACKLIST_ROLE_ID: return await interaction.followup.send( - 'cannot add blacklist giveaway role!!', ephemeral=True + 'Cannot add blacklist giveaway role!!', ephemeral=True ) if not interaction.user.guild_permissions.manage_roles: diff --git a/bot/config/__init__.py b/bot/config/__init__.py index a27983d..befd08c 100644 --- a/bot/config/__init__.py +++ b/bot/config/__init__.py @@ -75,5 +75,5 @@ class TCGConfig: class GiveawayConfig: - GIVEAWAY_ROLE_ID = 1058433863333978172 + BLACKLIST_ROLE_ID = 944559967191576636 BIG_GIVEAWAY = 50000 From 6203651f0064afd1fbd53f313a64dbb45d1d7f4d Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sun, 21 Jul 2024 23:04:44 +0700 Subject: [PATCH 06/14] feat(giveaway): add blacklist feature for giveaways --- bot/cogs/giveaway.py | 146 +++++++++++++++++++++++++++++++++++++++++++ bot/data/db.sql | 14 ++++- 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 bot/cogs/giveaway.py diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py new file mode 100644 index 0000000..7bb9f65 --- /dev/null +++ b/bot/cogs/giveaway.py @@ -0,0 +1,146 @@ +import logging +from datetime import datetime, timedelta, timezone +from typing import Optional + +import discord +from discord import app_commands, Interaction +from discord.ext import commands, tasks + +from bot.bot import WarnetBot +from bot.cogs.ext.temprole.time import parse_time_string +from bot.config import GiveawayConfig, GUILD_ID + +logger = logging.getLogger(__name__) + + +@commands.guild_only() +class Giveaway(commands.GroupCog, group_name='warnet-ga'): + def __init__(self, bot: WarnetBot) -> None: + self.bot = bot + self.db_pool = bot.get_db_pool() + + @commands.Cog.listener() + async def on_connect(self) -> None: + self._check_blacklist_ga.start() + + @app_commands.command(name='blacklist', description='Blacklist a user from giveaway') + @app_commands.describe( + amount='Amount of giveaway. Example: 50000', + winner='Winner of the giveaway. Example: 1234567890,0987654321', + ghosting='the ghost. Example: 1234567890,0987654321', + ) + async def give_role_on_poll( + self, + interaction: Interaction, + amount: int, + winner: str, + ghosting: Optional[str] = None, + ) -> None: + await interaction.response.defer() + if not interaction.user.guild_permissions.administrator: + return await interaction.followup.send('You are not an admin', ephemeral=True) + + winnerss: list[discord.Member] = [] + winners = winner.split(',') + for win in winners: + user = interaction.guild.get_member(int(win)) + if user is None: + return await interaction.followup.send(f'User {win} not found', ephemeral=True) + winnerss.append(user) + + ghostss: list[discord.Member] = [] + if ghosting: + ghosts = ghosting.split(',') + for ghost in ghosts: + user = interaction.guild.get_member(ghost) + if user is None: + return await interaction.followup.send( + f'User {ghost} not found', ephemeral=True + ) + ghostss.append(user) + + if amount < GiveawayConfig.BIG_GIVEAWAY: + winner_day: 15 + ghosting_day: 7 + else: + winner_day: 30 + ghosting_day: 15 + blacklist_role = interaction.guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) + + for winn in winnerss: + await winn.add_roles(blacklist_role) + end_time = datetime.now(timezone.utc) + timedelta(days=winner_day) + async with self.db_pool.acquire() as conn: + await conn.execute( + 'INSERT INTO black_ga (user_id, end_time, last_time, has_role) VALUES ($1, $2, $3, TRUE)', + winn.id, + end_time, + end_time, + ) + logger.info(f'Added role {blacklist_role.id} to user {winn.id} for {winner_day} days') + + for ghost in ghostss: + await ghost.add_roles(blacklist_role) + end_time = datetime.now(timezone.utc) + timedelta(days=ghosting_day) + async with self.db_pool.acquire() as conn: + await conn.execute( + 'INSERT INTO black_ga (user_id, end_time, last_time, has_role) VALUES ($1, $2, $3, TRUE)', + ghost.id, + end_time, + end_time, + ) + logger.info(f'Added role {blacklist_role.id} to user {ghost.id} for {ghosting_day} days') + + embed = discord.Embed( + title="Role Added", + description=f"Successfully added the role {blacklist_role.mention} to the {len(winnerss)} winners and {len(ghostss)} ghosting", + color=discord.Color.green(), + ) + embed.add_field( + name="List of Winners", + value=f"{', '.join([winner.mention for winner in winnerss])}", + ) + embed.add_field( + name="List of Ghosting", + value=f"{', '.join([ghost.mention for ghost in ghostss])}", + ) + await interaction.followup.send(embed=embed) + + @tasks.loop(seconds=60) + async def _check_blacklist_ga(self) -> None: + current_time = datetime.now(timezone.utc) + guild = self.bot.get_guild(GUILD_ID) + + async with self.db_pool.acquire() as conn: + records = await conn.fetch( + 'SELECT user_id FROM black_ga WHERE end_time <= $1 AND end_time > 0', + current_time, + ) + + for record in records: + try: + user = guild.get_member(int(record['user_id'])) + role = guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) + await user.remove_roles(role) + async with self.db_pool.acquire() as conn: + await conn.execute( + 'UPDATE black_ga SET has_role = FALSE WHERE user_id = $1', + user.id, + ) + logger.info(f'Removed role {role} from user {user.id}') + except Exception: + pass + + async with self.db_pool.acquire() as conn: + await conn.execute( + 'UPDATE black_ga SET end_time=0 WHERE end_time <= $1 AND has_role = FALSE', + current_time, + ) + + @_check_blacklist_ga.before_loop + async def _before_check_blacklist_ga(self): + await self.bot.wait_until_ready() + + +async def setup(bot: WarnetBot) -> None: + await bot.add_cog(Giveaway(bot)) diff --git a/bot/data/db.sql b/bot/data/db.sql index d9d7951..43b5606 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -73,4 +73,16 @@ CREATE TABLE temp_role( PRIMARY KEY(id), UNIQUE(id) ); -CREATE INDEX IF NOT EXISTS temp_role_role_id_idx ON temp_role (id); \ No newline at end of file +CREATE INDEX IF NOT EXISTS temp_role_role_id_idx ON temp_role (id); + +--- GIVEAWAY BLACKLIST FEATURE ---- +----------------------------------- +CREATE TABLE black_ga( + user_id BIGINT NOT NULL, + end_time TIMESTAMP NOT NULL, + last_time TIMESTAMP NOT NULL, + has_role BOOLEAN DEFAULT FALSE NOT NULL, + PRIMARY KEY(user_id), + UNIQUE(user_id) +); +CREATE INDEX IF NOT EXISTS black_ga_id_idx ON black_ga (user_id); \ No newline at end of file From 71e8db36da4ce2b85c71c297896a624796fb1369 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sun, 21 Jul 2024 23:05:57 +0700 Subject: [PATCH 07/14] fix --- bot/cogs/giveaway.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py index 7bb9f65..759b09b 100644 --- a/bot/cogs/giveaway.py +++ b/bot/cogs/giveaway.py @@ -7,7 +7,6 @@ from discord.ext import commands, tasks from bot.bot import WarnetBot -from bot.cogs.ext.temprole.time import parse_time_string from bot.config import GiveawayConfig, GUILD_ID logger = logging.getLogger(__name__) @@ -89,7 +88,9 @@ async def give_role_on_poll( end_time, end_time, ) - logger.info(f'Added role {blacklist_role.id} to user {ghost.id} for {ghosting_day} days') + logger.info( + f'Added role {blacklist_role.id} to user {ghost.id} for {ghosting_day} days' + ) embed = discord.Embed( title="Role Added", From b5eb1dc77c6bb2c5ea0191c21c7061c752d7aec0 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sun, 21 Jul 2024 23:09:19 +0700 Subject: [PATCH 08/14] improve syntax --- bot/cogs/giveaway.py | 4 ++-- bot/data/db.sql | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py index 759b09b..45c94dd 100644 --- a/bot/cogs/giveaway.py +++ b/bot/cogs/giveaway.py @@ -71,7 +71,7 @@ async def give_role_on_poll( end_time = datetime.now(timezone.utc) + timedelta(days=winner_day) async with self.db_pool.acquire() as conn: await conn.execute( - 'INSERT INTO black_ga (user_id, end_time, last_time, has_role) VALUES ($1, $2, $3, TRUE)', + 'INSERT INTO black_ga (user_id, end_time, last_time) VALUES ($1, $2, $3)', winn.id, end_time, end_time, @@ -83,7 +83,7 @@ async def give_role_on_poll( end_time = datetime.now(timezone.utc) + timedelta(days=ghosting_day) async with self.db_pool.acquire() as conn: await conn.execute( - 'INSERT INTO black_ga (user_id, end_time, last_time, has_role) VALUES ($1, $2, $3, TRUE)', + 'INSERT INTO black_ga (user_id, end_time, last_time) VALUES ($1, $2, $3)', ghost.id, end_time, end_time, diff --git a/bot/data/db.sql b/bot/data/db.sql index 43b5606..fffdb1d 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -81,7 +81,7 @@ CREATE TABLE black_ga( user_id BIGINT NOT NULL, end_time TIMESTAMP NOT NULL, last_time TIMESTAMP NOT NULL, - has_role BOOLEAN DEFAULT FALSE NOT NULL, + has_role BOOLEAN DEFAULT TRUE NOT NULL, PRIMARY KEY(user_id), UNIQUE(user_id) ); From d6cfa60286be359f5be9e141d08ea269c38b5eab Mon Sep 17 00:00:00 2001 From: hilmoo Date: Mon, 22 Jul 2024 09:00:09 +0700 Subject: [PATCH 09/14] fix/logic for giveaway blacklist --- bot/cogs/giveaway.py | 76 +++++++++++++++++++++++++++----------------- bot/data/db.sql | 2 +- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py index 45c94dd..b6f6c7f 100644 --- a/bot/cogs/giveaway.py +++ b/bot/cogs/giveaway.py @@ -64,18 +64,31 @@ async def give_role_on_poll( else: winner_day: 30 ghosting_day: 15 + end_time_no_streak = datetime.now(timezone.utc) + timedelta(days=winner_day) + end_time_streak = datetime.now(timezone.utc) + timedelta(days=winner_day * 2) blacklist_role = interaction.guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) - for winn in winnerss: - await winn.add_roles(blacklist_role) - end_time = datetime.now(timezone.utc) + timedelta(days=winner_day) - async with self.db_pool.acquire() as conn: - await conn.execute( - 'INSERT INTO black_ga (user_id, end_time, last_time) VALUES ($1, $2, $3)', - winn.id, - end_time, - end_time, - ) + async with self.db_pool.acquire() as conn: + streak_user = await conn.fetch( + 'SELECT user_id FROM black_ga WHERE status_user = 1', + ) + + for winn in winnerss: + await winn.add_roles(blacklist_role) + if win in streak_user: + await conn.execute( + 'UPDATE black_ga SET (end_time, status_user) VALUES ($2, $3) WHERE user_id = $1', + winn.id, + end_time_streak, + 3, + ) + else: + await conn.execute( + 'INSERT INTO black_ga (user_id, end_time, status_user) VALUES ($1, $2, $3)', + winn.id, + end_time_no_streak, + 1, + ) logger.info(f'Added role {blacklist_role.id} to user {winn.id} for {winner_day} days') for ghost in ghostss: @@ -83,10 +96,10 @@ async def give_role_on_poll( end_time = datetime.now(timezone.utc) + timedelta(days=ghosting_day) async with self.db_pool.acquire() as conn: await conn.execute( - 'INSERT INTO black_ga (user_id, end_time, last_time) VALUES ($1, $2, $3)', + 'INSERT INTO black_ga (user_id, end_time, status_user) VALUES ($1, $2, $3)', ghost.id, end_time, - end_time, + 2, ) logger.info( f'Added role {blacklist_role.id} to user {ghost.id} for {ghosting_day} days' @@ -94,7 +107,7 @@ async def give_role_on_poll( embed = discord.Embed( title="Role Added", - description=f"Successfully added the role {blacklist_role.mention} to the {len(winnerss)} winners and {len(ghostss)} ghosting", + description=f"Successfully added the role {blacklist_role.mention} to the {len(winnerss)} winners and {len(ghostss)} ghosts", color=discord.Color.green(), ) embed.add_field( @@ -102,42 +115,45 @@ async def give_role_on_poll( value=f"{', '.join([winner.mention for winner in winnerss])}", ) embed.add_field( - name="List of Ghosting", + name="List of Ghosts", value=f"{', '.join([ghost.mention for ghost in ghostss])}", ) await interaction.followup.send(embed=embed) @tasks.loop(seconds=60) async def _check_blacklist_ga(self) -> None: - current_time = datetime.now(timezone.utc) guild = self.bot.get_guild(GUILD_ID) async with self.db_pool.acquire() as conn: - records = await conn.fetch( - 'SELECT user_id FROM black_ga WHERE end_time <= $1 AND end_time > 0', - current_time, + user_want_remove = await conn.fetch( + 'SELECT user_id FROM black_ga WHERE end_time <= $1 AND has_role = TRUE', + datetime.now(timezone.utc) - timedelta(days=30), ) - for record in records: - try: - user = guild.get_member(int(record['user_id'])) - role = guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) - await user.remove_roles(role) - async with self.db_pool.acquire() as conn: + for user in user_want_remove: + user = guild.get_member(int(user['user_id'])) + if user: + blacklist_role = guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) + await user.remove_roles(blacklist_role) await conn.execute( 'UPDATE black_ga SET has_role = FALSE WHERE user_id = $1', user.id, ) - logger.info(f'Removed role {role} from user {user.id}') - except Exception: - pass + logger.info(f'Removed role {blacklist_role.id} from user {user.id} (rm role)') async with self.db_pool.acquire() as conn: - await conn.execute( - 'UPDATE black_ga SET end_time=0 WHERE end_time <= $1 AND has_role = FALSE', - current_time, + user_want_delete = await conn.fetch( + 'SELECT user_id FROM black_ga WHERE AND has_role = FALSE AND status_user != 1', ) + for user in user_want_delete: + if user: + await conn.execute( + 'DELETE FROM black_ga WHERE user_id = $1', + int(user['user_id']), + ) + logger.info(f'Deleted user {user.id} (rm row table)') + @_check_blacklist_ga.before_loop async def _before_check_blacklist_ga(self): await self.bot.wait_until_ready() diff --git a/bot/data/db.sql b/bot/data/db.sql index fffdb1d..79fdead 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -80,8 +80,8 @@ CREATE INDEX IF NOT EXISTS temp_role_role_id_idx ON temp_role (id); CREATE TABLE black_ga( user_id BIGINT NOT NULL, end_time TIMESTAMP NOT NULL, - last_time TIMESTAMP NOT NULL, has_role BOOLEAN DEFAULT TRUE NOT NULL, + status_user INT DEFAULT 0 NOT NULL, -- 0: default, 1: win, 2: ghost, 3: strek PRIMARY KEY(user_id), UNIQUE(user_id) ); From 94185915c6080a6fe67e837b83d00c717c262141 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Mon, 22 Jul 2024 10:42:17 +0700 Subject: [PATCH 10/14] change function name --- bot/cogs/giveaway.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py index b6f6c7f..bad28c5 100644 --- a/bot/cogs/giveaway.py +++ b/bot/cogs/giveaway.py @@ -28,7 +28,7 @@ async def on_connect(self) -> None: winner='Winner of the giveaway. Example: 1234567890,0987654321', ghosting='the ghost. Example: 1234567890,0987654321', ) - async def give_role_on_poll( + async def add_giveaway_blacklist( self, interaction: Interaction, amount: int, From 4469f074b5d54782ca92ece4f9c93df1a8ca37a4 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Fri, 20 Sep 2024 20:17:53 +0700 Subject: [PATCH 11/14] feat/refactor blacklist GA system --- bot/cogs/giveaway.py | 58 +++++++++++++++++++++++++------------------- bot/data/db.sql | 5 ++-- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py index bad28c5..fac2641 100644 --- a/bot/cogs/giveaway.py +++ b/bot/cogs/giveaway.py @@ -24,9 +24,9 @@ async def on_connect(self) -> None: @app_commands.command(name='blacklist', description='Blacklist a user from giveaway') @app_commands.describe( - amount='Amount of giveaway. Example: 50000', + amount='Giveaway prizes. Example: 50000', winner='Winner of the giveaway. Example: 1234567890,0987654321', - ghosting='the ghost. Example: 1234567890,0987654321', + ghosting='Member who do not claim the giveaway. Example: 1234567890,0987654321', ) async def add_giveaway_blacklist( self, @@ -64,8 +64,10 @@ async def add_giveaway_blacklist( else: winner_day: 30 ghosting_day: 15 + end_time_no_streak = datetime.now(timezone.utc) + timedelta(days=winner_day) end_time_streak = datetime.now(timezone.utc) + timedelta(days=winner_day * 2) + end_time_ghosting = datetime.now(timezone.utc) + timedelta(days=ghosting_day) blacklist_role = interaction.guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) async with self.db_pool.acquire() as conn: @@ -77,10 +79,11 @@ async def add_giveaway_blacklist( await winn.add_roles(blacklist_role) if win in streak_user: await conn.execute( - 'UPDATE black_ga SET (end_time, status_user) VALUES ($2, $3) WHERE user_id = $1', + 'UPDATE black_ga SET (end_time, status_user, cooldown_time) VALUES ($2, $3, $4) WHERE user_id = $1', winn.id, end_time_streak, - 3, + 0, + end_time_no_streak, ) else: await conn.execute( @@ -89,21 +92,28 @@ async def add_giveaway_blacklist( end_time_no_streak, 1, ) - logger.info(f'Added role {blacklist_role.id} to user {winn.id} for {winner_day} days') - - for ghost in ghostss: - await ghost.add_roles(blacklist_role) - end_time = datetime.now(timezone.utc) + timedelta(days=ghosting_day) - async with self.db_pool.acquire() as conn: - await conn.execute( - 'INSERT INTO black_ga (user_id, end_time, status_user) VALUES ($1, $2, $3)', - ghost.id, - end_time, - 2, + logger.info( + f'Added role {blacklist_role.id} to user {winn.id} for {winner_day} days' + ) + + for ghost in ghostss: + await ghost.add_roles(blacklist_role) + if ghost in streak_user: + await conn.execute( + 'UPDATE black_ga SET (end_time) VALUES ($2) WHERE user_id = $1', + ghost.id, + end_time_ghosting, + ) + else: + await conn.execute( + 'INSERT INTO black_ga (user_id, end_time, status_user) VALUES ($1, $2, $3)', + ghost.id, + end_time_ghosting, + 0, + ) + logger.info( + f'Added role {blacklist_role.id} to user {ghost.id} for {ghosting_day} days' ) - logger.info( - f'Added role {blacklist_role.id} to user {ghost.id} for {ghosting_day} days' - ) embed = discord.Embed( title="Role Added", @@ -123,17 +133,20 @@ async def add_giveaway_blacklist( @tasks.loop(seconds=60) async def _check_blacklist_ga(self) -> None: guild = self.bot.get_guild(GUILD_ID) + blacklist_role = guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) async with self.db_pool.acquire() as conn: user_want_remove = await conn.fetch( 'SELECT user_id FROM black_ga WHERE end_time <= $1 AND has_role = TRUE', - datetime.now(timezone.utc) - timedelta(days=30), + datetime.now(timezone.utc), + ) + user_want_delete = await conn.fetch( + 'SELECT user_id FROM black_ga WHERE has_role = FALSE AND status_user = 0', ) for user in user_want_remove: user = guild.get_member(int(user['user_id'])) if user: - blacklist_role = guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) await user.remove_roles(blacklist_role) await conn.execute( 'UPDATE black_ga SET has_role = FALSE WHERE user_id = $1', @@ -141,11 +154,6 @@ async def _check_blacklist_ga(self) -> None: ) logger.info(f'Removed role {blacklist_role.id} from user {user.id} (rm role)') - async with self.db_pool.acquire() as conn: - user_want_delete = await conn.fetch( - 'SELECT user_id FROM black_ga WHERE AND has_role = FALSE AND status_user != 1', - ) - for user in user_want_delete: if user: await conn.execute( diff --git a/bot/data/db.sql b/bot/data/db.sql index 11ca143..6d34d8a 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -79,9 +79,10 @@ CREATE INDEX IF NOT EXISTS temp_role_user_role_idx ON temp_role (user_id, role_i ----------------------------------- CREATE TABLE black_ga( user_id BIGINT NOT NULL, - end_time TIMESTAMP NOT NULL, + end_time TIMESTAMP WITH TIME ZONE NOT NULL, + cooldown_time TIMESTAMP WITH TIME ZONE, has_role BOOLEAN DEFAULT TRUE NOT NULL, - status_user INT DEFAULT 0 NOT NULL, -- 0: default, 1: win, 2: ghost, 3: strek + status_user INT NOT NULL, -- 1: in streak phase, 0: not in streak phase PRIMARY KEY(user_id), UNIQUE(user_id) ); From 8d80a70a576b08e88aa91628462314fe7abeef44 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sat, 21 Sep 2024 18:12:36 +0700 Subject: [PATCH 12/14] fix/Refactor role handling in giveaway and temporary cogs --- bot/bot.py | 5 +- bot/cogs/giveaway.py | 127 +++++++++++++++++++++-------------------- bot/cogs/temporary.py | 4 +- bot/config/__init__.py | 6 +- bot/data/db.sql | 4 +- 5 files changed, 75 insertions(+), 71 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index ac25edb..88d6ac2 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -42,7 +42,10 @@ async def setup_hook(self) -> None: self.session = aiohttp.ClientSession() self.bot_app_info = await self.application_info() - self.owner_ids = {m.id for m in self.bot_app_info.team.members} + if self.bot_app_info.team: + self.owner_ids = {m.id for m in self.bot_app_info.team.members} + else: + self.owner_id = self.bot_app_info.owner.id await self.load_cogs() diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py index fac2641..106b2d1 100644 --- a/bot/cogs/giveaway.py +++ b/bot/cogs/giveaway.py @@ -7,7 +7,7 @@ from discord.ext import commands, tasks from bot.bot import WarnetBot -from bot.config import GiveawayConfig, GUILD_ID +from bot.config import BLACKLIST_GA_ROLE_ID, GUILD_ID logger = logging.getLogger(__name__) @@ -24,89 +24,94 @@ async def on_connect(self) -> None: @app_commands.command(name='blacklist', description='Blacklist a user from giveaway') @app_commands.describe( - amount='Giveaway prizes. Example: 50000', - winner='Winner of the giveaway. Example: 1234567890,0987654321', - ghosting='Member who do not claim the giveaway. Example: 1234567890,0987654321', + big_GA='Yes if the giveaway is large than Rp50.000', + winners='Winner of the giveaway. Example: 1234567890,0987654321', + ghosts='Member who do not claim the giveaway. Example: 1234567890,0987654321', ) async def add_giveaway_blacklist( self, interaction: Interaction, - amount: int, - winner: str, - ghosting: Optional[str] = None, + big_GA: bool, + winners: str, + ghosts: Optional[str] = None, ) -> None: await interaction.response.defer() if not interaction.user.guild_permissions.administrator: return await interaction.followup.send('You are not an admin', ephemeral=True) - winnerss: list[discord.Member] = [] - winners = winner.split(',') - for win in winners: - user = interaction.guild.get_member(int(win)) - if user is None: - return await interaction.followup.send(f'User {win} not found', ephemeral=True) - winnerss.append(user) - - ghostss: list[discord.Member] = [] - if ghosting: - ghosts = ghosting.split(',') - for ghost in ghosts: - user = interaction.guild.get_member(ghost) - if user is None: + winner_list: list[discord.Member] = [] + for (winner := winners.split(',')) in winners: + if (user := interaction.guild.get_member(winner)): + return await interaction.followup.send( + f'User {winner} not found', ephemeral=True + ) + winner_list.append(user) + + ghost_list: list[discord.Member] = [] + if ghosts: + for (ghost := ghosts.split(',')) in ghosts: + if (user := interaction.guild.get_member(ghost)): return await interaction.followup.send( f'User {ghost} not found', ephemeral=True ) - ghostss.append(user) + ghost_list.append(user) - if amount < GiveawayConfig.BIG_GIVEAWAY: - winner_day: 15 - ghosting_day: 7 + if big_GA: + winner_day = 15 + ghosting_day = 7 else: - winner_day: 30 - ghosting_day: 15 + winner_day = 30 + ghosting_day = 15 - end_time_no_streak = datetime.now(timezone.utc) + timedelta(days=winner_day) - end_time_streak = datetime.now(timezone.utc) + timedelta(days=winner_day * 2) - end_time_ghosting = datetime.now(timezone.utc) + timedelta(days=ghosting_day) - blacklist_role = interaction.guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) + # change to days, seconds only for testing + now = datetime.now(timezone.utc) + end_time_no_streak = now + timedelta(seconds=winner_day) + end_time_streak = now + timedelta(seconds=winner_day * 2) + end_time_ghosting = now + timedelta(seconds=ghosting_day) + blacklist_role = interaction.guild.get_role(BLACKLIST_GA_ROLE_ID) async with self.db_pool.acquire() as conn: - streak_user = await conn.fetch( - 'SELECT user_id FROM black_ga WHERE status_user = 1', + streak_user_records = await conn.fetch( + 'SELECT user_id FROM blacklist_ga WHERE status_user = 1', ) + streak_user = {record['user_id'] for record in streak_user_records} - for winn in winnerss: - await winn.add_roles(blacklist_role) - if win in streak_user: + for winner in winner_list: + await winner.add_roles(blacklist_role) + if winner.id in streak_user: await conn.execute( - 'UPDATE black_ga SET (end_time, status_user, cooldown_time) VALUES ($2, $3, $4) WHERE user_id = $1', - winn.id, + ''' + UPDATE blacklist_ga + SET end_time = $2, status_user = $3 + WHERE user_id = $1 + ''', + winner.id, end_time_streak, 0, - end_time_no_streak, ) else: await conn.execute( - 'INSERT INTO black_ga (user_id, end_time, status_user) VALUES ($1, $2, $3)', - winn.id, + 'INSERT INTO blacklist_ga (user_id, end_time, status_user, cooldown_time) VALUES ($1, $2, $3, $4)', + winner.id, end_time_no_streak, 1, + end_time_streak, ) logger.info( - f'Added role {blacklist_role.id} to user {winn.id} for {winner_day} days' + f'Added role {blacklist_role.id} to user {winner.id} for {winner_day} days' ) - for ghost in ghostss: + for ghost in ghost_list: await ghost.add_roles(blacklist_role) - if ghost in streak_user: + if ghost.id in streak_user: await conn.execute( - 'UPDATE black_ga SET (end_time) VALUES ($2) WHERE user_id = $1', + 'UPDATE blacklist_ga SET (end_time) VALUES ($2) WHERE user_id = $1', ghost.id, end_time_ghosting, ) else: await conn.execute( - 'INSERT INTO black_ga (user_id, end_time, status_user) VALUES ($1, $2, $3)', + 'INSERT INTO blacklist_ga (user_id, end_time, status_user) VALUES ($1, $2, $3)', ghost.id, end_time_ghosting, 0, @@ -117,50 +122,50 @@ async def add_giveaway_blacklist( embed = discord.Embed( title="Role Added", - description=f"Successfully added the role {blacklist_role.mention} to the {len(winnerss)} winners and {len(ghostss)} ghosts", + description=f"Successfully added the role {blacklist_role.mention} to the {len(winner_list)} winners and {len(ghost_list)} ghosts", color=discord.Color.green(), ) embed.add_field( name="List of Winners", - value=f"{', '.join([winner.mention for winner in winnerss])}", + value=f"{', '.join([winner.mention for winner in winner_list])}", ) embed.add_field( name="List of Ghosts", - value=f"{', '.join([ghost.mention for ghost in ghostss])}", + value=f"{', '.join([ghost.mention for ghost in ghost_list])}", ) await interaction.followup.send(embed=embed) @tasks.loop(seconds=60) async def _check_blacklist_ga(self) -> None: guild = self.bot.get_guild(GUILD_ID) - blacklist_role = guild.get_role(GiveawayConfig.BLACKLIST_ROLE_ID) + blacklist_role = guild.get_role(BLACKLIST_GA_ROLE_ID) + now = datetime.now(timezone.utc) async with self.db_pool.acquire() as conn: user_want_remove = await conn.fetch( - 'SELECT user_id FROM black_ga WHERE end_time <= $1 AND has_role = TRUE', - datetime.now(timezone.utc), - ) - user_want_delete = await conn.fetch( - 'SELECT user_id FROM black_ga WHERE has_role = FALSE AND status_user = 0', + 'SELECT user_id FROM blacklist_ga WHERE end_time <= $1 AND has_role = TRUE', + now, ) - for user in user_want_remove: - user = guild.get_member(int(user['user_id'])) - if user: + if (user := guild.get_member(int(user['user_id']))): await user.remove_roles(blacklist_role) await conn.execute( - 'UPDATE black_ga SET has_role = FALSE WHERE user_id = $1', + 'UPDATE blacklist_ga SET has_role = FALSE WHERE user_id = $1', user.id, ) logger.info(f'Removed role {blacklist_role.id} from user {user.id} (rm role)') + user_want_delete = await conn.fetch( + 'SELECT user_id FROM blacklist_ga WHERE has_role = FALSE AND status_user = 0 OR cooldown_time <= $1', + now, + ) for user in user_want_delete: if user: await conn.execute( - 'DELETE FROM black_ga WHERE user_id = $1', + 'DELETE FROM blacklist_ga WHERE user_id = $1', int(user['user_id']), ) - logger.info(f'Deleted user {user.id} (rm row table)') + logger.info(f'Deleted user {user} (rm row table)') @_check_blacklist_ga.before_loop async def _before_check_blacklist_ga(self): diff --git a/bot/cogs/temporary.py b/bot/cogs/temporary.py index 7fca846..22ba018 100644 --- a/bot/cogs/temporary.py +++ b/bot/cogs/temporary.py @@ -7,7 +7,7 @@ from bot.bot import WarnetBot from bot.cogs.ext.temprole.time import parse_time_string -from bot.config import GiveawayConfig, GUILD_ID +from bot.config import BLACKLIST_GA_ROLE_ID, GUILD_ID logger = logging.getLogger(__name__) @@ -36,7 +36,7 @@ async def add_temporary_role( role: discord.Role, ) -> None: await interaction.response.defer() - if role.id == GiveawayConfig.BLACKLIST_ROLE_ID: + if role.id == BLACKLIST_GA_ROLE_ID: return await interaction.followup.send( 'Cannot add blacklist giveaway role!!', ephemeral=True ) diff --git a/bot/config/__init__.py b/bot/config/__init__.py index befd08c..d546c78 100644 --- a/bot/config/__init__.py +++ b/bot/config/__init__.py @@ -33,6 +33,7 @@ ADMINISTRATOR_ROLE_ID = {'admin': '761650159833841674', 'mod': '761662280541798421'} NON_ADMINISTRATOR_ROLE_ID = {'staff': '951170972671701063'} BOOSTER_ROLE_ID = 768874803712753727 +BLACKLIST_GA_ROLE_ID = 944559967191576636 ANNOUNCEMENT_CHANNEL_ID = 761680720245686313 ADMIN_CHANNEL_ID = 761684443915485184 @@ -72,8 +73,3 @@ class TCGConfig: TCG_TITLE_ROLE_ID[2]: '<:MasterDuelist:1052440400822018078>', TCG_TITLE_ROLE_ID[3]: '<:ImmortalDuelist:1052440404135518228>', } - - -class GiveawayConfig: - BLACKLIST_ROLE_ID = 944559967191576636 - BIG_GIVEAWAY = 50000 diff --git a/bot/data/db.sql b/bot/data/db.sql index 6d34d8a..5965ede 100644 --- a/bot/data/db.sql +++ b/bot/data/db.sql @@ -77,7 +77,7 @@ CREATE INDEX IF NOT EXISTS temp_role_user_role_idx ON temp_role (user_id, role_i --- GIVEAWAY BLACKLIST FEATURE ---- ----------------------------------- -CREATE TABLE black_ga( +CREATE TABLE blacklist_ga( user_id BIGINT NOT NULL, end_time TIMESTAMP WITH TIME ZONE NOT NULL, cooldown_time TIMESTAMP WITH TIME ZONE, @@ -86,4 +86,4 @@ CREATE TABLE black_ga( PRIMARY KEY(user_id), UNIQUE(user_id) ); -CREATE INDEX IF NOT EXISTS black_ga_id_idx ON black_ga (user_id); \ No newline at end of file +CREATE INDEX IF NOT EXISTS blacklist_ga_id_idx ON blacklist_ga (user_id); \ No newline at end of file From 28bd7ed0a2e47f59e6a9cb9387a270dff72708fb Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sat, 21 Sep 2024 21:05:37 +0700 Subject: [PATCH 13/14] fix/syntax error --- bot/cogs/giveaway.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py index 106b2d1..2c5d630 100644 --- a/bot/cogs/giveaway.py +++ b/bot/cogs/giveaway.py @@ -40,17 +40,18 @@ async def add_giveaway_blacklist( return await interaction.followup.send('You are not an admin', ephemeral=True) winner_list: list[discord.Member] = [] - for (winner := winners.split(',')) in winners: - if (user := interaction.guild.get_member(winner)): - return await interaction.followup.send( - f'User {winner} not found', ephemeral=True - ) + winners = winners.split(',') + print(winners) + for winner in winners: + if (user := interaction.guild.get_member(winner)) is None: + return await interaction.followup.send(f'User {winner} not found', ephemeral=True) winner_list.append(user) ghost_list: list[discord.Member] = [] if ghosts: - for (ghost := ghosts.split(',')) in ghosts: - if (user := interaction.guild.get_member(ghost)): + ghosts = ghosts.split(',') + for ghost in ghosts: + if user := interaction.guild.get_member(ghost): return await interaction.followup.send( f'User {ghost} not found', ephemeral=True ) @@ -147,7 +148,7 @@ async def _check_blacklist_ga(self) -> None: now, ) for user in user_want_remove: - if (user := guild.get_member(int(user['user_id']))): + if user := guild.get_member(int(user['user_id'])): await user.remove_roles(blacklist_role) await conn.execute( 'UPDATE blacklist_ga SET has_role = FALSE WHERE user_id = $1', From 62abaf0d85bf8aa372ea84541fbaa0fc0dbf5191 Mon Sep 17 00:00:00 2001 From: hilmoo Date: Sat, 21 Sep 2024 21:52:42 +0700 Subject: [PATCH 14/14] fix/all code has passed testing --- bot/bot.py | 5 +---- bot/cogs/giveaway.py | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 88d6ac2..ac25edb 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -42,10 +42,7 @@ async def setup_hook(self) -> None: self.session = aiohttp.ClientSession() self.bot_app_info = await self.application_info() - if self.bot_app_info.team: - self.owner_ids = {m.id for m in self.bot_app_info.team.members} - else: - self.owner_id = self.bot_app_info.owner.id + self.owner_ids = {m.id for m in self.bot_app_info.team.members} await self.load_cogs() diff --git a/bot/cogs/giveaway.py b/bot/cogs/giveaway.py index 2c5d630..d84f56c 100644 --- a/bot/cogs/giveaway.py +++ b/bot/cogs/giveaway.py @@ -24,14 +24,14 @@ async def on_connect(self) -> None: @app_commands.command(name='blacklist', description='Blacklist a user from giveaway') @app_commands.describe( - big_GA='Yes if the giveaway is large than Rp50.000', + big='Yes if the giveaway is large than Rp50.000', winners='Winner of the giveaway. Example: 1234567890,0987654321', ghosts='Member who do not claim the giveaway. Example: 1234567890,0987654321', ) async def add_giveaway_blacklist( self, interaction: Interaction, - big_GA: bool, + big: bool, winners: str, ghosts: Optional[str] = None, ) -> None: @@ -41,9 +41,8 @@ async def add_giveaway_blacklist( winner_list: list[discord.Member] = [] winners = winners.split(',') - print(winners) for winner in winners: - if (user := interaction.guild.get_member(winner)) is None: + if (user := interaction.guild.get_member(int(winner))) is None: return await interaction.followup.send(f'User {winner} not found', ephemeral=True) winner_list.append(user) @@ -51,24 +50,23 @@ async def add_giveaway_blacklist( if ghosts: ghosts = ghosts.split(',') for ghost in ghosts: - if user := interaction.guild.get_member(ghost): + if (user := interaction.guild.get_member(int(ghost))) is None: return await interaction.followup.send( f'User {ghost} not found', ephemeral=True ) ghost_list.append(user) - if big_GA: - winner_day = 15 - ghosting_day = 7 - else: + if big: winner_day = 30 ghosting_day = 15 + else: + winner_day = 15 + ghosting_day = 7 - # change to days, seconds only for testing now = datetime.now(timezone.utc) - end_time_no_streak = now + timedelta(seconds=winner_day) - end_time_streak = now + timedelta(seconds=winner_day * 2) - end_time_ghosting = now + timedelta(seconds=ghosting_day) + end_time_no_streak = now + timedelta(days=winner_day) + end_time_streak = now + timedelta(days=winner_day * 2) + end_time_ghosting = now + timedelta(days=ghosting_day) blacklist_role = interaction.guild.get_role(BLACKLIST_GA_ROLE_ID) async with self.db_pool.acquire() as conn: @@ -83,12 +81,13 @@ async def add_giveaway_blacklist( await conn.execute( ''' UPDATE blacklist_ga - SET end_time = $2, status_user = $3 + SET end_time = $2, status_user = $3, cooldown_time = $4 WHERE user_id = $1 ''', winner.id, end_time_streak, 0, + end_time_streak, ) else: await conn.execute( @@ -106,7 +105,7 @@ async def add_giveaway_blacklist( await ghost.add_roles(blacklist_role) if ghost.id in streak_user: await conn.execute( - 'UPDATE blacklist_ga SET (end_time) VALUES ($2) WHERE user_id = $1', + 'UPDATE blacklist_ga SET end_time = $2 WHERE user_id = $1', ghost.id, end_time_ghosting, )