我该如何使 ctx 不属于“交互”类型? (nextcord 斜杠命令)?
我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 nextcord 交互中,消息作者是
interaction.user
,通道是
interaction.channel
。您还可以通过
interaction.send
发送交互消息interaction.response.send_message
。它更短更容易阅读。
如果您想发送普通消息而不使用交互,请尝试
interaction.channel.send
。与应用命令中的ctx.channel.send
类似In nextcord interaction, message author is
interaction.user
,channel is
interaction.channel
.You also can send interaction message by
interaction.send
insteadof
interaction.response.send_message
. It's much shorter and easierto read.
If you want to send a normal message without using interaction, try
interaction.channel.send
. It's similar toctx.channel.send
in application command