問題描述
我對 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 文件加入時添加角色(自動角色)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!