简体   繁体   中英

Discord.py Ban Command doesn't works

Hi guys i'm coding a discord bot in python and i have ban and kick command. Well the main problem is that these commands are not working, And i don't know why: Please help, here is my code:

@client.command(aliases=['Kick','KICK','KİCK'])
@commands.has_permissions(kick_members=True)
async def kick(ctx, member : discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.send(f"{member.mention} is kicked.")

@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Kick Members** permission.")

@client.command(aliases=['Ban','BAN'])
@commands.has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, *, reason=None):
    await member.ban(reason=reason)
    await ctx.send(f'{member.mention} is banned.')

#ban someone in a server error
@ban.error
async def ban_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Ban Members** permission.")

@client.command(aliases=['Unban','UNBAN'])
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split('#')

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'{user.mention} is unbanned.')
            return

#unban someone in a server error
@unban.error
async def unban_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Ban Members** permission.

Edit: don't forget to import the following:

import discord
import os
from discord.ext import commands,tasks
from discord.ext.commands import has_permissions, CheckFailure

 @commands.command()
    @commands.has_permissions(ban_members=True)
    async def bAn(ctx,*, member : discord.Member = None, reason=None):
        if member is None:
            await ctx.send("Please mention someone to ban")
        if reason is None:
            reason = "Reason was not specified"
        await ctx.send(f'{member.mention} is banned.')
        await member.ban(reason=reason)      # could use ctx.guild.ban(member, reason=reason) here, works the same way though.

The above command is a rework of your ban command. Instead of client.commands() I used commands.command()

and declared Member as None and also specified a condition that if no member was mentioned, bot would ask the user to mention a user if no reason was mention, the default reason would be "no reason was specified" The kick command follows the same pattern as ban.

also, for unban you could use the following

@commands.command(aliases=['ub'])
    @commands.has_guild_permissions(ban_members=True)
    @commands.bot_has_permissions(ban_members=True)
    async def unban(ctx, member: discord.User = None, *, reason=None): # discord.User allows you to use discord ID/discordname#discriminator (674670744776474644 or Mystery R#6892)

if reason is None:
          reason = f"{ctx.author.name}#{ctx.author.discriminator} did not provide any reason"
if member is None:
          await ctx.send("Please write a ID#discriminator to unban")
x = len(reason)   
if x > 460: # 460 is the character limit of the reason in discord
          return await ctx.send('Reason must be less or equal to 460 characters')
else:
          await ctx.guild.unban(member, reason=reason) # add the response from bot after members are unbanned yourself after this line

@unban.error
async def unban_error(self,ctx, error): 
    if isinstance(error, commands.MemberNotFound):
                    await ctx.send("No member was found with the given argument")
    elif isinstance(error, commands.BotMissingPermissions):
                    await ctx.send("Bot is missing Ban Members permission(s) to run this command.")
    elif isinstance(error,commands.MissingPermissions):
                    await ctx.send("You are missing Ban Members permission(s) to run this command.")

Note: the command "ben" is ban which I changed to "ben" as I already had a ban command in another file!

在此处输入图像描述

解禁输出

I think that you have there in the arguments member: discord.Member, but discord is providing member mention. At least I am getting member.mention and then looping through the list of members in the guild, comparing member.mention with the member.mention provided and then I have the member and I can ban the member. Hope you understand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM