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

How to add a role with an ID that is saved in an JSON file discord.js v12?(如何添加 ID 保存在 JSON 文件 discord.js v12 中的角色?)
本文介绍了如何添加 ID 保存在 JSON 文件 discord.js v12 中的角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

我的 &setwarnedrole 代码:

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}`)
    }
}

我的 &warn 代码:

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

最后,这是角色 ID 与公会 ID 一起保存在 JSON 文件中的方式:

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

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

第一个数字是公会 ID,第二个数字是警告角色 ID.但是即使我删除了"签名,制作:

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}}

还是不行.如果我将 role 设置为特定数字,则警告命令有效.此外,记录 role 告诉 role = undefined.尝试记录 warnedRoleList[guildID] 会导致获取 [objectObject].我做错了什么?谢谢:)

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 :)

推荐答案

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

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.

这篇关于如何添加 ID 保存在 JSON 文件 discord.js v12 中的角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)