javascript - UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“声音”。在 play.js 中尝试运行命令时
问题描述
我收到错误 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);
这是我正在尝试制作的音乐命令,要求您在不和谐的语音频道中工作。
解决方案
您遇到的问题是您传递以执行命令的变量错位。在您的/play
命令文件中,您必须更改此行:
async execute (client, message, args)
到
async execute (client, message, args, Discord)
你可以摆脱
const Discord = require('discord.js');
因为您现在将从命令提取器中传递 Discord 变量。但要真正传入变量,您必须转到bot.js
文件并更改以下行:
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
对此:
try {
command.execute(client, message, args, Discord);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
您将传递的变量是(client, message, args, Discord)
,这意味着您只需为您将创建的每个命令添加它们,否则该命令将不起作用。
您当前的命令不起作用的原因是您client
在执行命令后没有调用该变量,这意味着该变量message
位于您的客户端变量的位置,话虽如此,您始终要牢记(client, message, args, Discord)
准确放置这些变量与它们在 bot.js 文件中的顺序相同,否则,该命令将始终引发问题,因为它们都必须以相同的顺序说明。
我希望这有帮助!祝你的项目好运。
推荐阅读
- javascript - 如何在 Laravel-8 和 InertiaJs 中向服务器发出 POST 请求时在浏览器中保留当前的 GET url
- python - 使用 Beautifulsoup 获取 HTML Header 的内容
- reactjs - 时间在 React 中没有正确显示
- python - 慢熊猫扩展().std()窗口函数的替代方法
- python - 如何使用配置文件指定要排序的目录?
- java - 如何将自定义图标设置为 JFrame?
- flutter - 如何在颤振中使用过期包构建和发布
- python - 如何使用 Django 访问网页的数据?
- bash - 如何在 Debian 上查看非当前内核的日期
- python - Traceback(最近一次通话最后一次)错误,即使是空项目