-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add giveaway blacklist feature #137
Merged
Merged
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b920eae
feat(temp-role): add temporary role system
hilmoo 8fb7c81
change description duration in the command
hilmoo 65293e6
add permission to add and remove temp roles
hilmoo 4f2fdbd
fix error logic
hilmoo 7349362
fix small bug
hilmoo 6203651
feat(giveaway): add blacklist feature for giveaways
hilmoo 71e8db3
fix
hilmoo b5eb1dc
improve syntax
hilmoo d6cfa60
fix/logic for giveaway blacklist
hilmoo 9418591
change function name
hilmoo bbac5c5
Merge remote-tracking branch 'origin/main' into feat/giveaway-blacklist
hilmoo 4469f07
feat/refactor blacklist GA system
hilmoo 8d80a70
fix/Refactor role handling in giveaway and temporary cogs
hilmoo 28bd7ed
fix/syntax error
hilmoo 62abaf0
fix/all code has passed testing
hilmoo f04d8d8
feat/Refactor giveaway blacklist command descriptions
hilmoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
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.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='Giveaway prizes. Example: 50000', | ||
winner='Winner of the giveaway. Example: 1234567890,0987654321', | ||
ghosting='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, | ||
) -> 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] = [] | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
winnerss.append(user) | ||
|
||
ghostss: list[discord.Member] = [] | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
) | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ghostss.append(user) | ||
|
||
if amount < GiveawayConfig.BIG_GIVEAWAY: | ||
winner_day: 15 | ||
ghosting_day: 7 | ||
else: | ||
winner_day: 30 | ||
ghosting_day: 15 | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
blacklist_role = interaction.guild.get_role(GiveawayConfig.BLACKLIST_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', | ||
) | ||
|
||
for winn in winnerss: | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await winn.add_roles(blacklist_role) | ||
if win in streak_user: | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await conn.execute( | ||
'UPDATE black_ga SET (end_time, status_user, cooldown_time) VALUES ($2, $3, $4) WHERE user_id = $1', | ||
winn.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, | ||
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) | ||
if ghost in streak_user: | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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' | ||
) | ||
|
||
embed = discord.Embed( | ||
title="Role Added", | ||
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( | ||
name="List of Winners", | ||
value=f"{', '.join([winner.mention for winner in winnerss])}", | ||
) | ||
embed.add_field( | ||
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: | ||
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), | ||
) | ||
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: | ||
hilmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 {blacklist_role.id} from user {user.id} (rm role)') | ||
|
||
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() | ||
|
||
|
||
async def setup(bot: WarnetBot) -> None: | ||
await bot.add_cog(Giveaway(bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example is too long for a parameter description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's too long, it helps users understand how to separate them if more than one is entered