如何使ctx不是"交互"类型(nextcord斜杠命令)?



我正在尝试使用nextcard斜杠命令和交互创建一个音乐机器人。命令还没有完全完成,因为我甚至很难让它加入语音频道。我还不知道交互是如何工作的,但我认为它与ctx是一个类似的概念。下面是我的音乐.py cog:

import nextcord
from nextcord.ext import commands
from nextcord import Interaction
class Music(commands.Cog):
def __init__(self, client):
self.client = client
guild_ids = ["Guild Ids Go Here"]
#slash commands go under here
@nextcord.slash_command(name="play", description="plays music in vc", guild_ids = guild_ids)
async def play(self, interaction : Interaction, query: str):
channel = interaction.author.voice.channel  #ERROR IS HERE
try:
await channel.connect()
await interaction.response.send_message("The bot has joined vc.")
except:
await interaction.response.send_message("Failed to find voice channel.")

def setup(client):
client.add_cog(Music(client))

我收到一个错误,上面写着"交互对象没有属性author。它出现在"播放"的第15行,上面写着"channel=interaction.authori.woice.channe"。我认为这意味着这不是获取作者语音频道的正确方法。如果是这样的话,还有更好的工作方法吗?

  1. 在nextcard交互中,消息作者是interaction.user,信道是CCD_ 2。

  2. 您也可以通过interaction.send发送交互消息CCD_ 4。它更短更容易阅读。

  3. 如果您想在不使用交互的情况下发送正常消息,请尝试interaction.channel.send。它类似于应用程序命令中的ctx.channel.send

最新更新