这是我的代码:
const { SlashCommandBuilder } = require('@discordjs/builders');
const { createAudioPlayer, createAudioResource, joinVoiceChannel } = require('@discordjs/voice');
const ytdl = require('ytdl-core');
const { EmbedBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('Play a song in your current voice channel')
.addStringOption(option =>
option.setName('song')
.setDescription('Enter the YouTube URL or keywords to search for')
.setRequired(true)),
async execute(interaction) {
const song = interaction.options.getString('song');
// Check if the user is in a voice channel
if (!interaction.member.voice.channel) {
return interaction.reply({ content: 'You are not in a voice channel!', ephemeral: true });
}
// Join the user's voice channel
const connection = joinVoiceChannel({
channelId: interaction.member.voice.channel.id,
guildId: interaction.guildId,
adapterCreator: interaction.guild.voiceAdapterCreator
});
// Get video info and create an audio resource
const videoInfo = await ytdl.getInfo(song);
const stream = ytdl(song, { filter: 'audioonly' });
const resource = createAudioResource(stream);
// Create an audio player and play the resource
const player = createAudioPlayer();
player.play(resource);
// Subscribe the connection to the player
connection.subscribe(player);
// Send a confirmation message
const embed = new EmbedBuilder()
.setColor("Green")
.setTitle('Now Playing')
.setDescription(`[${videoInfo.videoDetails.title}](${song})`)
.setThumbnail(videoInfo.videoDetails.thumbnails[0].url);
interaction.reply({ embeds: [embed] });
},
};
这是一个允许用户在他们所在的语音频道中播放音乐的命令.用户必须在语音通道上,运行命令,然后机器人将连接到与用户相同的通道.问题是,当我在语音通道上时,它也会发送'You are not in a voice channel!'
,而不会进入下一步!我认为问题出在这里:
if (!interaction.member.voice.channel) {
return interaction.reply({ content: 'You are not in a voice channel!', ephemeral: true });
}
有没有人可以帮我解决这个问题(如果你发现任何其他问题,也请让我知道.)提前谢谢!
我有以下意图:[Guilds, GuildMembers, GuildMessages, MessageContent, DirectMessages]
.
注:
This article或this不能解决我的案子,它也不是复制品.