前提・実現したいこと
Discord.jsでDiscordのBOTを作っています。
Discordのスラッシュコマンドで、チャンネルを移動する機能を作りたかったのですが下のようなエラーが出ました。(DiscordAPIError[50035]:Invalid Form Body)
発生している問題・エラーメッセージ
DiscordAPIError[50035]: Invalid Form Body options[2][APPLICATION_COMMAND_OPTIONS_REQUIRED_INVALID]: Required options must be placed before non-required options at SequentialHandler.runRequest(/home/runner/BOTtest/node_modules/@discordjs/rest/dist/index.js:933:15) at process.processTicksAndRejections (node: internal/process/task queues: 95:5) at sync SequentialHandler. queueRequest(/home/runner/BOTtest/node _modules/@discordjs/rest/dist/index. js:712:14) at async REST.request (/home/runner/STARTER/node_modules/@discordjs/rest/dist/index. js:1321:22 at async /home/runner/BOTtest/add_command.js:52: 16 frawError: :{ code: 50035 errors: { options: [Object] }, message: ‘Invalid Form Body’ }, code: 50035, status: 400, method: ‘PUT’, url:’https://discord.com/api/v10/applications/1088266852729368677/guilds/1082959808757502032/commands’ requestBody: {files: undefined, json: [ [Object], [Object] ] } }
該当のソースコード
JavaScript(add_command.js)
1 2const { Client, GatewayIntentBits, Partials, ClientApplication, EmbedBuilder, REST, Routes, SlashCommandBuilde, Collection, Events } = require("discord.js"); 3const token = 4process.env["TOKEN"] 5const CHANNEL_ID = 6process.env["CHANNEL_ID"] 7const clientId = 8process.env["CLIENT_ID"] 9const guildId = 10process.env["GUILD_ID"] 11const fs = require('node:fs'); 12const path = require('node:path'); 13 14 15const client = new Client({ 16 intents: [ 17 GatewayIntentBits.Guilds, 18 GatewayIntentBits.MessageContent, 19 GatewayIntentBits.GuildMessages, 20 ], 21 partials: [ 22 Partials.User, 23 Partials.Channel, 24 Partials.GuildMember, 25 Partials.Message, 26 Partials.Reaction, 27 Partials.GuildScheduledEvent, 28 Partials.ThreadMember, 29 ] 30}); 31const rest = new REST({ version: "10" }).setToken(token); 32 33const commands = []; 34// Grab all the command files from the commands directory you created earlier 35const commandsPath = path.join(__dirname, 'commands'); 36const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); 37 38// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment 39for (const file of commandFiles) { 40 const command = require(`./commands/${file}`); 41 commands.push(command.data.toJSON()); 42} 43 44// Construct and prepare an instance of the REST module 45 46 47// and deploy your commands! 48(async () => { 49 try { 50 console.log(`${commands.length}個のアプリケーションコマンドを読み込んでいます`); 51 52 // The put method is used to fully refresh all commands in the guild with the current set 53 const data = await rest.put( 54 Routes.applicationGuildCommands( clientId, guildId ), 55 { body: commands }, 56 ); 57 58 console.log(`${data.length}個のアプリケーションを読み込みました`); 59 } catch (error) { 60 // And of course, make sure you catch and log any errors! 61 console.error(error); 62 } 63})(); 64
JavaScript(index.js)
1 2const TOKEN = 3process.env["TOKEN"] 4const CHANNEL_ID = 5process.env["CHANNEL_ID"] 6const CLIENT_ID = 7process.env["CLIENT_ID"] 8const GUILD_ID = 9process.env["GUILD_ID"] 10const path = require('node:path'); 11const fs = require('node:fs'); 12const { Client, GatewayIntentBits, Partials, ClientApplication, EmbedBuilder, REST, Routes, SlashCommandBuilde, Collection, Events } = require("discord.js"); 13 14const client = new Client({ 15 intents: [ 16 GatewayIntentBits.Guilds, 17 GatewayIntentBits.MessageContent, 18 GatewayIntentBits.GuildMessages, 19 ], 20 partials: [ 21 Partials.User, 22 Partials.Channel, 23 Partials.GuildMember, 24 Partials.Message, 25 Partials.Reaction, 26 Partials.GuildScheduledEvent, 27 Partials.ThreadMember, 28 ] 29}); 30 31const rest = new REST({ version: '10' }).setToken(TOKEN); 32const commandsPath = path.join(__dirname, 'commands'); 33const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); 34client.commands = new Collection(); 35 36const http = require("http"); 37http.createServer(function (req, res) { 38 res.write("POWER!!!!!"); 39 res.end(); 40}).listen(8080); 41 42for (const file of commandFiles) { 43 const filePath = path.join(commandsPath, file); 44 const command = require(filePath); 45 if ('data' in command && 'execute' in command) { 46 client.commands.set(command.data.name, command); 47 } else { 48 console.log(`[!]この ${filePath}には"data"か"execute"の値が入ってないそうです `); 49 } 50} 51 52client.on(Events.InteractionCreate, async interaction => { 53 if (!interaction.isChatInputCommand()) return; 54 55 const command = interaction.client.commands.get(interaction.commandName); 56 57 if (!command) { 58 console.error(`[!]この${interaction.commandName}ていうコマンドは存在しないそうです`); 59 return; 60 } 61 62 try { 63 await command.execute(interaction); 64 } catch (error) { 65 console.error(error); 66 if (interaction.replied || interaction.deferred) { 67 await interaction.followUp({ content: "ちょっと何言ってるのか...", ephemeral: true }); 68 } else { 69 await interaction.reply({ content: "ちょっと何言ってるのか...", ephemeral: true }); 70 } 71 } 72}); 73 74//ここから別のやつ〜// 75//Discordにログイン〜 76client.login(TOKEN); 77
試したこと
エラー箇所の変数の型の変更など
補足情報
clientIdやguildIdはReplit側で保存しています。
先日まではこのコードで動いていましたが、今はなぜかエラーを吐くようになっています。
Replitで書いています。下のURLから全体のソースコードに飛べます。
https://replit.com/@LastelPyrire/BOTtest

回答1件
あなたの回答
tips
プレビュー