[英]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.