问题
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