How to add a role with an ID that is saved in an JSON file discord.js v12?

我怕爱的太早我们不能终老 提交于 2021-02-11 15:00:19

问题


I'm making a warn command and I've run into a problem with the role giving. First, I have a command that sets the warned role for the guild. It reacts to &setwarnedrole {role ID}. It saves the role ID into a JSON file, next to the guild ID. Then, the warn command reads the file, and gets the role ID that was stored with the guild ID the command is executed in. You use &warn {user ping/user ID} to warn people. But when I do that, it gives me an error:

TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.

My code for &setwarnedrole:

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const fs = require("fs");
let warnedRoleList = JSON.parse(fs.readFileSync("./roleIDs/warnedRole.json", "utf-8"));

module.exports = {
    name: 'setwarnedrole',
    description: "Set the warned role for your guild with this command",
    execute(message, args){
        const fs = require("fs");
        let warnedRoleList = JSON.parse(fs.readFileSync("./roleIDs/warnedRole.json", "utf-8"));
        if (!message.member.hasPermission("MANAGE_GUILD")) return message.reply("you are missing the manage server permissions")
        let guildID = message.guild.id;
        const warnedRole = message.guild.roles.cache.get(args[0]);
        const warnedRoleID = warnedRole.id
        if (!warnedRole) {
            return message.reply('invalid role ID')
        };

        message.reply(`warned role succesfully set to ${warnedRole}`)

        if(!warnedRoleList[guildID])warnedRoleList[guildID] = {
            warnedRoleList: warnedRoleID
        };
    
        fs.writeFile("./roleIDs/warnedRole.json", JSON.stringify(warnedRoleList), err => {
            if (err) console.log(err)
        });

        console.log(`Warned role ID = ${warnedRoleID}`)
        console.log(`Guild ID = ${guildID}`)
    }
}

My code for &warn:

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const fs = require("fs");
let warnedRoleList = JSON.parse(fs.readFileSync("./roleIDs/warnedRole.json", "utf-8"));
const commands = require("./commands");

module.exports = {
    name: 'warn',
    description: "The bot will warn the mentioned user",
    execute(message, args){
        let warnedRoleList = JSON.parse(fs.readFileSync("./roleIDs/warnedRole.json", "utf-8"));
        let guildID = message.guild.id;
        if(message.member.permissions.has('MANAGE_MESSAGES')){
            var role = message.guild.roles.cache.get(`${warnedRoleList[guildID]}`);
            var member = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
            if(!member) {
                message.reply('please specify the user that should be warned')
                return
            }
            if(!role) {
                message.reply('there is not a role set as the warned role, set it by doing &setwarnedrole (that requires manage server permissions)')
            }
            if (member.roles.cache.has(`${warnedRoleList[guildID]}`)) {
                message.reply('this user is already warned!')
            } else
            member.roles.add(role)
            .then(memberAdded => {
             message.channel.send(`${member.displayName} was succesfully warned by ${message.author.tag}`);
            })
            .catch(error => {
            console.log(error);
            });    
        } else message.reply(`you don't have permissions to execute this command.`);
        console.log(`${role}`)
        console.log(`${warnedRoleList[guildID]}`)
    }
}

and finally, this is how the role ID is saved with the guild ID in the JSON file:

{"745376827324760245":{"warnedRoleList":"751119465868951643"}}

with the first number being the guild ID and the second number being the warned role ID. But even if I remove the "" sign, making it:

{"745376827324760245":{"warnedRoleList":751119465868951643}}

it still doesn't work. The warn command works if I set role to a specific number. Also, logging role tells that role = undefined. Trying to log warnedRoleList[guildID] results into getting [objectObject]. What have I done wrong? Thanks :)


回答1:


Ok, I found the mistake. I was setting the role in &warn to message.guild.roles.cache.get(`${warnedRoleList[guildID]}`);, but it is supposed to be message.guild.roles.cache.get(`${warnedRoleList[guildID].warnedRoleList}`);. Case closed.



来源:https://stackoverflow.com/questions/63745937/how-to-add-a-role-with-an-id-that-is-saved-in-an-json-file-discord-js-v12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!