Discord.py 禁止命令

Blo*_*ody 2 python discord.py

    if message.content.upper().startswith('!BAN'):
        if "449706643710541824" in [role.id for role in message.author.roles]:
            await
Run Code Online (Sandbox Code Playgroud)

我有基本设置,所以只有管理员可以禁止。我想发出禁令命令,但我不知道该怎么做。

Pix*_*ite 8

尝试这个:

import discord #Imports the discord module.
from discord.ext import commands #Imports discord extensions.

#The below code verifies the "client".
client = commands.Bot(command_prefix='pb?')
#The below code stores the token.
token = "Your token"

'''
The below code displays if you have any errors publicly. This is useful if you don't want to display them in your output shell.
'''
@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send('Please pass in all requirements :rolling_eyes:.')
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You dont have all the requirements :angry:")

#The below code bans player.
@client.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member : discord.Member, *, reason = None):
    await member.ban(reason = reason)

#The below code unbans player.
@client.command()
@commands.has_permissions(administrator = True)
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'Unbanned {user.mention}')
            return

#The below code runs the bot.
client.run(token)

Run Code Online (Sandbox Code Playgroud)

我希望这会有所帮助,祝你的机器人好运!


小智 6

我为我的机器人获得的禁令命令是,显然不要保留禁令部分的注释,当我不知道如何将其锁定到角色时,我只是将其放在那里

#bans a user with a reason
@client.command()
@commands.has_any_role("Keyblade Master","Foretellers")
async def ban (ctx, member:discord.User=None, reason =None):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    # await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")
Run Code Online (Sandbox Code Playgroud)