[英]Discord.py | Member list
I need to get the list of all users on my discord server.我需要获取我的 Discord 服务器上所有用户的列表。 I tried this code but I got an error.我试过这段代码,但出现错误。
How can I do this?我怎样才能做到这一点?
First I tried to get all members and put it into a .txt file.首先,我尝试获取所有成员并将其放入 .txt 文件中。
def get_users(self):
with open('users.txt','w') as f:
for member in discord.Guild.fetch_members(limit=150):
print("{}".format(member.name), file=f)
Then I put it into init function cause I need to get this list when the bot is started up.然后我把它放到init函数中,因为我需要在机器人启动时获取这个列表。
class BotBanners(commands.Cog):
def __init__(self, bot):
self.bot = bot
BotBanners.get_users_avatars(self)
BotBanners.generate_main_banner(self)
I got an error and I can't find anything about that.我遇到了一个错误,我找不到任何关于它的信息。
Error错误
Traceback (most recent call last):
File "bot.py", line 48, in <module>
bot.load_extension('cogs.{}'.format(filename[:-3]))
File "/home/name/.local/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 664, in load_extension
self._load_from_module_spec(spec, name)
File "/home/name/.local/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 624, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.BotBanners' raised an error: TypeError: fetch_members() missing 1 required positional argument: 'self'
The issue is that you are calling fetch_members
on the Guild
class like a static method, but you need to call it on an instance of a guild.问题是您像静态方法一样在Guild
类上调用fetch_members
,但您需要在公会的实例上调用它。 Most likely you'll want to just get that guild by ID, or loop through the bot's guilds.很可能您只想通过 ID 获取该公会,或者遍历机器人的公会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.