当前位置: 首页 > 知识库问答 >
问题:

与ban命令不一致的问题。py(重写分支)

史旺
2023-03-14

我一直在用discord编程一个机器人。py(重写分支)和我想添加一个ban命令。bot仍然没有禁止该成员,只是显示了一个错误:

@client.command(aliases=['Ban'])
async def ban(ctx,member: discord.Member, days: int = 1):
    if "548841535223889923" in (ctx.author.roles):
        await client.ban(member, days)
        await ctx.send("Banned".format(ctx.author))
    else:
        await ctx.send("You don't have permission to use this command.".format(ctx.author))
        await ctx.send(ctx.author.roles)      

它将禁止ping用户并确认它确实禁止了ping

共有2个答案

安浩瀚
2023-03-14
async def ban(ctx, member: discord.Member=None, *, reason=None):
  if reason:
    await member.send(f'You got banned from {ctx.guild.name} by {ctx.author}, reason: ```{reason}```')
    
    await member.ban()
    
    await ctx.send(f'{member.mention} got banned by {ctx.author.mention} with reason: ```{reason}```')
  if reason is None: 
    await ctx.send("please specify a reason")
汪文光
2023-03-14

Member.rolesRole对象的列表,而不是字符串。您可以使用discord.utils.get使用id(作为int)搜索该列表。

from discord.utils import get

@client.command(aliases=['Ban'])
async def ban(ctx, member: discord.Member, days: int = 1):
    if get(ctx.author.roles, id=548841535223889923):
        await member.ban(delete_message_days=days)
        await ctx.send("Banned {}".format(ctx.author))
    else:
        await ctx.send("{}, you don't have permission to use this command.".format(ctx.author))
        await ctx.send(ctx.author.roles)

也不再有Client.ban协程,并且Member.ban的附加参数必须作为关键字参数传递。

 类似资料:
  • 我正在尝试修复我今天遇到的一个错误,错误如下: C:\用户\nalfo\OneDrive\桌面\不和谐机器人\NetSync\命令\审核\ban.js:15let{user}=message.mentions.members.first () || message.guild.members.get(参数[0]);^ TypeError:无法对(message.indications.member

  • 我在我的discord机器人上遇到了一个问题。命令不起作用。我认为代码不是问题。除了命令外,一切都很完美。不和谐,如果我使用!在这里测试mytext,什么都不会发生。 提前谢谢你!

  • 上面的代码正在运行。 我没有得到任何错误,但我也希望机器人显示他们是afk的原因,当他们提到。有人能帮忙吗?

  • 这是我的密码 我确实检查了这个discord bot和这个命令的每个其他cog(仅存在于此…)。为什么我总是犯这个错误? 我所做的:我注释掉了那段代码,再次启动bot,它给了我相同的错误,但是命令名不同,我注释了所有显示的命令11次(尽管它们只在一个地方定义)。但我总是犯这样的错误! 这个机器人有超过240个命令,它与问题有关吗? 我能做什么?

  • 我得到了一个unban命令代码,在控制台中出现以下错误: (节点:9348)未处理的PromisejectionWarning:TypeError:无法读取未定义的at对象的属性“member”。在客户端执行(C:\Users\19nik\Documents\GitHub\bot project\commands\unban.js:9:22)。(C:\Users\19nik\Documents\G

  • 因此,我制作了一些代码,允许我将编辑过的消息记录到某个频道。代码如下: 但是,当我运行代码时,我得到错误消息: 忽略on_message_edit Traceback(最近一次调用last)中的异常:文件“C:\Users\jackt\AppData\Local\Programs\Python39\lib\site packages\discord\client.py”,第333行,在运行事件等待