简体   繁体   English

使用 js 时无法读取未定义的属性(读取“执行”)

[英]Cannot read properties of undefined (reading 'execute) when i using js

hello I have a discord bot and I want to create reaction roles but when I execute It doesn't work ?你好我有一个不和谐的机器人,我想创建反应角色,但是当我执行它不起作用? please any body help me请任何人帮助我

this is ap prt of the code put it only doesn't work actually i tried hard这是代码的一部分,只是它不起作用实际上我很努力

module.exports = {
    name: 'reac',
    description: "Sets up a reaction role message!",
    async execute(message, args, Discord, client) {
        const channel = '974390749862961162';
        const students_rule = message.guild.roles.cache.find(role => role.name === "Student");
        const blueTeamRole = message.guild.roles.cache.find(role => role.name === "student");

here the index.js file and the code of this file :这里是 index.js 文件和这个文件的代码:

const Discord = require('discord.js');

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

for(const file of commandFiles){
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Nashmi Bot is online!');
});
client.on('message', message => {
    if(!message.content.startsWith(prefix) || message.author.bot )return;
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'rules'){
        client.commands.get('rules').execute(message, args);
    }
    if (command ==='reac'){
        client.commands.get('reac').execute(message, args, Discord, client);
    }
});

In your command file, follow the pattern on the first few lines.在您的命令文件中,遵循前几行的模式。

Instead of async execute(message, args, Discord, client) , Try execute: async (message, args, Discord, client)而不是async execute(message, args, Discord, client) ,尝试execute: async (message, args, Discord, client)

I think you should replace execute with run .我认为您应该将execute替换为run Change your command file as following:更改您的命令文件如下:

module.exports = {
    name: 'reac',
    description: "Sets up a reaction role message!",
    async run(message, args, Discord, client) => {
        const channel = '974390749862961162';
        const students_rule = message.guild.roles.cache.find(role => role.name === "Student");
        const blueTeamRole = message.guild.roles.cache.find(role => role.name === "student");

This works for me.这对我有用。

Important note: change all other command files to be like this run function as well.重要提示:将所有其他命令文件也更改为类似于此运行功能。


I also suggest following this structure for your index.js file:我还建议为您的 index.js 文件遵循此结构:

client.on("message", async message => {
  if (message.author.bot) return;
  else if (message.content.startsWith(prefix)){
    const args = message.content.slice(prefix.length).trim().split(/ +/g)
    const commandName = args.shift().toLowerCase();
    const command = await client.commands.get(commandName);
    if (!command) return;
    await command.run(client, message, args, Discord);
  }
}

That way, you don't have to create a lot of if and else if statements for each of your command, and it makes creating additional commands so much easier.这样,您不必为每个命令创建大量 if 和 else if 语句,这使得创建其他命令变得更加容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性(读取“执行”) - TypeError: Cannot read properties of undefined (reading 'execute') 类型错误:使用 mongoose 时无法读取未定义的属性(读取“长度”) - TypeError: Cannot read properties of undefined (reading 'length') when using mongoose MUI 5 无法读取未定义的属性(读取“向上”),使用 makestyles 时 - MUI 5 Cannot read properties of undefined (reading 'up'), When using makestyles 使用 ipcRenderer 时出现错误(类型错误无法读取未定义的属性(读取“发送”)) - I am getting an error when using ipcRenderer (typeerror cannot read properties of undefined (reading 'send')) discord.js - 发送嵌入 - 类型错误:无法读取未定义的属性(读取“执行”) - discord.js - Sending an Embed - TypeError: Cannot read properties of undefined (reading 'execute') 我收到此错误:TypeError:每当我执行命令时都无法读取未定义的属性(读取“成员”) - i recieve this error: TypeError: Cannot read properties of undefined (reading 'members') whenever i execute command 未处理的运行时错误类型错误:当我尝试使用 next.js 的图像组件时无法读取未定义的属性(读取“调用”) - Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'call') when I try to use the Image component of next.js 当我单击单个产品时出现无法读取 ProductScreen.js 中未定义的属性(读取“参数”)的错误。 电子商务网站 - when i click on single product getting error of Cannot read properties of undefined (reading 'params') in ProductScreen.js . mern ecommerce site 无法读取未定义的属性(读取 *) - Cannot read properties of undefined (reading *) 无法读取未定义的属性(读取 'then') - Cannot read properties of undefined (reading 'then')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM