Skip to content
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

[IMPROVEMENT] Show an error message in case the user already left when kicking #138

Merged
Merged
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
2 changes: 2 additions & 0 deletions src/cmds/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ async def kick(self, ctx: ApplicationContext, user: Member, reason: str, evidenc
-> Interaction | WebhookMessage:
"""Kick a user from the server."""
member = await self.bot.get_member_or_user(ctx.guild, user.id)
if not isinstance(member, discord.Member):
return await ctx.respond("User seems to have already left the server.")
if not member:
return await ctx.respond(f"User {user} not found.")
if member_is_staff(member):
Expand Down
19 changes: 19 additions & 0 deletions tests/src/cmds/core/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ async def test_kick_success(self, ctx, guild, bot, session):
ctx.guild.kick.assert_called_once_with(user=user_to_kick, reason="Violation of rules")
ctx.respond.assert_called_once_with("User to Kick got the boot!")

@pytest.mark.asyncio
async def test_kick_fail_user_left(self, ctx, guild, bot, session):
ctx.user = helpers.MockMember(id=1, name="Test Moderator")
user_to_kick = helpers.MockMember(id=2, name="User to Kick", bot=False)
ctx.guild = guild
ctx.guild.kick = AsyncMock()
bot.get_member_or_user = AsyncMock(return_value=None)

# Ensure the member_is_staff mock doesn't block execution
with patch('src.cmds.core.user.member_is_staff', return_value=False):
cog = user.UserCog(bot)
await cog.kick.callback(cog, ctx, user_to_kick, "Violation of rules")

# Assertions
bot.get_member_or_user.assert_called_once_with(ctx.guild, user_to_kick.id)
ctx.guild.kick.assert_not_called() # No kick should occur
ctx.respond.assert_called_once_with("User seems to have already left the server.")


def test_setup(self, bot):
"""Test the setup method of the cog."""
# Invoke the command
Expand Down
Loading