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

Discord.js v14+DisTube v4报错:无法读取autoplay属性及命令别名需求

问题解决:Discord.js v14 + DisTube v4 机器人错误修复及命令别名添加

一、TypeError: Cannot read properties of undefined (reading 'autoplay') 错误修复

错误原因

执行-s/-stop命令时,若当前无正在播放的队列,distube.getQueue(message)会返回undefined,直接访问queue.autoplay会触发属性读取错误;同时-s命令的判断逻辑存在语法颠倒问题。

修复代码

-s(跳过)命令:

client.on('messageCreate', (message) => {
  // 仅处理内容为'-s'的消息
  if (message.content !== '-s') return;
  
  const queue = distube.getQueue(message);
  // 先检查队列是否存在
  if (!queue) return message.channel.send('❌ 目前没有正在播放的队列!');
  
  // 确保队列存在后再执行原逻辑
  if (!queue.autoplay && queue.songs.length <= 1) {
    return message.channel.send('⏹ 没有可跳过的歌曲了!');
  }
  
  distube.skip(message);
  message.channel.send('⏭ 已跳过当前歌曲。');
});

-stop(停止)命令:

client.on('messageCreate', (message) => {
  if (!message.content.startsWith('-stop')) return;
  
  const queue = distube.getQueue(message);
  // 先检查队列是否存在
  if (!queue) return message.channel.send('❌ 目前没有正在播放的队列!');
  
  if (!queue.autoplay && queue.songs.length <= 1) {
    return message.channel.send('⏹ 无法停止,当前队列已无剩余歌曲!');
  }
  
  distube.stop(message);
  message.channel.send('🛑 播放队列已停止。');
});

二、添加-p作为-play的别名

修改播放命令的触发判断,同时支持-play和-p开头的消息,并根据命令长度正确截取参数:

client.on("messageCreate", async (message) => {
  // 同时匹配'-play'和'-p'命令
  const isPlayCommand = message.content.startsWith('-play') || message.content.startsWith('-p');
  if (!isPlayCommand) return;

  const voiceChannel = message.member.voice.channel;
  if (!voiceChannel) return message.channel.send('Devi essere in un canale vocale per utilizzare questo comando.');

  // 根据不同命令截取参数
  const args = message.content.startsWith('-play') 
    ? message.content.slice(6).trim() 
    : message.content.slice(2).trim();
  
  if (!args) return message.channel.send('Devi specificare una canzone o un URL valido.');

  distube.play(message.member.voice.channel, args, {
    member: message.member,
    textChannel: message.channel,
    message: message
  });
});

完整修改后的代码

const discord = require('discord.js');
const { EmbedBuilder } = require('discord.js');
const { SpotifyPlugin } = require("@distube/spotify")
const { SoundCloudPlugin } = require("@distube/soundcloud")

const { DisTube } = require('distube');

const client = new discord.Client({
  intents: [
    discord.GatewayIntentBits.Guilds,
    discord.GatewayIntentBits.GuildMessages,
    discord.GatewayIntentBits.GuildVoiceStates,
    discord.GatewayIntentBits.MessageContent
  ]
});

const distube = new DisTube(client, {
  leaveOnStop: false,
  emitNewSongOnly: true,
  emitAddSongWhenCreatingQueue: false,
  emitAddListWhenCreatingQueue: false,
  plugins: [new SpotifyPlugin(), new SoundCloudPlugin()]
});

client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

// 播放命令(支持-play和-p别名)
client.on("messageCreate", async (message) => {
  const isPlayCommand = message.content.startsWith('-play') || message.content.startsWith('-p');
  if (!isPlayCommand) return;

  const voiceChannel = message.member.voice.channel;
  if (!voiceChannel) return message.channel.send('Devi essere in un canale vocale per utilizzare questo comando.');

  const args = message.content.startsWith('-play') 
    ? message.content.slice(6).trim() 
    : message.content.slice(2).trim();
  
  if (!args) return message.channel.send('Devi specificare una canzone o un URL valido.');

  distube.play(message.member.voice.channel, args, {
    member: message.member,
    textChannel: message.channel,
    message: message
  });
});

// 跳过命令(-s)
client.on('messageCreate', (message) => {
  if (message.content !== '-s') return;
  
  const queue = distube.getQueue(message);
  if (!queue) return message.channel.send('❌ 目前没有正在播放的队列!');
  
  if (!queue.autoplay && queue.songs.length <= 1) {
    return message.channel.send('⏹ 没有可跳过的歌曲了!');
  }
  
  distube.skip(message);
  message.channel.send('⏭ 已跳过当前歌曲。');
});

// 停止命令(-stop)
client.on('messageCreate', (message) => {
  if (!message.content.startsWith('-stop')) return;
  
  const queue = distube.getQueue(message);
  if (!queue) return message.channel.send('❌ 目前没有正在播放的队列!');
  
  if (!queue.autoplay && queue.songs.length <= 1) {
    return message.channel.send('⏹ 无法停止,当前队列已无剩余歌曲!');
  }
  
  distube.stop(message);
  message.channel.send('🛑 播放队列已停止。');
});

distube.on('playSong', (queue, song) => {
  const embed = new discord.EmbedBuilder()
        .setTitle("Riproducendo canzone:")
        .addFields({ name: "Canzone:", value: song.name })
    queue.textChannel.send({ embeds: [embed] })
});

distube.on('addSong', (queue, song) => {
  const embed = new discord.EmbedBuilder()
  .setTitle("Canzone aggiunta:")
  .addFields({ name: "Canzone:", value: song.name })
  queue.textChannel.send({ embeds: [embed] })
});

client.login('TOKEN');

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 14:05:01