首页 > 解决方案 > Discord.py:检查用户是否具有列表中的角色 ID 不起作用

问题描述

我目前正在为我的 Discord.py 机器人进行反应角色配置,我遇到了一个问题,试图将用户列入黑名单以获取角色。这是我的代码:

    cursor.execute(f"SELECT blacklists FROM reactionroles WHERE message_id = {payload.message_id} AND emojis = '{reaction}'")

    blacklist_result = cursor.fetchone()

    member = payload.member

    rolesUserHas = member.roles
    rolesIDuserHas = []

    for role in rolesUserHas:
      rolesIDuserHas.append(role.id)
      print(rolesIDuserHas)

    if any(item in blacklist_result for item in rolesIDuserHas):
      await member.send(f'{x_mark} You are blacklisted from picking up roles from that message.')
      return
    else:
      pass

然而,当我给自己一个列入黑名单的角色时,它并没有取消或通知我,它只是继续这个过程,就好像它认为我没有这个角色一样。没有给出错误,我不知道为什么会这样。

标签: pythonsqlitediscorddiscord.pydiscord.py-rewrite

解决方案


Note that everyone id attribute in discord.py is a base 10 integer, and not a string. Either convert your database results to integers, or the ids to strings:

role_ids = list(map(str, [r.id for r in member.roles]))

推荐阅读