【发布时间】: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