【问题标题】:Get a role with discord.py通过 discord.py 获得一个角色
【发布时间】:2018-03-18 10:20:11
【问题描述】:

我正在尝试获取用户的角色来执行命令:

async def clear (ctx, n):
    if "Mod" in [y.name.lower() for y in ctx.message.author.roles]:
       //delete messages
    else:
       client.send_message(ctx.message.channel, "You are not allowed to use this command!")

普通用户使用!clear时,可以清除消息,但也会出现权限错误。

代码:

@bot.command(pass_context=True)
async def clear(ctx, n):
   if "mod" in [y.name.lower() for y in ctx.message.author.roles]:
       n = int(n)
       tn = n + 1
       async for x in bot.logs_from(ctx.message.channel, limit=tn):
          await bot.delete_messages(x)

          await bot.send_message(ctx.message.channel, "Deleted" + str(n) + "messages")
   elif not "mod" in [y.name.lower() for y in ctx.message.author.roles]:
       await bot.send_message(ctx.message.channel, "You need the **Mod** role to use this command!")

普通用户使用!clear时,可以清除消息,但也会出现权限错误。

解决方案:

@bot.command(pass_context=True)
async def clear(ctx, n):
   if "mod" in [y.name.lower() for y in ctx.message.author.roles]:
       n = int(n)
       msg = []
       tn = n + 1
       async for x in bot.logs_from(ctx.message.channel, limit=tn):
          msg.append(x)
          await bot.delete_messages(x)

       await bot.send_message(ctx.message.channel, "Deleted" + str(n) + "messages")
   elif not "mod" in [y.name.lower() for y in ctx.message.author.roles]:
       await bot.send_message(ctx.message.channel, "You need the **Mod** role to use this command!")

【问题讨论】:

  • 大写字母的“Mod”如何匹配name.lower()?如果删除消息 ever 真的运行,我会感到惊讶......
  • 它与“mod”@squaswin 发生同样的事情
  • 能否请您编辑您的帖子整个功能,这个问题是代码的产物,而不是您发送的内容。如果普通用户收到错误消息 并且 可以删除消息,这意味着删除调用实际上不在 if 块内...
  • 我添加了整个命令
  • 不是导致您的错误的问题,而是 1:您为什么在 for 循环中调用批量删除 (delete_messages),2:break 在做什么,3:它要发送"Deletednmessages" 它删除的每条消息,仍然存在一个问题,即带有大写字母的“Mod”永远不会存在于小写名称列表中,因此代码实际上不应该运行......

标签: python python-3.x discord discord.py


【解决方案1】:

拥有 Mod 角色的用户也至少拥有 @everyone 角色。所以你需要把else改成elif not "Mod" in [y.name.lower() for y in ctx.message.author.roles]:

【讨论】:

  • 这只是 else 语句的逻辑原因......用户拥有什么角色并不重要,如果“mod”不是其中之一的名称,则 if 块不应该是完全在运行。
  • 这样我得到了“mod”角色的权限错误
猜你喜欢
  • 2021-03-16
  • 2021-06-05
  • 2021-04-18
  • 2020-09-26
  • 2021-05-15
  • 2021-05-27
  • 2019-10-20
  • 2021-09-30
  • 1970-01-01
相关资源
最近更新 更多