如何排除 discord.py 中的角色
How to exclude a role in discord.py
所以我有一个紧急命令,以防万一,我必须对每个人进行 DM,但是机器人有问题,因为 DM 会导致错误(机器人的角色是 "BOTS")
@bot.command(pass_context=True)
async def emergency(ctx, *, message: str):
if admin in [role.id for role in ctx.message.author.roles]:
for server_member in ctx.message.server.members:
await bot.send_message(server_member, message)
else:
await bot.say("DENIED, You do not have permission to use this command")
和错误
Traceback (most recent call last):
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: FORBIDDEN (status code: 403): Cannot send messages to this user
在其他一些情况下您会收到此消息(您已被阻止,该用户已完全禁用 DM,等等),因此每次都捕获错误并继续更有意义它发生了。
@bot.command(pass_context=True)
async def emergency(ctx, *, message: str):
if admin in [role.id for role in ctx.message.author.roles]:
for server_member in ctx.message.server.members:
try:
await bot.send_message(server_member, message)
except discord.Forbidden:
pass
else:
await bot.say("DENIED, You do not have permission to use this command")
如果您还想检查角色,可以使用 discord.utils.get
if get(server_member.roles, name="BOTS"):
# has role
else:
# does not have role
所以我有一个紧急命令,以防万一,我必须对每个人进行 DM,但是机器人有问题,因为 DM 会导致错误(机器人的角色是 "BOTS")
@bot.command(pass_context=True)
async def emergency(ctx, *, message: str):
if admin in [role.id for role in ctx.message.author.roles]:
for server_member in ctx.message.server.members:
await bot.send_message(server_member, message)
else:
await bot.say("DENIED, You do not have permission to use this command")
和错误
Traceback (most recent call last):
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
File "C:\Users\adamk\PycharmProjects\bot\venv\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: FORBIDDEN (status code: 403): Cannot send messages to this user
在其他一些情况下您会收到此消息(您已被阻止,该用户已完全禁用 DM,等等),因此每次都捕获错误并继续更有意义它发生了。
@bot.command(pass_context=True)
async def emergency(ctx, *, message: str):
if admin in [role.id for role in ctx.message.author.roles]:
for server_member in ctx.message.server.members:
try:
await bot.send_message(server_member, message)
except discord.Forbidden:
pass
else:
await bot.say("DENIED, You do not have permission to use this command")
如果您还想检查角色,可以使用 discord.utils.get
if get(server_member.roles, name="BOTS"):
# has role
else:
# does not have role