首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnhandledPromiseRejectionWarning: TypeError:无法读取未定义的属性'voice‘。在play.js中尝试运行命令时

UnhandledPromiseRejectionWarning: TypeError:无法读取未定义的属性'voice‘。在play.js中尝试运行命令时
EN

Stack Overflow用户
提问于 2021-06-30 04:08:28
回答 1查看 36关注 0票数 0

我得到错误语音: TypeError:无法读取我的代码中未定义的属性‘UnhandledPromiseRejectionWarning’。这是依赖关系的问题,还是代码中的错误。这是我的代码。

代码语言:javascript
复制
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代码

代码语言:javascript
复制
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);

这是一个音乐命令,我试图使它需要你在不和谐的语音通道中工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-30 05:38:18

您遇到的问题是为执行命令而传递的变量放错了位置。在/play命令文件中,您必须更改以下行:

代码语言:javascript
复制
async execute (client, message, args)

代码语言:javascript
复制
async execute (client, message, args, Discord)

而且你可以去掉

代码语言:javascript
复制
const Discord = require('discord.js');

因为您现在将从您的命令抓取器传递Discord变量。但要真正传入变量,您必须转到bot.js文件并更改以下行:

代码语言:javascript
复制
        try {
            command.execute(message, args);
        } catch (error) {
            console.error(error);
            message.reply('there was an error trying to execute that command!');
        }

要这样做:

代码语言:javascript
复制
        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文件中的顺序放置,否则,命令将总是抛出问题,因为必须以相同的顺序声明它们。

我希望这对你有帮助!祝你的项目好运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68185036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档