You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人play命令报错TypeError: Cannot read property 'get' of undefined 求助

Discord 私人音乐机器人 play 命令异常修复

控制台报错原文

TypeError: Cannot read property 'get' of undefined

故障根因

外部参考代码省略了全局队列的初始化步骤:代码中直接调用message.client.queue.get(),但从未在客户端实例上定义过queue属性,其值始终为undefined,自然无法调用get方法。

分步修复方案

  • 初始化全局队列实例:在机器人客户端创建完成的位置(比如new Client()之后、登录逻辑之前)添加代码,为客户端绑定存储队列的Map结构:
// 示例:在客户端初始化文件中添加
client.queue = new Map();
  • 增加入参校验:避免用户未附歌曲链接时触发ytdl.getInfo空值报错,在获取歌曲信息的代码前增加判断:
if (!args[1]) return message.channel.send("请在指令后附上要播放的歌曲链接!");
  • 修复上下文指向问题:当前模块内this.play()的调用容易因指令加载的上下文丢失出现指向错误,可将内部播放逻辑的引用改为固定绑定当前模块,避免后续触发方法不存在的报错。

修正后可运行的play命令代码

const ytdl = require("ytdl-core");

module.exports = {
  name: "play",
  description: "在语音频道播放歌曲",
  async execute(message) {
    try {
      const args = message.content.split(" ");
      // 先校验输入的歌曲链接
      if (!args[1]) return message.channel.send("请在指令后附上要播放的歌曲链接!");

      const queue = message.client.queue;
      const serverQueue = queue.get(message.guild.id);

      const voiceChannel = message.member.voice.channel;
      if (!voiceChannel)
        return message.channel.send(
          "你需要先进入一个语音频道才能点歌!"
        );
      const permissions = voiceChannel.permissionsFor(message.client.user);
      if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
        return message.channel.send(
          "我需要获得加入语音频道、在频道内发言的权限才能播放音乐!"
        );
      }

      const songInfo = await ytdl.getInfo(args[1]);
      const song = {
        title: songInfo.videoDetails.title,
        url: songInfo.videoDetails.video_url
      };

      if (!serverQueue) {
        const queueContruct = {
          textChannel: message.channel,
          voiceChannel: voiceChannel,
          connection: null,
          songs: [],
          volume: 5,
          playing: true
        };

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

        try {
          const connection = await voiceChannel.join();
          queueContruct.connection = connection;
          // 绑定当前模块上下文调用播放方法
          module.exports.play(message, queueContruct.songs[0]);
        } catch (err) {
          console.log(err);
          queue.delete(message.guild.id);
          return message.channel.send("加入语音频道失败,请检查权限配置!");
        }
      } else {
        serverQueue.songs.push(song);
        return message.channel.send(
          `已将《${song.title}》加入播放队列!`
        );
      }
    } catch (error) {
      console.log(error);
      message.channel.send("播放失败,请检查歌曲链接是否有效!");
    }
  },

  play(message, song) {
    const queue = message.client.queue;
    const guild = message.guild;
    const serverQueue = queue.get(guild.id);

    if (!song) {
      serverQueue.voiceChannel.leave();
      queue.delete(guild.id);
      return;
    }

    const dispatcher = serverQueue.connection
      .play(ytdl(song.url))
      .on("finish", () => {
        serverQueue.songs.shift();
        module.exports.play(message, serverQueue.songs[0]);
      })
      .on("error", error => console.error(error));
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
    serverQueue.textChannel.send(`开始播放:**${song.title}**`);
  }
};

内容的提问来源于stack exchange,提问作者JFSdragonfire

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.23 03:36:05