【问题标题】:Bot doesnt connect to voice channelBot 未连接到语音通道
【发布时间】:2022-01-05 15:25:29
【问题描述】:

在我使用旧功能之前,我的机器人无法连接到语音通道,但我将其更改为新功能,它仍然无法正常工作。 我尝试将它放在If(video),但它不起作用。

代码:

const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');

module.exports = {
    name: 'play',
    description: '',
    async execute(message, args){
        const voiceChannel = message.member.voice.channel;
        const { joinVoiceChannel } = require('@discordjs/voice');

        if (!voiceChannel) return message.channel.send('You need to be in a voice channel');
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT')) return message.channel.send('You dont have a permission to do that');
        if (!permissions.has('SPEAK')) return message.channel.send('You dont have a permission to do that');
        if (!args.length) return message.channel.send('You have to add a link');

        const connection = joinVoiceChannel({
            channelId: message.member.voice.channel.id,
            guildId: message.guild.id,
            adapterCreator: message.guild.voiceAdapterCreator
        });
            
        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);

            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;      
        }

        const video = await videoFinder(args.join(''));

        if (video){
            const stream = ytdl(video.url, {filter: 'audioonly'});
            connection.play(stream, {seek: 0, volume: 1})
            .on('finish', () =>{
                voiceChannel.leave();
            })

            await message.reply(`Now playing: ***${video.title}***`)
        } else {
            message.channel.send('No video redults found');
        }
    }
}

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    如果您没有收到任何错误,我建议您检查您是否提供了有效的意图。我相信您需要 'GUILD_VOICE_STATES' 意图。

    干杯!

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 加入语音频道不需要意图
    猜你喜欢
    • 2017-07-31
    • 2020-02-19
    • 2021-04-29
    • 2020-12-06
    • 1970-01-01
    • 2021-08-05
    • 2021-07-01
    • 2020-10-19
    • 1970-01-01
    相关资源
    最近更新 更多