javascript - 为什么我会出错?TypeError:“无法读取未定义的属性‘执行’”
问题描述
在 main.js 中(不是那里的所有内容,但有什么问题):
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 === 'ping'){
client.commands.get('ping').execute(message, args);
} else if (command == 'hello'){
client.commands.get('hello').execute(message, args);
} else if (command == 'command'){
client.commands.get('command').execute(message, args, Discord);
} else if (command == 'clear'){
client.commands.get('clear').execute(message, args);
} else if (command == 'kick'){
client.commands.get('kick').execute(message, args);
} else if (command == 'ban'){
client.commands.get('ban').execute(message, args);
} else if (command == 'mute'){
client.commands.get('mute').execute(message,args);
} else if (command == 'wake'){
client.commands.get('wake up').execute(message,args);
} else if (command == 'play'){
client.commands.get('play').execute(message, args);
} else if (command == 'leave'){
client.commands.get('leave').execute(message, args);
} else if (command == 'coin'){
client.commands.get('coin').execute(message, args, Discord);
} else if (command == 'avatar'){
client.commands.get('avatar').execute(client, message, args);
} else if (command == 'weather'){
client.commands.get('weather').execute(client, message, args);
} else if (command == 'slowmode'){
client.commands.get('slowmode').execute(message, args);
} else if (command == 'youtube'){
client.commands.get('youtube').execute(message, args);
} else if (command == 'kill'){
client.commands.get('kill').execute(message, args);
} else if (command == 'beep'){
client.commands.get('beep').execute(message, args, Discord);
} else if (command == 'spam'){
client.commands.get('spam').execute(message, args, Discord);
} else if (command == 'reactionrole'){
client.commands.get('reactionrole').execute(message, args, Discord);
}
});
reactrole.js 中的内容:
module.exports = {
name: 'reactionrole',
description: "Sets up a reaction role message!",
async execute(message, args, Discord, client) {
const channel = '│announcements';
const yellowTeamRole = message.guild.roles.cache.find(role => role.name === "New Role");
const pepepog = 'pepepog';
let embed = new Discord.MessageEmbed()
.setColor('#e42643')
.setTitle('Want A Custom Role?')
.setDescription('Want Your own Custom Role? Yea, Thought So React In The Next 30 Minutes To Get Your Own Custom Role! @dioZ Will Make It When He Is Not Busy!!'
+ `${pepepog} for custom role :))`);
let messageEmbed = await message.channel.send(embed);
messageEmbed.react(pepepog);
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === pepepog) {
await reaction.message.guild.members.cache.get(user.id).roles.add(yellowTeamRole);
}
} else {
return;
}
});
client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === pepepog) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(yellowTeamRole); }
} else {
return;
}
});
}
}
解决方案
您的问题解决起来超级简单,您将事件和命令相互混淆,请移步
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === pepepog) {
await reaction.message.guild.members.cache.get(user.id).roles.add(yellowTeamRole);
}
} else {
return;
}
});
client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === pepepog) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(yellowTeamRole); }
} else {
return;
}
});
}
// To your mainfile as it is an event and furthermore check if reactionrole.js is being exported properly~! Hope it helps
推荐阅读
- bash - 无法通过 shell 脚本运行 mvn 目标
- ios - iOS 双卡,蜂窝数据的默认 sim 卡
- mysql - 比较两个 MySQL 表并在第二个表中选择不同的条目
- xamarin - Xamarin UI 文本 - 如何在 UITextView 中利用属性字符串(超链接)
- java - 从 Fuseki 获取 prefixMap
- angular - 具有混合数据源的 Angular 模板
- c - 在C中的最后一个位置反转一个带有0的数字
- mongodb - 从 MongoDB 中的整个集合中获取所有唯一的字段数组组合
- python - 仅在 Python 中定义用于测试的变量
- python - 将数据框中的空值替换为其他数据框