自动反应角色 / Discord.js Bot

时间:2021-07-09 09:40:27

标签: javascript discord.js

所以我在让我的自动角色工作时遇到了一个小问题我一直在尝试通过 message.js 和reactionrole.js 对其进行排序但它仍然出现同样的问题想知道是否有人可以提供帮助将不胜感激我已经通过教程查找过它,除了由于不同的文本/细节而导致的一些差异外,它没有帮助

如果有帮助的话,我也在使用命令处理程序 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);
}

2 个答案:

答案 0 :(得分: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,它将不起作用。

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

答案 1 :(得分:0)

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