通过json文件在联接上添加角色(自动角色) [英] Adding a role (autorole) on join from a json file

查看:99
本文介绍了通过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角色写入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.

首先,我尝试过此操作:

First I tried this:

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

我的完整代码是:

My complete code would be:

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

编辑:
为了帮助您在注释中提出要求,您可以添加带有else语句的 if(!jsonFile [guildId])。换句话说,如果您的对象不存在,请执行此操作。

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文件在联接上添加角色(自动角色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