Print friends-list to console Discord.py

ε祈祈猫儿з 提交于 2019-12-20 06:45:13

问题


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)

回答1:


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)



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/51823648/print-friends-list-to-console-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!