自动React角色/discord.js bot

7jmck4yq  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(307)

因此,我在如何让我的自动角色正常工作方面遇到了一个小问题。我一直在尝试通过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);
}
hgtggwj0

hgtggwj01#

message.guild 似乎没有定义
你在直接(私人)信息中吗?

uujelgoq

uujelgoq2#

在你的 message.js 你有

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

reactionrole.js 你有

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

这仅仅意味着值名称不匹配。
有三种方法可以解决这个问题。
更改命令文件中的顺序
也许是解决这个问题的最好办法。只需改变开始的时间 reactionrole.js 致:

module.exports = {
    name: 'reactionrole',
    description: 'Sets Up Roles!',
    async execute(client, message, args, Discord){
    //the rest of the code

更改命令处理程序输出
不建议这样做,因为您可能有其他已使用当前格式的命令文件,但仍有可能。
只需更改中的最后一行 message.js

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

但这可能意味着必须同时更改所有命令文件。
重新格式化输出和输入
最好的解决方案之一是使用对象。
在里面 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 这是行不通的。
使用任何你喜欢的方法,和一个快乐的编码!

相关问题