这里有新的discord.py-rewrite用户。
我正在编写一个具有不和谐接口的机器人:它连接到多个服务器(公会),我目前需要一个auth系统来限制其使用。我想我可以获得所有用户角色,并在以后需要时解析它们。所以我做了:
@client.event
async def on_ready():
...
for guild in client.guilds:
for member in guild.members:
for role in member.roles:
if role.name == "Test":
print("USER_ID: %d - ROLE: %s" % (member.id, role.name))
但我不喜欢它。我必须存储这些并且效率不高。此外,我必须使用后台协同程序刷新以检查新成员是否加入/更改角色。所以我的问题是:有没有一种方法可以在收到消息时检查相互公会的飞行用户角色?滚动官方API获取用户的相互公会的唯一方法是profile(),但作为一个机器人,我得到一个禁止错误,就像API说的那样。
@client.event
async def on_message(message):
...
profile = await message.author.profile()
discord.errors.Forbidden: FORBIDDEN (status code: 403): Bots cannot use this endpoint
(更新)ADDENDUM:
我甚至需要在私人消息中检查用户的角色,因此需要获取mutual_guild
我查看了文档,但我认为没有更简单的方法可以做到这一点。
@commands.command(pass_context=True)
async def test(self, ctx):
for role in ctx.guild.roles:
if role.name == 'Your role name':
#Code
只需在调用某个命令时,您可以检查已发送消息的公会上的每个角色,如果角色名称与您选择的角色名称匹配,它将执行某些代码。