如何添加 ID 保存在 JSON 文件 discord.js v12 中的角色?
Posted
技术标签:
【中文标题】如何添加 ID 保存在 JSON 文件 discord.js v12 中的角色?【英文标题】:How to add a role with an ID that is saved in an JSON file discord.js v12? 【发布时间】:2020-12-24 00:11:27 【问题描述】:我正在发出警告命令,但我遇到了角色分配问题。首先,我有一个命令为公会设置警告角色。它对 &setwarnedrole role 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
设置为特定数字,则警告命令有效。此外,记录role
告诉role = undefined
。尝试记录warnedRoleList[guildID]
会导致获取[objectObject]
。我做错了什么?谢谢:)
【问题讨论】:
【参考方案1】:好的,我发现了错误。我将&warn 中的role
设置为message.guild.roles.cache.get(`$warnedRoleList[guildID]`);
,但它应该是message.guild.roles.cache.get(`$warnedRoleList[guildID].warnedRoleList`);
。结案。
【讨论】:
以上是关于如何添加 ID 保存在 JSON 文件 discord.js v12 中的角色?的主要内容,如果未能解决你的问题,请参考以下文章
如何从单个文件中读取不同的模式 json 对象并将其保存到表中