【发布时间】:2021-07-10 00:47:50
【问题描述】:
所以我在让我的自动角色工作时遇到了一个小问题我已经通过教程查找了它,除了由于不同的文本/细节而导致的一些差异之外,它并没有帮助
如果有帮助,我也会使用命令处理程序 V2 错误
(node:7712) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of undefined at Object.execute (\commands\other\reactionrole.js:6:46) at module.exports (\events\guild\message.js:46:25) at Client.emit (events.js:376:20) at MessageCreateAction.handle
(node_modules\discord.js\src\client\actions\MessageCreate.js:31:14) at Object.module.exports [as MESSAGE_CREATE] (node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) at WebSocketShard.onMessage (\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) at WebSocket.onMessage \node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:376:20) (Use `node --trace-warnings ...` to show where the warning was created) (node:7712) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 1) (node:7712) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
reactionrole.js
module.exports = {
name: 'reactionrole',
description: 'Sets Up Roles!',
async execute(message, args, Discord, client){
const channel = '860952043845058570'
const vgmembers = message.guild.roles.cache.find(role => role.name === "VG Members");
const vgamembers = message.guild.roles.cache.find(role => role.name === "VG-A Members");
const vghmembers = message.guild.roles.cache.find(role => role.name === "VG-H Members");
const vgmembersEmoji = `<:VG:860965732057219122>`;
const vgamembersEmoji = `<:VGA:860964110434566154>`;
const vghmembersEmoji = `<:VGH:860964110371913748>`;
let embed = new Discord.MessageEmbed()
.setColor('#e42643')
.setTitle('What Family Are You In')
.setDescription('Select The Emjoi Of The Alliance You Are In \n\n'
+ `${vgmembersEmoji} If Your A VG Member\n`
+ `${vgamembersEmoji} If Your A VG-A Member\n`
+ `${vghmembersEmoji} If Your A VG-H Member`);
let messageEmbed = await message.channel.send(embed);
messageEmbed.react(yellowTeamEmoji);
messageEmbed.react(blueTeamEmoji);
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 === vgmembersEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.add(vgmembers);
}
if (reaction.emoji.name === vgamembersEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.add(vgamembers);
}
if (reaction.emoji.name === vghmembersEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.add(vghmembers);
}
} 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 === vgmembersEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(vgmembers);
}
if (reaction.emoji.name === vgamembersEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(vgamembers);
}
if (reaction.emoji.name === vghmembersEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(vghmembers);
}
} else {
return;
}
});
}
}
message.js
require("dotenv").config();
const { Console, time } = require('console');
const cooldowns = new Map();
module.exports = async (Discord, client, message) => {
const prefix = process.env.PREFIX;
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd) || client.commands.find((a) => a.aliases && a.aliases.includes(cmd));
if (!command) return message.channel.send("This Command Doesn't Exist!");
if (command === "reactionrole"){
client.commands.get('reactionrole').execute(message, Discord, client);
}
if(!cooldowns.has(command.name)){
cooldowns.set(command.name, new Discord.Collection());
}
const current_time = Date.now();
const time_stamps = cooldowns.get(command.name);
const cooldown_amount = (command.cooldown) * 1000;
//If time_stamps has a key with the author's id then check the expiration time to send a message to a user.
if(time_stamps.has(message.author.id)){
const expiration_time = time_stamps.get(message.author.id) + cooldown_amount;
if(current_time < expiration_time){
const time_left = (expiration_time - current_time) / 1000;
return message.reply(`Please wait ${time_left.toFixed(1)} more seconds before using ${command.name}`);
}
}
//If the author's id is not in time_stamps then add them with the current time.
time_stamps.set(message.author.id, current_time);
//Delete the user's id once the cooldown is over.
setTimeout(() => time_stamps.delete(message.author.id), cooldown_amount);
if(command) command.execute(client, message, args, Discord);
}
【问题讨论】:
标签: javascript discord.js