[p]warn ask to ban when user not in server #6481

Merged
Ascensionn merged 18 commits from V3/develop into V3/develop 2024-12-08 19:42:23 +01:00
Ascensionn commented 2024-11-26 22:56:00 +01:00 (Migrated from github.com)

Description of the changes

image

This is to follow up with what @Flame442 said: "If a disruptive user is leaving before a moderator is able to take action, it may often make sense to consider them a troll and bypass the server's standard warnings track."

Instead of warning them, I check if they are in the discord guild; if they are not, then they will be banned.

Closes #6445

Have the changes in this PR been tested?

image

Note: the command here is "[p]wasd" because I tested it by implementing a new temporary cog.

### Description of the changes ![image](https://github.com/user-attachments/assets/ef7b7e57-fcd6-4953-bb2f-1f26dc26a3b6) This is to follow up with what @Flame442 said: "If a disruptive user is leaving before a moderator is able to take action, it may often make sense to consider them a troll and bypass the server's standard warnings track." Instead of warning them, I check if they are in the discord guild; if they are not, then they will be banned. Closes #6445 ### Have the changes in this PR been tested? ![image](https://github.com/user-attachments/assets/cf90bbb1-b02f-43ac-9c3b-0578e57a5c36) Note: the command here is "[p]wasd" because I tested it by implementing a new temporary cog.
Flame442 commented 2024-11-26 23:40:14 +01:00 (Migrated from github.com)

My original comment was not intended to be complete approval of the idea of having this command default to banning the member if they have left the server. I think it could be useful to have it as an option (whether that be by asking if they should be banned as a prompt when [p]warn is used on a member that left, or as a config option that causes [p]warn to ban when used on a member that has left), but I would consider it unexpected to happen automatically without any input from the server owner/mod. My comment was mostly intended to address the fact that this issue is one I would consider a fairly rare one to run into, and one that has a fairly easy manual second step to address it.

Regarding the implementation of this fix, the RawUserIdConverter would be the preferred way to accept both a mention or a user ID, and is the converter used by other parts of the bot with similar behavior (ie, [p]ban).

Additionally, you are currently failing the style checks. Please format your PR with black -l 99 using the instructions in our CONTRIBUTING.MD file.

