Python Discord.py-ban命令

Python Discord.py-ban命令,python,discord.py,Python,Discord.py,我有基本设置,所以只有管理员可以禁止。我想发出ban命令,但我不知道怎么做 我建议使用discord.ext.commands来生成命令,这样更容易使用。禁止的功能是。这是一个使用discord.ext.commands的示例: if message.content.upper().startswith('!BAN'): if "449706643710541824" in [role.id for role in message.author.roles]:

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

我建议使用
discord.ext.commands
来生成命令,这样更容易使用。禁止的功能是。这是一个使用
discord.ext.commands
的示例:

    if message.content.upper().startswith('!BAN'):
        if "449706643710541824" in [role.id for role in message.author.roles]:
            await
bot=commands.bot(command_prefix=“!”)
@命令(pass\u context=True)
异步定义禁止(成员:discord.member,天数:int=1):
如果[message.author.roles中角色的role.id]中的“449706643701541824”:
等待bot.ban(成员,天)
其他:
等待bot.say(“您没有使用此命令的权限”)
bot.run(“”)

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

#有理由禁止用户
@client.command()
@命令。有任何角色(“钥匙刀大师”、“预言家”)
异步定义禁止(ctx,成员:discord.User=None,reason=None):
如果成员==无或成员==ctx.message.author:
等待ctx.channel.send(“你不能禁止自己”)
返回
如果原因==无:
reason=“因为我是个混蛋!”
message=f“您因{reason}被禁止进入{ctx.guild.name}”
等待成员。发送(消息)
#等待ctx.guild.ban(成员,reason=reason)
等待ctx.channel.send(f“{member}被禁止!”)
这是最好的

bot = commands.Bot(command_prefix = "!")

@bot.command(pass_context = True)
async def ban(member: discord.Member, days: int = 1):
    if "449706643710541824" in [role.id for role in message.author.roles]:
        await bot.ban(member, days)
    else:
        await bot.say("You don't have permission to use this command.")

bot.run("<TOKEN>")
试试这个:

