Print friends-list to console Discord.py

2020-04-30 18:06发布

How would I go about printing to console a list of all my friends? I'm hoping to be able to achieve this with the Discord.py library, hopefully someone here knows.

I currently get the error:
for user in discord.ClientUser.friends: TypeError: 'property' object is not iterable

Program:

token = ""
prefix = "::"

import discord
import asyncio
import codecs
import sys
import io
from discord.ext import commands
from discord.ext.commands import Bot

print ("waiting")

bot = commands.Bot(command_prefix=prefix, self_bot=True)
bot.remove_command("help")

@bot.event
async def on_ready():
    print ("Friends")

@bot.command()
async def userlist(ctx):
    for user in discord.ClientUser.friends:
        print (user.name+"#"+user.discriminator)

bot.run(token, bot=False)

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-04-30 18:10

discord.ClientUser.friends is not iterable - thus you can't run through its items in a for loop. I don't know that package, but try to see what type it is (you can do this like this - print(type(discord.ClientUser.friends))) and then see how to access the data in it.

查看更多
够拽才男人
3楼-- · 2020-04-30 18:12

discord.ClientUser is a class. You want the ClientUser instance that represents your bots user account. You can get this with bot.user, as commands.Bot is a subclass of Client

@bot.command()
async def userlist(ctx):
    for user in bot.user.friends:
        print (user.name+"#"+user.discriminator)
查看更多
Juvenile、少年°
4楼-- · 2020-04-30 18:13

From what the error states, discord.ClientUser.friends does not seem to have "unpackable" data.

For example,
"abcdefg" would iterate as "a", "b", "c", etc.
[1, 2, 3, 4] would iterate as 1, 2, 3, 4
The value stored in discord.ClientUser.friends appears to be an object and cannot be iterated.

Try doing print discord.ClientUser.friends to confirm this.

查看更多
登录 后发表回答