在从 json 文件加入时添加角色(自动角色)

时间:2022-11-26
本文介绍了在从 json 文件加入时添加角色(自动角色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我对 JS 还很陌生,为了学习,我决定为 Discord 制作一个机器人,我学到了很多东西,并且还在继续学习.我有一个自动角色"的想法.我知道传统的做法.

I am fairly new to JS, and to learn I decided to make a bot for Discord, I have learned a lot and am continuing to learn. I had the idea for an "autorole". I know the conventional way of doing it.

bot.on('guildMemberAdd', member => {
  var role = member.guild.roles.find('name', 'Member');
  member.addRole(role);
});

但是,我希望它从 .json 文件中获取角色.其余代码都很好,我可以使用 >autorole Role 写入 json.我只是不确定如何将 json 合并到 member.addRole(role).

However, I want it to get the role from a .json file. The rest of the code is fine, I can write to the json with >autorole Role. I am just unsure on how to incorporate the json into the member.addRole(role).

我的 json 看起来像这样:

My json looks like this:

{
  "505107608391254026": {
    "role": "Staff"
  }
}

我尝试过并且认为可行的方法如下.请记住,在有人决定将我列入尝试学习和失败的名单之前,我还是个新手.

What I tried, and thought would work is the following. Please remember I am very new before someone decides to slate me for trying to learn and failing.

首先我尝试了这个:

let auto = JSON.parse(fs.readFileSync("./Storage/autorole.json", "utf8"));
bot.on('guildMemberAdd', member => {
  var role = member.guild.roles.find('name', auto.role);
  member.addRole(role);
});

失败后,我尝试了这个.

After that failed, I tried this.

let auto = JSON.parse(fs.readFileSync("./Storage/autorole.json", "utf8"));
bot.on('guildMemberAdd', member => {
  var role = auto.role;
  member.addRole(role);
});

推荐答案

我在我的 discord 机器人中使用了相同的方法.根据您的代码,您可以在 json 文件中的不和谐 ID 下列出每个用户的角色.

I am using the same method in my discord bot. From what I can tell by your code, you have roles for each user listed under their discord ids in your json file.

我没有意识到那是公会 ID.我已更改以下代码以适应它.

I didn't realize that was the guild ID. I have changed the below code to accommodate that.

如果您还没有将文件定义为可用变量,那么您首先需要:

You will want to start out by defining your file as a usable variable if you haven't already:

var jsonPath = 'path to json here';
var jsonRead = fs.readFileSync(jsonPath);
var jsonFile = JSON.parse(jsonRead);

接下来,在 json 文件中定义公会的 id:

Next, define the guild's id in the json file:

bot.on('guildMemberAdd', member => {
    var guildId = member.guild.id;
    let autoRole = jsonFile[guildId]
})

既然已经完成了,我们现在可以将我们想要赋予的角色定义为 autoRole.role

Since that is done, we can now define the role we want to give as autoRole.role

我的完整代码是:

var jsonPath = 'path to json here';
var jsonRead = fs.readFileSync(jsonPath);
var jsonFile = JSON.parse(jsonRead);

bot.on('guildMemberAdd', member => {
    var guildId = member.guild.id;
    let autoRole = jsonFile[guildId]
    let myRole = member.guild.roles.find(role => role.name === autoRole.role);
    member.addRole(myRole)
})

为了帮助您在评论中提出问题,您可以添加 if (!jsonFile[guildId]) 和 else 语句.换句话说,如果您的对象不存在,请执行此操作.

To help with what you asked in your comment, you could add if (!jsonFile[guildId]) with an else statement. In other words, if your object doesn't exist, do this.

代码:

var jsonPath = 'path to json here';
var jsonRead = fs.readFileSync(jsonPath);
var jsonFile = JSON.parse(jsonRead);

bot.on('guildMemberAdd', member => {
    var guildId = member.guild.id;
    if (!jsonFile[guildId]) {
        console.log('Role could not be found')
    } else {
        let autoRole = jsonFile[guildId]
        let myRole = member.guild.roles.find(role => role.name === autoRole.role);
        member.addRole(myRole)
    }
})

这篇关于在从 json 文件加入时添加角色(自动角色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:未定义 member.voiceChannel 下一篇:如何使用 discord.js 创建角色

相关文章

最新文章