My original comment was not intended to be complete approval of the idea of having this command default to banning the member if they have left the server. I think it could be useful to have it as an option (whether that be by asking if they should be banned as a prompt when `[p]warn` is used on a member that left, or as a config option that causes `[p]warn` to ban when used on a member that has left), but I would consider it unexpected to happen automatically without any input from the server owner/mod. My comment was mostly intended to address the fact that this issue is one I would consider a fairly rare one to run into, and one that has a fairly easy manual second step to address it. Regarding the implementation of this fix, the [`RawUserIdConverter`](https://docs.discord.red/en/stable/framework_commands.html#redbot.core.commands.converter.RawUserIdConverter) would be the preferred way to accept both a mention or a user ID, and is the converter used by other parts of the bot with similar behavior (ie, `[p]ban`). Additionally, you are currently failing the style checks. Please format your PR with `black -l 99` using the instructions in our [CONTRIBUTING.MD](https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/CONTRIBUTING.md#43-style) file.
Ascensionn commented 2024-11-28 02:47:27 +01:00 (Migrated from github.com)

ok, I can add that as a prompt then. The bot should say "[User]" has left the server, so I cannot warn them. Would you like to ban them instead? [Y/N]". If the mod/owner responds with "Y", [User] will be banned. Else, no action will be taken.

I will submit another PR request with the changes.

ok, I can add that as a prompt then. The bot should say "[User]" has left the server, so I cannot warn them. Would you like to ban them instead? [Y/N]". If the mod/owner responds with "Y", [User] will be banned. Else, no action will be taken. I will submit another PR request with the changes.
Ascensionn commented 2024-11-28 03:41:23 +01:00 (Migrated from github.com)

*Reminder: the reason I used "[p]wasd" here is because I had to create a new separate cog in order to test for this kind of command. After I finished testing it, I carefully put in the modified parts.

image

From this image, we can see that the bot eventually timed out after it hasn't received a proper command (either Y or N).

image
image

The bot only registers the responses it expects. Otherwise it will not do anything and eventually time out.

*Reminder: the reason I used "[p]wasd" here is because I had to create a new separate cog in order to test for this kind of command. After I finished testing it, I carefully put in the modified parts. ![image](https://github.com/user-attachments/assets/616fedcf-c11c-4301-858b-84af4659b57d) From this image, we can see that the bot eventually timed out after it hasn't received a proper command (either Y or N). ![image](https://github.com/user-attachments/assets/63519c00-26f5-46c5-b046-bf72ef6ed4d6) ![image](https://github.com/user-attachments/assets/4b28b8f2-3f93-4a19-abdb-2e4af9ad55fa) The bot only registers the responses it expects. Otherwise it will not do anything and eventually time out.
Flame442 (Migrated from github.com) requested changes 2024-11-29 23:01:26 +01:00
Flame442 (Migrated from github.com) commented 2024-11-29 22:43:36 +01:00

This should use RawUserIdConverter to handle the case where the user is no longer in the server. See the [p]ban command for an example. Similar to that command, it should be Unioning the Member and RawUserIdConverter, and having an isinstance(user, int) check to perform this banning logic instead of the existing warning behavior.

Additionally, the name of the function parameter defines the name a user will see in [p]help, so it should still be called either member or user, not identifier.

This should use [`RawUserIdConverter`](https://docs.discord.red/en/stable/framework_commands.html#redbot.core.commands.converter.RawUserIdConverter) to handle the case where the user is no longer in the server. See the [`[p]ban`](https://github.com/Cog-Creators/Red-DiscordBot/blob/33e0eac741955ce5b7e89d9b8f2f2712727af770/redbot/cogs/mod/kickban.py#L373-L396) command for an example. Similar to that command, it should be `Union`ing the `Member` and `RawUserIdConverter`, and having an `isinstance(user, int)` check to perform this banning logic instead of the existing warning behavior. Additionally, the name of the function parameter defines the name a user will see in `[p]help`, so it should still be called either `member` or `user`, not `identifier`.
Flame442 (Migrated from github.com) commented 2024-11-29 22:47:30 +01:00

Avoid having multiple send calls for the same input, as that spams the API. One way to handle this is to combine strings until one final send, but in this case the last send you have already says basically the same thing, so this message is redundant.

Avoid having multiple `send` calls for the same input, as that spams the API. One way to handle this is to combine strings until one final `send`, but in this case the last `send` you have already says basically the same thing, so this message is redundant.
Flame442 (Migrated from github.com) commented 2024-11-29 22:50:25 +01:00

If this is intended to be a part of the help text, it must be within the string directly below the function header. Otherwise, don't use string literals, use comments.

If this is intended to be a part of the help text, it must be within the string directly below the function header. Otherwise, don't use string literals, use comments.
Flame442 (Migrated from github.com) commented 2024-11-29 22:52:01 +01:00

fetch_user is a very heavily ratelimited call, and there is no reason for it to be used in this case since the actual user data isn't useful. As in the [p]ban command, just having self.bot.get_user(ident) or discord.Object(id=user) is enough to have something to pass to guild.ban(). You won't be able to use user.name, but just repeating the .id or saying "that user" is probably fine.

`fetch_user` is a very heavily ratelimited call, and there is no reason for it to be used in this case since the actual user data isn't useful. As in the `[p]ban` command, just having `self.bot.get_user(ident) or discord.Object(id=user)` is enough to have something to pass to `guild.ban()`. You won't be able to use `user.name`, but just repeating the `.id` or saying "that user" is probably fine.
Flame442 (Migrated from github.com) commented 2024-11-29 22:58:44 +01:00

Instead of manually defining a check, you should use MessagePredicate.yes_or_no (example) to handle more ways to say "yes" or "no" as a text response.

EDIT: I didn't know we already had a util for this, but now that buttons exist the best option is probably to use those. The ConfirmView utility should make it easy, see the examples in the docs.

Instead of manually defining a check, you should use ~~[`MessagePredicate.yes_or_no`](https://docs.discord.red/en/stable/framework_utils.html#redbot.core.utils.predicates.MessagePredicate.yes_or_no) ([example](https://github.com/Cog-Creators/Red-DiscordBot/blob/33e0eac741955ce5b7e89d9b8f2f2712727af770/redbot/cogs/admin/admin.py#L526-L531)) to handle more ways to say "yes" or "no" as a text response~~. EDIT: I didn't know we already had a util for this, but now that buttons exist the best option is probably to use those. The [`ConfirmView`](https://docs.discord.red/en/stable/framework_utils.html#redbot.core.utils.views.ConfirmView) utility should make it easy, see the examples in the docs.
Ascensionn commented 2024-11-30 03:52:25 +01:00 (Migrated from github.com)

Implemented the fixes you have mentioned @Flame442

Here is what happens when you warn the user who is not in the server:

image
image

Here is what happens when it times out after 10 seconds.

image

Implemented the fixes you have mentioned @Flame442 Here is what happens when you warn the user who is not in the server: ![image](https://github.com/user-attachments/assets/9f087bf4-a879-4099-ab6f-83d2dc11d2f3) ![image](https://github.com/user-attachments/assets/90be16e1-051a-4cb2-9568-23ea1668a943) Here is what happens when it times out after 10 seconds. ![image](https://github.com/user-attachments/assets/c083d7ed-6561-492a-b525-2c9e93a6e760)
Flame442 (Migrated from github.com) requested changes 2024-11-30 07:05:04 +01:00
Flame442 (Migrated from github.com) left a comment

If the ban is successful, it should create a modlog case (example).

If the ban is successful, it should create a modlog case ([example](https://github.com/Cog-Creators/Red-DiscordBot/blob/33e0eac741955ce5b7e89d9b8f2f2712727af770/redbot/cogs/mod/kickban.py#L215-L225)).
@ -386,6 +387,49 @@ class Warnings(commands.Cog):
or a custom reason if ``[p]warningset allowcustomreasons`` is set.
Flame442 (Migrated from github.com) commented 2024-11-30 06:58:14 +01:00

This try should only wrap around the ban, not the entire block of code.

This `try` should only wrap around the `ban`, not the entire block of code.
Flame442 (Migrated from github.com) commented 2024-11-30 06:58:28 +01:00
                confirm = ConfirmView(ctx.author, timeout=30)

10s is a bit short.

```suggestion confirm = ConfirmView(ctx.author, timeout=30) ``` 10s is a bit short.
Flame442 (Migrated from github.com) commented 2024-11-30 06:59:54 +01:00
                    _("User `{user}` is not in the server. Would you like to ban them instead?").format(user=user),
                    view=confirm,

Allow the string to be translated (you might need to re-black format after accepting some of these changes).

```suggestion _("User `{user}` is not in the server. Would you like to ban them instead?").format(user=user), view=confirm, ``` Allow the string to be translated (you might need to re-black format after accepting some of these changes).
Flame442 (Migrated from github.com) commented 2024-11-30 07:00:32 +01:00
                if confirm.result:

No need to have a whole extra message for a slightly different case.

```suggestion if confirm.result: ``` No need to have a whole extra message for a slightly different case.
Flame442 (Migrated from github.com) commented 2024-11-30 07:03:02 +01:00

If the bot does not have add_reactions permissions, this will cause it to claim an error occurred while trying to ban a user, which isn't quite true. Either this should use await ctx.tick() which handles that case correctly, or it should send a message instead to confirm.

If the bot does not have `add_reactions` permissions, this will cause it to claim an error occurred while trying to ban a user, which isn't quite true. Either this should use `await ctx.tick()` which handles that case correctly, or it should send a message instead to confirm.
Flame442 (Migrated from github.com) commented 2024-11-30 07:03:19 +01:00
                    confirm.message = await ctx.send(_("No action taken."))

Translations.

```suggestion confirm.message = await ctx.send(_("No action taken.")) ``` Translations.
Flame442 (Migrated from github.com) commented 2024-11-30 07:03:38 +01:00
                await ctx.send(_("I don't have permission to ban this user."))
            except discord.HTTPException:
                await ctx.send(_("An error occurred while trying to ban the user."))

Translations.

```suggestion await ctx.send(_("I don't have permission to ban this user.")) except discord.HTTPException: await ctx.send(_("An error occurred while trying to ban the user.")) ``` Translations.
@ -389,0 +415,4 @@
user,
ctx.author,
reason,
until=None,
Flame442 (Migrated from github.com) commented 2024-11-30 06:57:47 +01:00

Rather than asking and hoping we have perms, let's add a check to exit slightly earlier if the bot doesn't have them

            if not ctx.channel.permissions_for(ctx.guild.me).ban_members:
                await ctx.send(_("User {user} is not in the server."),format(user=user))
                return
            user_obj = self.bot.get_user(user) or discord.Object(id=user)
Rather than asking and hoping we have perms, let's add a check to exit slightly earlier if the bot doesn't have them ```suggestion if not ctx.channel.permissions_for(ctx.guild.me).ban_members: await ctx.send(_("User {user} is not in the server."),format(user=user)) return user_obj = self.bot.get_user(user) or discord.Object(id=user) ```
Ascensionn (Migrated from github.com) reviewed 2024-12-02 03:10:46 +01:00
@ -386,6 +387,49 @@ class Warnings(commands.Cog):
or a custom reason if ``[p]warningset allowcustomreasons`` is set.
Ascensionn (Migrated from github.com) commented 2024-12-02 03:10:46 +01:00

yep, now I added a checker

yep, now I added a checker
Flame442 (Migrated from github.com) requested changes 2024-12-02 03:28:52 +01:00
@ -388,1 +389,4 @@
guild = ctx.guild
member = None
if isinstance(user, discord.Member):
member = user
Flame442 (Migrated from github.com) commented 2024-12-02 03:25:01 +01:00

I had the string simply stating that the user was not in the server, because when the [p]warn command is used, that is the best response the bot can give. Stating as a response that the bot cannot ban users, without explaining why it is trying to ban is confusing. I think it is better to just state that the user isn't in the server without worrying about the fact that it could have attempted to ban.

I had the string simply stating that the user was not in the server, because when the `[p]warn` command is used, that is the best response the bot can give. Stating as a response that the bot cannot ban users, without explaining _why_ it is trying to ban is confusing. I think it is better to just state that the user isn't in the server without worrying about the fact that it _could_ have attempted to ban.
Flame442 (Migrated from github.com) commented 2024-12-02 03:27:05 +01:00

ctx.tick is a shorthand for if permissions_for.add_reactions: add_reaction(CHECK). That's why I said before you could just replace the add_reaction call with a call to tick, and let its error handling do the trick.

`ctx.tick` is a shorthand for `if permissions_for.add_reactions: add_reaction(CHECK)`. That's why I said before you could just replace the `add_reaction` call with a call to `tick`, and let its error handling do the trick.
Ascensionn (Migrated from github.com) reviewed 2024-12-02 06:35:51 +01:00
@ -388,1 +389,4 @@
guild = ctx.guild
member = None
if isinstance(user, discord.Member):
member = user
Ascensionn (Migrated from github.com) commented 2024-12-02 06:35:51 +01:00

oh ok, thank you for the clarification. Yes you're right, it would be strange to see that the bot suddenly prompts up with an error that it cannot ban a person when the [p]warn command is evoked. In that case, would you think it makes more sense for the bot to check "if not ctx.channel.permissions_for(ctx.guild.me).ban_members:" and then put the call to the ban() function in that if statement?

oh ok, thank you for the clarification. Yes you're right, it would be strange to see that the bot suddenly prompts up with an error that it cannot ban a person when the [p]warn command is evoked. In that case, would you think it makes more sense for the bot to check "if not ctx.channel.permissions_for(ctx.guild.me).ban_members:" and then put the call to the ban() function in that if statement?
Ascensionn (Migrated from github.com) reviewed 2024-12-02 06:38:58 +01:00
@ -388,1 +389,4 @@
guild = ctx.guild
member = None
if isinstance(user, discord.Member):
member = user
Ascensionn (Migrated from github.com) commented 2024-12-02 06:38:58 +01:00

In that case, do you want me to remove the "try...except"? I think the try..except block will be pointless then. All I need to do is add the check "if not ctx.channel.permissions_for(ctx.guild.me).ban_members:" before calling the ban() function. Because this ensures that there are no permission issues. However, as I didn't write the ban function, there could be another possible error, which is why I wanted to keep the try...except block in there.

In that case, do you want me to remove the "try...except"? I think the try..except block will be pointless then. All I need to do is add the check "if not ctx.channel.permissions_for(ctx.guild.me).ban_members:" before calling the ban() function. Because this ensures that there are no permission issues. However, as I didn't write the ban function, there could be another possible error, which is why I wanted to keep the try...except block in there.
Flame442 (Migrated from github.com) reviewed 2024-12-02 14:52:04 +01:00
@ -388,1 +389,4 @@
guild = ctx.guild
member = None
if isinstance(user, discord.Member):
member = user
Flame442 (Migrated from github.com) commented 2024-12-02 14:52:04 +01:00

No. It makes no sense to ask if the user should be banned if the bot is not going to be able to ban them. A simpler failure message and an even earlier exist is sufficient.

No. It makes no sense to ask if the user should be banned if the bot is not going to be able to ban them. A simpler failure message and an even earlier exist is sufficient.
Flame442 (Migrated from github.com) reviewed 2024-12-02 14:58:06 +01:00
@ -388,1 +389,4 @@
guild = ctx.guild
member = None
if isinstance(user, discord.Member):
member = user
Flame442 (Migrated from github.com) commented 2024-12-02 14:58:06 +01:00

There is always the potential for error, but if an error you cannot think of happens, you probably don't want to suppress it. You want to be able to identify it so you know if it is something that needs to be dealt with. Any API call can error if the internet goes out, but it doesn't make sense to wrap every API call in a try just in case.

I don't think it hurts to keep an HTTPException try on the ban call, since there are other cases that do make sense to catch, such as if the user is already banned. But the bot not having permissions is an obvious one that makes sense to have additional pre-check for. The try on the tick would be unnecessary.

There is always the potential for error, but if an error you cannot think of happens, you probably _don't_ want to suppress it. You want to be able to identify it so you know if it is something that needs to be dealt with. Any API call can error if the internet goes out, but it doesn't make sense to wrap every API call in a `try` just in case. I don't think it hurts to keep an `HTTPException` `try` on the `ban` call, since there are other cases that do make sense to catch, such as if the user is already banned. But the bot not having permissions is an obvious one that makes sense to have additional pre-check for. The `try` on the `tick` would be unnecessary.
Ascensionn (Migrated from github.com) reviewed 2024-12-05 01:53:38 +01:00
@ -388,1 +389,4 @@
guild = ctx.guild
member = None
if isinstance(user, discord.Member):
member = user
Ascensionn (Migrated from github.com) commented 2024-12-05 01:53:38 +01:00

Sorry, I misinterpreted what you were correcting, but yes, I see what ctx.tick() does. I just wanted to keep it consistent with what the other commands did, and I saw that they were doing the add_reaction() call. Fixed it.

Sorry, I misinterpreted what you were correcting, but yes, I see what ctx.tick() does. I just wanted to keep it consistent with what the other commands did, and I saw that they were doing the add_reaction() call. Fixed it.
Flame442 (Migrated from github.com) reviewed 2024-12-05 03:26:53 +01:00
@ -388,1 +389,4 @@
guild = ctx.guild
member = None
if isinstance(user, discord.Member):
member = user
Flame442 (Migrated from github.com) commented 2024-12-05 03:26:52 +01:00

I'm not sure which ones you mean, in this file at least they are all using tick.

I'm not sure which ones you mean, in this file at least they are all using `tick`.
Ascensionn (Migrated from github.com) reviewed 2024-12-05 04:06:22 +01:00
@ -388,1 +389,4 @@
guild = ctx.guild
member = None
if isinstance(user, discord.Member):
member = user
Ascensionn (Migrated from github.com) commented 2024-12-05 04:06:22 +01:00

nevermind. I swore I saw there was some mention of it. Must've been in a different file.

nevermind. I swore I saw there was some mention of it. Must've been in a different file.
Flame442 (Migrated from github.com) requested changes 2024-12-08 19:23:26 +01:00
@ -386,6 +387,49 @@ class Warnings(commands.Cog):
or a custom reason if ``[p]warningset allowcustomreasons`` is set.
Flame442 (Migrated from github.com) commented 2024-12-08 18:59:25 +01:00
            await ctx.tick()
```suggestion await ctx.tick() ```
Flame442 (Migrated from github.com) commented 2024-12-08 19:19:48 +01:00
                        "hackban",
```suggestion "hackban", ```
Flame442 (Migrated from github.com) approved these changes 2024-12-08 19:41:36 +01:00
Flame442 (Migrated from github.com) left a comment

Thanks for contributing!

Thanks for contributing!
Sign in to join this conversation.
No reviewers
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!6481
No description provided.