【发布时间】:2021-06-30 05:38:17
【问题描述】:
我收到错误 UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined in my code。是依赖关系的问题还是代码中的错误。这是我的代码。
const Discord = require('discord.js');
module.exports = {
name: 'play',
aliases: ['p'],
description: 'plays a song/nasheed',
async execute (client, message, args) {
if(!message.member.voice.channel) return message.reply('Pleases be in a vc to use this command.');
const music = args.join(" "); //&play song name
if(!music) return message.reply("Invalid song/nasheed name.");
await client.distube.play(message, music);
}
}
这是我的 bot.js 代码
const fs = require('fs');
const Discord = require("discord.js");
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
client.commands = new Discord.Collection();
client.cooldowns = new Discord.Collection();
const commandFolders = fs.readdirSync('./src/commands');
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./src/commands/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./src/commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}
client.once('ready', () => {
console.log('bot is online');
client.user.setPresence({
status: 'available',
activity: {
name: 'Answering &help',
type: 'WATCHING',
url: 'https://www.youtube.com/channel/UC1RUkzjpWtp4w3OoMKh7pGg'
}
});
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.guildOnly && message.channel.type === 'dm') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (command.permissions) {
const authorPerms = message.channel.permissionsFor(message.author);
if (!authorPerms || !authorPerms.has(command.permissions)) {
return message.reply('You can not do this!');
}
}
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
const distube = require('distube');
client.distube = new distube(client, { searchSongs: false, emitNewSongOnly: true });
client.distube
.on('playSong', (message, queue, song) => message.channel.send(
`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`,
))
.on('addSong', (message, queue, song) => message.channel.send(
`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`,
))
.on('error', (message, e) => {
//console.error(e)
message.channel.send(`An error encountered: ${e}`)
})
client.login(token);
这是我正在尝试制作的音乐命令,要求您在不和谐的语音频道中工作。
【问题讨论】:
标签: javascript node.js discord.js