如何添加具有保存在JSON文件discord.js v12中的ID的角色?

时间:2020-09-04 17:53:09

标签: javascript node.js json discord.js

我正在发出警告命令,但我在角色分配上遇到了问题。首先,我有一个命令来设置行会的警告角色。它对&setwarnedrole {角色ID}做出反应。它将角色ID保存到公会ID旁边的JSON文件中。然后,warn命令读取文件,并获取与执行该命令的行会ID一起存储的角色ID。您可以使用&warn {user ping / user ID}来警告人们。但是当我这样做时,它给了我一个错误:

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

我的&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}`)
    }
}

我的&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]}`)
    }
}

最后,这是将角色ID与行业ID一起保存在JSON文件中的方法:

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

,第一个数字是行会ID,第二个数字是警告角色ID。但是,即使我删除了“”符号,也可以这样做:

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

它仍然不起作用。如果我将role设置为特定数字,则warn命令有效。另外,记录role会告诉role = undefined。尝试登录warnedRoleList[guildID]会导致获得[objectObject]。我做错了什么?谢谢:)

1 个答案:

答案 0 :(得分:0)

好,我发现了错误。我在&warn中将role设置为message.guild.roles.cache.get(`${warnedRoleList[guildID]}`);,但应该是message.guild.roles.cache.get(`${warnedRoleList[guildID].warnedRoleList}`);。结案了。