discord.py Intents.members 无法正常工作
Posted
技术标签:
【中文标题】discord.py Intents.members 无法正常工作【英文标题】:discord.py Intents.members not working properly 【发布时间】:2021-08-29 00:21:24 【问题描述】:所以我有我编写的他的不和谐机器人,并且正在尝试创建成员计数(同时分别编写在线和离线成员)。我看到我需要 Intents,所以我在开发者门户中启用了它们并将其添加到我的代码中:
intents = discord.Intents(members=True)
稍后,当运行使用意图的实际代码时,添加了以下内容:
client = commands.Bot(members_prefixes, intents=intents)
(members_prefixes 是前缀)
每次我尝试运行此命令时,都会出现异常:
忽略命令 member_count 中的异常: Traceback(最近一次调用最后一次):
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)
【问题讨论】:
这对您有帮助吗? ***.com/questions/64831017/… 【参考方案1】:尝试在 Discord Developer 网站上启用所有意图并使用以下代码。我也有这个问题,但它有效:
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
不用担心commnad_prefix
。您可以删除该参数。
【讨论】:
只是这样做了,但似乎仍然不起作用。它仍然给我同样的错误 应该如此。你还有问题吗?您能否分享整个代码,也许有问题。【参考方案2】:好的,所以我弄清楚了问题所在。 当我创建机器人时,我创建了一个客户端
client = commands.Bot()
当时我没有在代码中启用意图,也没有在我的客户端代码行中启用意图。
互联网上的所有答案都指示您编写机器人创建行,您应该在其中启用意图。所以,我在我的代码中写了这行代码,完全忘记了我已经创建了机器人对象,我只需要在行本身中启用意图。
client = commands.Bot(intents=intents)
【讨论】:
以上是关于discord.py Intents.members 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
discord.Embed 不被视为嵌入 discord.py