import discord#导入discord模块。
从discord.ext导入命令#导入discord扩展。
#下面的代码验证“客户端”。
client=commands.Bot(command_prefix='pb?'))
#下面的代码存储令牌。
token=“您的代币”
'''
如果您公开有任何错误,将显示以下代码。如果您不想在输出shell中显示它们,这将非常有用。
'''
@客户端事件
命令上的异步定义错误(ctx,错误):
如果isinstance(错误,commands.MissingRequiredArgument):
等待ctx发送('请通过所有要求:滚动眼睛:'。)
如果isinstance(错误,commands.MissingPermissions):
等待ctx.send(“你没有所有的要求:愤怒:”)
#下面的代码禁止播放。
@client.command()
@commands.has_权限(ban_members=True)
异步定义禁止(ctx,成员:discord.member,*,reason=None):
等待成员。禁止(原因=原因)
#下面的代码将解压玩家。
@client.command()
@commands.has_权限(administrator=True)
异步def unban(ctx,*,成员):
禁止的用户=等待ctx.guild.bans()
成员名称,成员鉴别器=member.split(“#”)
对于禁用用户中的禁用条目:
user=ban_entry.user
如果(user.name,user.discriminator)=(member\u name,member\u discriminator):
等待ctx.guild.unban(用户)
等待ctx.send(f'Unbanned{user.notice})
返回
#下面的代码运行bot。
client.run(令牌)

我希望这有帮助,祝你好运

禁止命令?其实很简单

async def ban(ctx, member : discord.Member, reason=None):
    """Bans a user"""
    if reason == None:
        await ctx.send(f"Woah {ctx.author.mention}, Make sure you provide a reason!")
    else:
        messageok = f"You have been banned from {ctx.guild.name} for {reason}"
        await member.send(messageok)
        await member.ban(reason=reason)

如果你知道kick,你只需要将
user.kick
更改为
user.ban
,因为
user.kick
将踢一个成员,这意味着
user.ban
将禁止一个成员。

这是我使用的ban命令,对我来说效果非常好。如果你愿意,你有完全的权限使用整个东西

@commands.has_permissions(ban_members=True)
@bot.command()
async def ban(ctx, user: discord.Member, *, reason="No reason provided"):
        await user.ban(reason=reason)
        ban = discord.Embed(title=f":boom: Banned {user.name}!", description=f"Reason: {reason}\nBy: {ctx.author.mention}")
        await ctx.message.delete()
        await ctx.channel.send(embed=ban)
        await user.send(embed=ban)

根据Discord官方文件:-

@bot.command(name=“ban”,help=“命令禁止用户”)
@commands.has_权限(ban_members=True)
异步定义禁止(ctx,成员:discord.member,*,reason=None):
“”“禁止用户的命令。检查!帮助禁止”“”
尝试:
等待成员。禁止(原因=原因)
等待ctx.message.delete()
wait ctx.channel.send(f'{member.name}已被服务器禁止'
f'Reason:{Reason}')
除例外情况外:
等待ctx.channel.send(f“Bot没有足够的权限禁止某人。请升级权限”)
@bot.command(name=“unban”,help=“对unban用户的命令”)
@commands.has_权限(administrator=True)
异步定义unban(ctx,*,成员id:int):
“”“取消绑定用户的命令。检查!帮助取消绑定”“”
等待ctx.guild.unban(discord.Object(id=member_id))
等待ctx.send(f“Unban{member_id}”)

也许您真的尝试在代码中以某种方式使用该命令?请为您的答案添加一些详细信息,而不是简单地发布没有上下文的代码段。确定。下次我会这么做的!我总是建议将令牌存储在程序的主文件中。op可能会意外地分担不知道风险的问题是关于禁止,而不是踢。我最真诚的道歉!我没怎么注意,所以犯了一个很大的错误。我只是编辑以匹配。再一次,对不起!
async def ban(self, ctx, member:discord.Member, *, reason=None):
    guild = ctx.guild
    author = ctx.message.author
    if author.guild_permissions.administrator == False:
        embed4=discord.Embed(color=discord.Colour.red(), timestamp=datetime.datetime.utcnow(), title="Missing Permissions!", description="You don't have the required permissions to use this command!")
        message1 = await ctx.send(embed=embed4)    
        sleeper=5
        await asyncio.sleep(sleeper) 
        await message1.delete()
        return  
    if member.guild_permissions.administrator and member != None:
        embed=discord.Embed(color=discord.Colour.red(), title="Administrator", description="This user is an administrator and is not allowed to be banned.")
        message2 = await ctx.send(embed=embed)
        sleeper=5
        await asyncio.sleep(sleeper)
        await message2.delete()
        return
    if reason == None:
        embed1=discord.Embed(color=discord.Colour.red(), title="Reason Required!", description="You must enter a reason to ban this member.")    
        message3 = ctx.send(embed=embed1)
        sleeper=5
        await asyncio.sleep(sleeper)
        await message3.delete()
        return
    else:
        guild = ctx.guild
        await member.ban()
        embed2=discord.Embed(color=discord.Colour.green(), timestamp=datetime.datetime.utcnow(), title="Member Banned", description=f"Banned: {member.mention}\n Moderator: **{author}** \n Reason: **{reason}**")
        embed3=discord.Embed(color=discord.Colour.green(), timestamp=datetime.datetime.utcnow(), title=f"You've been banned from **{guild}**!", description=f"Target: {member.mention}\nModerator: **{author.mention}** \n Reason: **{reason}**")
        message4 = await ctx.send(embed=embed2)
        message5 = await ctx.send("✔ User has been notified.")
        sleeper=5
        await asyncio.sleep(sleeper)
        await message4.delete()
        await message5.delete()
        await member.send(embed=embed3)
@bot.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member :  discord.Member, *,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 member.ban(reason=reason)
    await ctx.send(f"{member} is banned!")