-
-
Notifications
You must be signed in to change notification settings - Fork 41
Temp Ban command & Unban Task #515
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e0c3b0d
Added a tempban command and a function that gets all guilds from the db
petabyte-imo f183569
Fixed the tempban_check looped task and added some comments
petabyte-imo c3ca0cc
Fixed the tempban_check looped task and added some comments
petabyte-imo 49d6e45
oopsie
petabyte-imo 707e161
Added reason to unban
petabyte-imo e9868f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] edcda8f
Merge branch 'main' into tempban
c035205
Merge branch 'allthingslinux:main' into tempban
petabyte-imo bbd8fb6
Added a set_tempban_expired and get_expired_tempbans function and cha…
petabyte-imo e293eab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c5e0d48
Made stuff a bit cleaner and fix some issues related to tempban
petabyte-imo 46dbf10
Merge branch 'allthingslinux:main' into tempban
petabyte-imo 0f2a588
oopsiess
petabyte-imo d564cad
Refactor tempban & optimize
1cc97ef
Merge branch 'allthingslinux:main' into tempban
petabyte-imo ace679e
Merge branch 'main' into tempban
electron271 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 hidden or 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
This file contains hidden or 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,113 @@ | ||
| from datetime import UTC, datetime | ||
|
|
||
| import discord | ||
| from discord.ext import commands, tasks | ||
| from loguru import logger | ||
|
|
||
| from prisma.enums import CaseType | ||
| from tux.bot import Tux | ||
| from tux.utils import checks | ||
| from tux.utils.flags import TempBanFlags, generate_usage | ||
| from tux.utils.functions import parse_time_string | ||
|
|
||
| from . import ModerationCogBase | ||
|
|
||
|
|
||
| class TempBan(ModerationCogBase): | ||
| def __init__(self, bot: Tux) -> None: | ||
| super().__init__(bot) | ||
| self.tempban.usage = generate_usage(self.tempban, TempBanFlags) | ||
| self.tempban_check.start() | ||
|
|
||
| @commands.hybrid_command(name="tempban", aliases=["tb"]) | ||
| @commands.guild_only() | ||
| @checks.has_pl(3) | ||
| async def tempban( | ||
| self, | ||
| ctx: commands.Context[Tux], | ||
| member: discord.Member, | ||
| *, | ||
| flags: TempBanFlags, | ||
| ) -> None: | ||
| """ | ||
| Temporarily ban a member from the server. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ctx : commands.Context[Tux] | ||
| The context in which the command is being invoked. | ||
| member : discord.Member | ||
| The member to ban. | ||
| flags : TempBanFlags | ||
| The flags for the command. | ||
|
|
||
| Raises | ||
| ------ | ||
| discord.Forbidden | ||
| If the bot is unable to ban the user. | ||
| discord.HTTPException | ||
| If an error occurs while banning the user. | ||
| """ | ||
| if not ctx.guild: | ||
| logger.warning("Ban command used outside of a guild context.") | ||
| return | ||
|
|
||
| if not await self.check_conditions(ctx, member, ctx.author, "temporarily ban"): | ||
| return | ||
|
|
||
| duration = parse_time_string(f"{flags.expires_at}d") | ||
| expires_at = datetime.now(UTC) + duration | ||
|
|
||
| try: | ||
| await self.send_dm(ctx, flags.silent, member, flags.reason, action="temporarily banned") | ||
| await ctx.guild.ban(member, reason=flags.reason, delete_message_days=flags.purge_days) | ||
| except (discord.Forbidden, discord.HTTPException) as e: | ||
| logger.error(f"Failed to temporarily ban {member}. {e}") | ||
| await ctx.send(f"Failed to temporarily ban {member}. {e}", delete_after=30, ephemeral=True) | ||
| return | ||
|
|
||
| case = await self.db.case.insert_case( | ||
| case_user_id=member.id, | ||
| case_moderator_id=ctx.author.id, | ||
| case_type=CaseType.TEMPBAN, | ||
| case_reason=flags.reason, | ||
| guild_id=ctx.guild.id, | ||
| case_expires_at=expires_at, | ||
| case_tempban_expired=False, | ||
| ) | ||
|
|
||
| await self.handle_case_response(ctx, CaseType.TEMPBAN, case.case_number, flags.reason, member) | ||
|
|
||
| @tasks.loop(hours=1) | ||
| async def tempban_check(self) -> None: | ||
| expired_temp_bans = await self.db.case.get_expired_tempbans() | ||
|
|
||
| for temp_ban in expired_temp_bans: | ||
| guild = self.bot.get_guild(temp_ban.guild_id) or await self.bot.fetch_guild(temp_ban.guild_id) | ||
| if not guild: | ||
| logger.error(f"Failed to get guild with ID {temp_ban.guild_id} for tempban check.") | ||
| continue | ||
|
|
||
| try: | ||
| ban_entry = await guild.fetch_ban(discord.Object(id=temp_ban.case_user_id)) | ||
| await guild.unban(ban_entry.user, reason=f"Tempban expired | Case number: {temp_ban.case_number}") | ||
|
|
||
| await self.db.case.set_tempban_expired(temp_ban.case_number, temp_ban.guild_id) | ||
| await self.db.case.insert_case( | ||
| guild_id=temp_ban.guild_id, | ||
| case_user_id=temp_ban.case_user_id, | ||
| case_moderator_id=temp_ban.case_moderator_id, | ||
| case_type=CaseType.UNTEMPBAN, | ||
| case_reason="Expired tempban", | ||
| case_tempban_expired=True, | ||
| ) | ||
| logger.debug(f"Unbanned user with ID {temp_ban.case_user_id} | Case number {temp_ban.case_number}") | ||
|
|
||
| except (discord.Forbidden, discord.HTTPException, discord.NotFound) as e: | ||
| logger.error( | ||
| f"Failed to unban user with ID {temp_ban.case_user_id} | Case number {temp_ban.case_number}. Error: {e}", | ||
| ) | ||
|
|
||
|
|
||
| async def setup(bot: Tux) -> None: | ||
| await bot.add_cog(TempBan(bot)) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.