我该如何使 ctx 不属于“交互”类型? (nextcord 斜杠命令)?

发布于 2025-01-17 09:16:36 字数 995 浏览 0 评论 0原文

我正在尝试使用NextCord Slash命令和交互创建音乐机器人。该命令尚未完全完成,因为我很难让它甚至加入语音频道。我还不知道互动的工作原理,但我假设这是一个与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))

我遇到了一个错误,上面写着“互动'对象没有属性'作者'。它出现在'play'的第15行中,当'channel =互动=互动。author.voice.voice.channel '。

I'm trying to create a music bot using nextcord slash commands and interactions. The command isn't fully finished yet, as I am having trouble getting it to even join the voice channel. I don't exactly know how interactions work yet but I'm assuming it's a similar concept as ctx. Below is my music.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))

I'm getting an error that says "'Interaction' object has no attribute 'author'. It occurs on line 15 in 'play' when it says 'channel = interaction.author.voice.channel'. I think this means that this isn't the right way to go about getting the author's voice channel. If this is the case, is there a better, working method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2025-01-24 09:16:36
  1. nextcord 交互中,消息作者是interaction.user,
    通道是interaction.channel

  2. 您还可以通过 interaction.send 发送交互消息
    interaction.response.send_message。它更短更容易
    阅读。

  3. 如果您想发送普通消息而不使用交互,请尝试
    interaction.channel.send。与应用命令中的ctx.channel.send类似

  1. In nextcord interaction, message author is interaction.user,
    channel is interaction.channel.

  2. You also can send interaction message by interaction.send instead
    of interaction.response.send_message. It's much shorter and easier
    to read.

  3. If you want to send a normal message without using interaction, try
    interaction.channel.send. It's similar to ctx.channel.send in application command

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文