Discord.py discord.ext.commands在Cog中如何工作?

Discord.py discord.ext.commands在Cog中如何工作?,discord.py,Discord.py,这些是我的进口货 import discord import os import random import time from discord.ext import commands 这个代码确实有效^^^ @bot.command() async def online(ctx): response = "" guild = guild_get(ctx) for i in guild.members: if not i.bot:

这些是我的进口货

import discord
import os
import random
import time


from discord.ext import commands
这个代码确实有效^^^

@bot.command()
async def online(ctx):
  response = ""

  guild = guild_get(ctx)
  
  for i in guild.members:
    if not i.bot:
      if i.status == discord.Status.offline:
        """ """
      else:
        response += i.mention
        response += ", "
  if len(response) > 0:
    await ctx.channel.send("||" + response + "||")
  else:
    await ctx.channel.send("No members are online.")
此代码不支持^^^

当我打印循环中经过的每个成员时,它只打印自己。在公会中看不到任何其他成员

@commands.has_any_role("RoleName1","RoleName2","RoleName3")
@bot.command()
async def online(ctx):
  response = ""

  guild = guild_get(ctx)
  
  for i in guild.members:
    if not i.bot:
      if i.status == discord.Status.offline:
        """ """
      else:
        response += i.mention
        response += ", "
  if len(response) > 0:
    await ctx.channel.send("||" + response + "||")
  else:
    await ctx.channel.send("No members are online.")
顺便说一句,这是我的帮会获取功能。

为了让你的机器人看到其他成员/用户,你需要成员意图

从discord.ext导入命令 意图=discord.intents.default intents.members=True bot=commands.Botcommand\u prefix=whatever,intents=intents 现在你的机器人可以看到公会成员了。 笔记 确保在应用程序中启用意图

查看有关如何在自定义机器人上启用意图的教程

阅读更多有关意向的信息:

你为什么需要帮会获得??!你可以使用ctx.guild
def guild_get(ctx):
  for guild in bot.guilds:
    if guild.id == ctx.guild.id:
      break
  return guild