Adding a role (autorole) on join from a json file

你。 提交于 2019-12-11 04:08:20

问题


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

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

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

回答1:


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.

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

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

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

Since that is done, we can now define the role we want to give as 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)
})

EDIT: 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.

Code:

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



来源:https://stackoverflow.com/questions/53123986/adding-a-role-autorole-on-join-from-a-json-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!