为什么命令对我的不和谐.py机器人不起作用



`

import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
from discord import FFmpegPCMAudio
load_dotenv(".env")
TOKEN = os.getenv("DISCORD_TOKEN")
client = commands.Bot(command_prefix='$')
@client.command()
async def unvc(ctx):
await ctx.guild.voice_client.disconnect()
@client.command()
async def vc(ctx):
if ctx.author.voice:
channel = ctx.author.voice.channel
await channel.connect()
source = FFmpegPCMAudio('music.mp3')
player = voice.play(source)
@client.event
async def on_ready():
print('{0.user} has connected toDiscord!'.format(client))
return await client.change_presence(
activity=discord.Activity(type=discord.ActivityType.playing, name="anime waifu simulator VR"))
client.run(TOKEN)

`

我正试图用我的discord机器人使用命令,但当我键入命令时,它什么也不做。它仍然可以删除消息,所以与Discord的连接不是问题。

似乎您使用的是旧的不和谐.py 1.7.3版本,由于需要新的意图,该版本目前已经过时。

pip show discord.py检查您的版本,应该升级到2.x

如果您使用的是discord.py 的消息命令框架,那么您还必须使用消息意图

为此,请转到discord开发者面板并激活特权意图"消息"。之后,将以下内容添加到您的bot定义之上的代码中:

intents = discord.Intents()
intents.message_content = True

然后将意图传递给bot构造函数intents=intents

(此外,通常在不协调.py编程中,我们将Bot实例命名为bot,仅将Client实例命名为client)

您必须让机器人同时监听命令和消息。如果你发送一条消息,它将被解读为一条消息而不是一条命令。用于修复添加:

@client.event
async def on_message(ctx:Context):
await client.process_commands(ctx)

添加此:

intents = discord.Intents.all()
client = commands.Bot(command_prefix='$', intents=intents)

这会增加机器人的意图。我已经试过了。这应该对你有用。您还必须在discord开发人员门户的bot设置中启用intents。

最新更新