What is the correct operation for returning an iterable of server roles?

643 views Asked by At

I am attempting to iterate over the output of discord.Server.roles in order to output each role on the server I am connecting to.

The documentation states this for the declaration:

roles

A list of Role that the server has available.

However it seems that the resulting output is a class called member_descriptor which is non iterable.

See my example below:

import discord
import asyncio

client = discord.Client()

@client.event
async def on_ready():
    print(type(discord.Server.roles))
    try:
        for i in discord.Server.roles:
            print(i)
    except:
        print("Object is not iterable")

client.run(MY_SESSION_KEY)

Is anyone aware of whether it is possible to return a list of roles that a server has as this declaration is not returning a list but a class?

1

There are 1 answers

0
Sam Rockett On BEST ANSWER

You need a realised server object, not just the class definition.

Basically, instead of using discord.Server, you should be using client.get_server("ID of server")

def on_ready():
    server = client.get_server("id")
    for r in server.roles:
        print(r.name)