【问题标题】:Auto Reaction Roles / Discord.js Bot自动反应角色 / Discord.js 机器人
【发布时间】: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


    【解决方案1】:

    在您的message.js 中有

    if(command) command.execute(client, message, args, Discord);
    

    reactionrole.js 你有

    async execute(message, args, Discord, client){
    

    这仅仅意味着值名称不匹配。

    有 3 种方法可以解决此问题。

    1. 更改命令文件中的顺序

    可能是解决此问题的最佳方法。 只需将reactionrole.js 的开头更改为:

    module.exports = {
        name: 'reactionrole',
        description: 'Sets Up Roles!',
        async execute(client, message, args, Discord){
        //the rest of the code
    
    1. 更改您的命令处理程序输出

    不建议这样做,因为您可能有其他命令文件已经使用当前格式,但仍有可能。

    只需将message.js中的最后一行更改为

    if(command) command.execute(message, args, Discord, client);
    

    但这可能意味着必须同时更改所有命令文件。

    1. 重新格式化输出和输入

    最好的解决方案之一是使用对象。

    message.js中将最后一行改为

    if(command) command.execute({ client, message, args, Discord });
    

    在命令文件中,将执行属性更改为

    async execute({ client, message, args, Discord }){
    

    这也将允许您仅在命令文件中获取特定属性,并更改获取它们的顺序。

    例子:

    • 如果是这样的简单响应,您可以省略 client、args 和 Discord 属性:
    async execute({ message }){
    
    • 您可以更改订单而不会受到处罚
    async execute({ message, args, Discord, client }){
    

    即使顺序改变了,这仍然有效。

    使用这种方法唯一需要注意的是大写。 如果您输入 discord 而不是 Discord 它将不起作用。

    使用您喜欢的任何方法,并进行愉快的编码!

    【讨论】:

    • 你是一个救命稻草,谢谢我接受了你的 3. 想法我很感激我会支持你的帮助,但它不会让我因为我需要 15 声望大声笑
    • @KazTeko 没问题,如果您考虑接受正确答案,将不胜感激
    • 再次感谢大声笑,现在努力为我的 RP bot xD 添加更多内容
    【解决方案2】:

    message.guild 似乎未定义
    您是否在直接(私人)消息中?

    【讨论】:

    • 如果您的意思是在私人消息中执行命令,那么在一般聊天中没有输入命令
    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 2023-03-25
    • 2022-10-30
    • 2021-07-25
    相关资源
    最近更新 更多