【问题标题】:Get the list of name of all existing roles of Discord server - Discord.py获取 Discord 服务器所有现有角色的名称列表 - Discord.py
【发布时间】:2020-05-06 16:17:09
【问题描述】:
import discord

def read_token():
    with open("token.txt", "r") as f:
        lines = f.readlines()
        return lines[0].strip()

token = read_token()

client = discord.Client()

@client.event
async def on_message(message):
    guild = client.get_guild(id)
    channels = ['general']

user = guild.roles

if str(message.channel) in channels:
        if message.content == "!role":
            await message.channel.send(user)

client.run(token)

这给出了输出

[<Role id=[confidential] name='@everyone'>, <Role id=707478326872375317 name='test role'>, <Role id=705472652411666482 name='dear'>, <Role id=705472646896418846 name='issue'>, <Role id=705456460024250409 name='pro'>, <Role id=705456411127316540 name='ansh'>, <Role id=705012241426284545 name='gsheet-bot'>]

我只想获取列表中的名称而不是 ID。我能做什么?

【问题讨论】:

  • this 回答你的问题了吗?
  • @DaveStSomeWhere 不是真的

标签: python discord discord.py


【解决方案1】:

由于user = guild.roles 返回一个列表,其中每个元素都是一个 discord.Role 对象,因此您可以访问 discord.Role 对象的 name 属性。

尝试以下方法来获取角色名称列表(不确定为什么要为角色命名变量 user):

user = [r.name for r in guild.roles]

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 2021-05-20
    • 2022-01-15
    • 1970-01-01
    • 2021-10-19
    • 2021-07-16
    • 1970-01-01
    • 2020-10-30
    • 2021-04-12
    相关资源
    最近更新 更多