如何使用 JSON 使具有多个角色的成员静音?
Posted
技术标签:
【中文标题】如何使用 JSON 使具有多个角色的成员静音?【英文标题】:How to mute a member with multiple roles using JSON? 【发布时间】:2021-04-04 09:06:42 【问题描述】:我对 javascript 相当陌生,并且一直在搞乱 discord.js。我一直在尝试获取这个 Discord 机器人,但我一直卡住了。我正在尝试将成员角色缓存保存到对象列表中,该对象列表将使用 fs 保存到 json 文件中。如果机器人取消成员静音,则保存的角色将被重新应用。到目前为止,我能够获得定时静音来静音提到的成员并将角色应用回来。但是,如果我只使用取消静音命令,我会一直遇到错误。
这是我目前对 mute.js 的了解:
const ms = require("ms");
const fs = require("fs");
//let cachedUserRoles = ;
let cachedUserRoles = JSON.parse(fs.readFileSync("./roles.json", "utf-8"));
module.exports =
name: "mute",
description: "Mute a member",
execute(message, args)
let muteRole = message.guild.roles.cache.find(
(role) => role.name === "Mute"
);
const target =
message.mentions.users.first() || message.guild.members.get(args[0]);
let memberTarget = message.guild.members.cache.get(target.id);
let cachedUserRoles = JSON.parse(fs.readFileSync("./roles.json", "utf-8"));
if (!message.member.hasPermission("MANAGE_MESSAGES"))
return message.channel.send(
"You do not have the permission to manage messages"
);
if (target)
if (memberTarget.roles.cache.some((role) => role.name === "Mute"))
return message.channel.send("User is already muted!");
cachedUserRoles[memberTarget] = memberTarget.roles.cache;
if (!args[1])
fs.writeFile(
"./roles.json",
JSON.stringify(cachedUserRoles, null, 4),
(err) =>
if (err) console.log(err);
);
memberTarget.roles
.set([])
.then((target) => target.roles.add(muteRole))
.catch(console.err);
message.channel.send(`<@$memberTarget.user.id> ℎ???????? ???????????????? ????????????????????`);
return;
if (isNaN(ms(args[1])))
message.channel.send("???????????????????? ???? ???????????????????? ???????????????? ????????????/????????ℎ/????????????/????????????");
return;
fs.writeFile(
"./roles.json",
JSON.stringify(cachedUserRoles, null, 4),
(err) =>
if (err) console.log(err);
);
memberTarget.roles
.set([])
.then((memberTarget) => memberTarget.roles.add(muteRole))
.catch(console.err);
//memberTarget.roles.add(muteRole.id);
message.channel.send(
`<@$memberTarget.user.id> ℎ???????? ???????????????? ???????????????????? ???????????? $ms(ms(args[1]))`
);
setTimeout(function ()
memberTarget.roles
.set(cachedUserRoles[memberTarget])
.catch(console.error);
//memberTarget.roles.add(mainRole.id);
memberTarget.roles.remove(muteRole.id);
message.channel.send(`<@$memberTarget.user.id>ℎ???????? ???????????????? ????????????????????????????`);
cachedUserRoles = ;
fs.writeFile(
"./roles.json",
JSON.stringify(cachedUserRoles, null, 4),
(err) =>
if (err) console.log(err);
);
, ms(args[1]));
else
message.chanel.send("????????????'???? ???????????????? ????????????????????????!");
,
;
这是我的 unmute.js
const fs = require("fs");
let cachedUserRoles = JSON.parse(fs.readFileSync("./roles.json", "utf-8"));
module.exports =
name: "unmute",
description: "Unmute a member",
execute(message, args)
const target = message.mentions.users.first();
let muteRole = message.guild.roles.cache.find(
(role) => role.name === "Mute"
);
if (!message.member.hasPermission("MANAGE_MESSAGES"))
return message.channel.send(
"You do not have the permission to manage messages"
);
if (target)
let memberTarget = message.guild.members.cache.get(target.id);
if (!memberTarget.roles.cache.some((role) => role.name === "Mute"))
return message.channel.send("User is not muted!");
memberTarget.roles.remove(muteRole.id);
memberTarget.roles.set(cachedUserRoles[memberTarget]);
// console.log(rolesList[memberTarget]);
message.channel.send(`<@$memberTarget.user.id> ℎ???????? ???????????????? ????????????????????????????`);
cachedUserRoles = ;
fs.writeFile(
"./roles.json",
JSON.stringify(cachedUserRoles, null, 4),
(err) =>
if (err) console.log(err);
);
else
message.chanel.send("????????????'???? ???????????????? ????????????????????????!");
,
;
我得到的错误是
(node:9796) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
roles[0]: Value "'guild': '259552722242633728', 'id': '360211881228763138', 'name': 'Alright People', 'color': 3066993, 'hoist': True, 'rawPosition': 4, 'permissions': 1849155265, 'managed': False, 'mentionable': True, 'deleted': False, 'createdTimestamp': 1505951604898" is not snowflake.
roles[1]: Value "'guild': '259552722242633728', 'id': '259552722242633728', 'name': '@everyone', 'color': 0, 'hoist': False, 'rawPosition': 0, 'permissions': 1024, 'managed': False, 'mentionable': False, 'deleted': False, 'createdTimestamp': 1481952591239" is not snowflake.
我也搜索了以下帖子以寻找解决方案:
How to add a role with an ID that is saved in an JSON file discord.js v12?
How to remove all roles and add one role on discord bot, and then remove the added role and restore previous roles
但我仍然遇到错误,最常见的是
TypeError[INVALID_TYPE]:Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes
【问题讨论】:
【参考方案1】:我认为以下行返回错误。
memberTarget.roles.set(cachedUserRoles[memberTarget]);
如果您尝试为成员添加角色,请记住,这里您只是更改缓存,而 Discord.js 集合只是 Map 的扩展,设置时需要一个值.
这里是 the right way 为成员添加角色。
memberTarget.roles.add(JSON.parse(cachedUserRoles[memberTarget]).id);
在corresponding docs了解更多信息。
【讨论】:
您好,感谢您查看代码。我只是试图测试那条线,但它给了我一个 TyperError: Cannot read property of 'id of undefined.我也试过 memberTarget.roles.add(cachedUserRoles[memberTarget]);但它给了我另一个错误: UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not an Array or Collection of Roles or Snowflakes. 令我困惑的是我能够从 setInterval 函数中取消静音,所以我认为相同的行可以在不同的模块中工作。 哦,我的错,我忘了解析它。那是因为你不是给对象,而是对象字符串。请参阅JSON.parse()
以反转 JSON.stringify()
。编辑我的帖子。
抱歉,即使您编辑了帖子,我也遇到了错误。这是错误:SyntaxError: Unexpected token u in JSON at position 0
给出错误的根源真的很有帮助。检查您的 JSON 文件。以上是关于如何使用 JSON 使具有多个角色的成员静音?的主要内容,如果未能解决你的问题,请参考以下文章
如何使 SQL Server 数据库在附加用户和角色成员资格时继承它们?
当我尝试创建一个角色时,它给了我一个错误(discord.js)
Discord Py - 如何使用文本命令向公会的所有成员添加多个角色