嵌入音乐机器人不工作/discord.js

Posted

技术标签:

【中文标题】嵌入音乐机器人不工作/discord.js【英文标题】:Embed for music bot not working/discord.js 【发布时间】:2021-09-18 04:18:36 【问题描述】:

我正在为我的不和谐服务器开发我的音乐机器人。因此,从技术角度来看,我的代码可以工作,它可以播放音乐,并且有队列,但我正在尝试将嵌入添加到消息中,但我不断收到一条错误消息,指出我的消息、频道、作者未定义。如何正确定义嵌入工作所需的属性?

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

const queue = new Map();

module.exports = 
    name: 'play',
    aliases: ('skip', 'stop'),
    cooldown: 0,
    description: 'Advanced music bot',
    async execute(client, message, args, cmd, Discord)


        const voice_channel = message.member.voice.channel;
        if (!voice_channel) return message.channel.send('You need to be in a channel to execute this command!');
        const permissions = voice_channel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins');
        if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');

        const server_queue = queue.get(message.guild.id);

        if (cmd === 'play')
            if (!args.length) return message.channel.send('You need to send the second argument!');
            let song = ;

            if (ytdl.validateURL(args[0])) 
                const song_info = await ytdl.getInfo(args[0]);
                song =  title: song_info.videoDetails.title, url: song_info.videoDetails.video_url 
             else 
                const video_finder = async (query) =>
                    const video_result = await ytSearch(query);
                    return (video_result.videos.length > 1) ? video_result.videos[0] : null;
                

                const video = await video_finder(args.join(' '));
                if (video)
                    song =  title: video.title, url: video.url 
                 else 
                     message.channel.send('Error finding video.');
                
            

            if (!server_queue)

                const queue_constructor = 
                    voice_channel: voice_channel,
                    text_channel: message.channel,
                    connection: null,
                    songs: []
                

                queue.set(message.guild.id, queue_constructor);
                queue_constructor.songs.push(song);

                try 
                    const connection = await voice_channel.join();
                    const Discord = require('discord.js');
                    queue_constructor.connection = connection;
                    video_player(message.guild, queue_constructor.songs[0]);
                 catch (err) 
                    queue.delete(message.guild.id);
                    message.channel.send('There was an error connecting!');
                    throw err;
                
             else
                server_queue.songs.push(song);
                newEmbed = new Discord.MessageEmbed()
                .setTitle("**Now adding...**")
                .setDescription(`$song.title\n\`Requested by:\` $message.author`)
                .setColor("#ff0000")
                .setThumbnail('https://i.pinimg.com/474x/db/cd/d0/dbcdd0a38ebdb5f7f32cfda2f671dede.jpg')

                return message.channel.send


                message.channel.send(newEmbed);
            
        

    
    


const video_player = async (guild, song) => 
    const song_queue = queue.get(guild.id);

    if (!song) 
        song_queue.voice_channel.leave();
        queue.delete(guild.id);
        return;
    
    const stream = ytdl(song.url,  filter: 'audioonly' );
    const Discord = require('discord.js');
    song_queue.connection.play(stream,  seek: 0, volume: 0.5 )
    .on('finish', () => 
        song_queue.songs.shift();
        video_player(guild, song_queue.songs[0]);
    );
    await song_queue.text_channel.send
    newEmbed = new Discord.MessageEmbed()
    .setTitle("**Now playing...**")
    .setDescription(`$song.title\n\`Requested by:\` $message.author`)
    .setColor("#ff0000")
    .setThumbnail('https://i.pinimg.com/236x/a1/57/2c/a1572c4306f27fd644ab62199def8aab.jpg')

    message.channel.send(newEmbed);

【问题讨论】:

您能减少您的代码并使其成为 javascript 格式吗?最好将其减少到出现错误的位置 【参考方案1】:

我认为您在“newEmbed”之前缺少 const 并且您也不能有两次“newEmbed”,因为它会返回错误。更改。例如

const newEmbed = new Discord.MessageEmbed()

或者你可以有

const songList = new Discord.MessageEmbed()

songList 可以是任何东西,但对于您想要的其他嵌入来说并不相同。

【讨论】:

这确实有帮助,感谢您指出这一点,但我仍然收到错误 ReferenceError: message is not defined

以上是关于嵌入音乐机器人不工作/discord.js的主要内容,如果未能解决你的问题,请参考以下文章

音乐机器人 discord.js 问题

使用 DiscordJS 的音乐 Discord 机器人无法正常工作

从 twitch bot 调用 Discord.js 音乐问题找到语音频道

Discord.js 在嵌入链接中将 api 连接到不和谐机器人

discord.js 如何编辑/更新嵌入?

Discord.js 机器人使用数组中的加权随机选择嵌入