@tasks.loop(minutes=1) async def fetch_members(): for guild in bot.guilds: members = [] async for member in guild.fetch_members(): print(f"Fetched Member {member}") members.append(member) # 続きの処理... print(f"Fetched {len(members)} members.") print("Members fetching job done.")
@bot.event async def on_ready(): print('We have logged in as {0.user}'.format(bot)) fetch_members.start() # Start the task
bot.run(token='*****')
確認
これを実行すると、1分ごとにサーバーのメンバーリストを取得してコンソールに表示してくれます。
例外エラー
BOT が「サーバーのメンバーリストを扱う」ということを宣言していないため。
1 2 3 4 5 6
例外が発生しました: PrivilegedIntentsRequired Shard ID None is requesting privileged intents that have not been explicitly enabled in the developer portal. It is recommended to go to https://discord.com/developers/applications/ and explicitly enable the privileged intents within your application's page. If this is not possible, then consider disabling the privileged intents instead. discord.gateway.WebSocketClosure: During handling of the above exception, another exception occurred: discord.errors.ConnectionClosed: Shard ID None WebSocket closed with 4014 During handling of the above exception, another exception occurred: