問題
JavaScriptでDiscord.jsのbotを作っています。
バージョンはnode.jsは16.17.0、Discord.jsが14.2.0です。
jsonに記載されている値などをembedsの配列?に変換しようとしてますが、方法がわかりません。
jsonの内容は追加したりするため、その度にコードを書き換えるのをやめたいです。
実現したいこと
config.json
1{ 2 "data": [ 3 { 4 "command": "simane", 5 "name": "島根", 6 "explanation": "それはそう" 7 }, 8 { 9 "command": "tottori", 10 "name": "鳥取", 11 "explanation": "これはどう" 12 } 13 ] 14} 15
上記のjsonファイルをforなどを使用し、以下のようにしたいです
const config = require("./config.json"); //jsonの取得 //ここから var embed = { title: "タイトル, description: "内容", fields: [ { name: "島根", value:"simane" }, { name: "鳥取" value:"tottori" } ] }; //ここまでのコードを自動化したい //そしてそのデータを送信する message.channel.send({ embeds: [embed] });
jsonの内容が変わった際に、fieldsの内容が増えたりするようforでコードを作りたいです。
足りない点や意味が分からない場合は指摘してくれると嬉しいです。
現時点の全体のコード
bot.js
1const { Client, GatewayIntentBits, Partials } = require('discord.js'), 2 client = new Client({ 3 partials: [Partials.Channel], 4 intents: [ 5 GatewayIntentBits.Guilds, 6 GatewayIntentBits.GuildVoiceStates, 7 GatewayIntentBits.GuildMessages, 8 GatewayIntentBits.GuildMessageReactions, 9 GatewayIntentBits.DirectMessages, 10 GatewayIntentBits.MessageContent 11 ] 12 }), 13 config = require('./config.json'); 14var args, command; 15 16client.on("ready", () => { 17 console.log("よう"); 18 client.user.setPresence({ 19 activities: [{ 20 name: config.play //プレイ中の内容 21 }], 22 status: "online" 23 }); 24}); 25client.on('messageCreate', message => { 26 if (message.author.bot) return; //bot自身なら実行停止 27 if (message.content.startsWith(config.prefix)) { //ボットのプレフィックスからメッセージが始まっているか確認 28 args = message.content.slice(config.prefix.length).trim().split(/ +/g); 29 command = args.shift().toLowerCase(); 30 if (command == "help") { 31 message.channel.send({ 32 embeds: [{ 33 title: "ヘルプ", 34 description: "全てのコマンドの初めに`" + config.prefix + "`をつける必要があります。\n今調べられる都道府県の数は、`" + config.data.length + "`個調べられます", 35 url: '', 36 fields: [ 37 { name: "沖縄", value: "`okinawa`" }, 38 { name: "鹿児島", value: "`kagosima`" }, 39 { name: "宮崎", value: "`miyazaki`" }, 40 { name: "大分", value: "`ooita`" }, 41 { name: "熊本", value: "`kumamoto`" }, 42 { name: "長崎", value: "`nagasaki`" }, 43 { name: "佐賀", value: "`saga`" }, 44 { name: "福岡", value: "`fukuoka`" }, 45 { name: "山口", value: "`yamaguti`" }, 46 { name: "広島", value: "`hirosima`" }, 47 { name: "岡山", value: "`okayama`" }, 48 { name: "島根", value: "`simane`" }, 49 { name: "鳥取", value: "`tottori`" }, 50 { name: "ヘルプ", value: "`help`" } 51 ], 52 }] 53 }); 54 } else { 55 message.channel.send("有効なコマンド名ではありません。`" + config.prefix + "help`でコマンドを確認できます。"); 56 }; 57 }; 58});
config.json
1{ 2 "token": "tokentokentokentokentokent.tokent.tokentokentokentokentokentokentokentok", 3 "prefix": "a!", 4 "data": [ 5 { 6 "command": "okinawa", 7 "name": "沖縄", 8 "explanation": "" 9 }, 10 { 11 "command": "kagosima", 12 "name": "鹿児島", 13 "explanation": "" 14 }, 15 { 16 "command": "miyazaki", 17 "name": "宮崎", 18 "explanation": "" 19 }, 20 { 21 "command": "ooita", 22 "name": "大分", 23 "explanation": "" 24 }, 25 { 26 "command": "kumamoto", 27 "name": "熊本", 28 "explanation": "" 29 }, 30 { 31 "command": "nagasaki", 32 "name": "長崎", 33 "explanation": "" 34 }, 35 { 36 "command": "saga", 37 "name": "佐賀", 38 "explanation": "" 39 }, 40 { 41 "command": "fukuoka", 42 "name": "福岡", 43 "explanation": "" 44 }, 45 { 46 "command": "yamaguti", 47 "name": "山口", 48 "explanation": "" 49 }, 50 { 51 "command": "hirosima", 52 "name": "広島", 53 "explanation": "" 54 }, 55 { 56 "command": "okayama", 57 "name": "岡山", 58 "explanation": "" 59 }, 60 { 61 "command": "simane", 62 "name": "島根", 63 "explanation": "" 64 }, 65 { 66 "command": "tottori", 67 "name": "鳥取", 68 "explanation": "" 69 } 70 ] 71} 72
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。