所以我有他编码的不和谐机器人,并且正在尝试创建成员计数(同时分别编写在线和离线成员)。我看到为此需要 Intent,因此我在开发人员门户中启用了它们并将其添加到我的代码中:
intents = discord.Intents(members=True)
然后,当运行使用意图的实际代码时,添加了以下内容:
client = commands.Bot(members_prefixes, intents=intents)
(members_prefixes 是前缀)
每次尝试运行此命令时,都会出现异常:
忽略命令 member_count 中的异常: 回溯(最近一次调用):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 190, in member_count
members = server.fetch_members()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/guild.py", line 1367, in fetch_members
raise ClientException('Intents.members must be enabled to use this.')
discord.errors.ClientException: Intents.members must be enabled to use this.
这是我的代码的开头和使用意图的函数:
# bot.py
import asyncio
import json
import math
import os
import random
import requests as rq
import time
import re
import itertools
import discord
from discord.ext import commands
from numpy.core import long
from replit import db
import keepOn as ko
from flask import Flask
from threading import Thread
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='-', intents=intents)
TOKEN = 'token'
@client.command(aliases=["mc"])
async def member_count(ctx):
embed = discord.Embed(
colour=discord.Colour.blue()
)
'''
client = commands.Bot(members_prefixes, intents=intents)
'''
intents.members = True
server = ctx.message.guild
members = server.fetch_members()
icon = ctx.message.guild.icon_url
embed.set_author(name="Number of members in " + server.name)
count = 0
async for member in ctx.message.guild.fetch_members(limit=5):
count+=1
count_online = 0
count_offline = 0
for member in members:
count += 1
for member in members:
if member.status == discord.Status.online:
count_online += 1
else:
count_offline += 1
embed.set_thumbnail(url=icon)
embed.add_field(name='Number of online members', value=str(count_online), inline=True)
embed.add_field(name='Number of offline members', value=str(count_offline), inline=True)
embed.add_field(name='Total number of members in the server', value=str(count), inline=True)
await ctx.send(embed=embed)
client.run(TOKEN)
答案 0 :(得分:2)
尝试在 Discord 开发者网站上启用所有意图并使用下面的代码。我也遇到了这个问题,但它有效:
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
不要担心commnad_prefix
。您可以删除该参数。
答案 1 :(得分:0)
好的,所以我想出了问题所在。 当我创建机器人时,我创建了一个客户端
client = commands.Bot()
当时我没有在代码中启用意图,也没有在我的客户端代码行中启用意图。
互联网上的所有答案都指导您编写机器人创建行,您应该在其中启用意图。所以,我在代码中写了这行,完全忘记了我已经创建了 bot 对象,我只需要在行本身中启用意图。
client = commands.Bot(intents=intents)