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

时间:2020-11-01 18:32:24

标签: 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:
      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.")

此代码不是^^^

当我打印每个成员时,它将遍历该循环,它只会自己打印。公会中没有其他成员。

def guild_get(ctx):
  for guild in bot.guilds:
    if guild.id == ctx.guild.id:
      break
  return guild

这是我的guild_get()函数。

1 个答案:

答案 0 :(得分:0)

您需要使用members意图,才能使漫游器看到其他成员/用户。

from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="whatever", intents=intents)
# now your bot can see guild members.

注意

确保在discord developer portal中启用意图。

Click here 观看有关如何在自定义漫游器上启用意图的教程。

详细了解意图