In attempt to create slash commands in Discord.JS V14 following the official guide, I came across the following error:
DiscordAPIError[50035]: Invalid Form Body
0[LIST_TYPE_CONVERT]: Only iterables may be used in a ListType
The specific command (ping) that I would like to create doesn't have any additional options as it's a very basic command and simply is just unethical to add any options.
ping.js:
module.exports = {
name: "ping",
description: "View the reaction times",
slashInfo: {
enabled: true,
public: false,
},
getSlashInfo: function() {
const { SlashCommandBuilder } = require("discord.js");
const builder = new SlashCommandBuilder();
// Set basic command information
builder.setName(this.name);
builder.setDescription(this.description);
// If the command can be used in DMs
builder.setDMPermission(true);
// Return the information in JSON format
return builder.toJSON();
},
async execute(interaction, _prefix, client) {
interaction.reply({ content: `**Pong** in ${client.ws.ping}ms` });
}
}
commandHandler.js:
function postSlashCommand(data, to, client) {
if (!to) {
// Post command to all guilds
rest.put(
Routes.applicationCommands(client.user.id),
{ body: data }
);
} else {
// Post command for use only in a specific server
rest.put(
Routes.applicationGuildCommands(client.user.id, to),
{ body: data }
);
}
}
async function setupSlashCommands(directory, client) {
// Loop through known command files
const commandFolders = fs.readdirSync(`./${directory}`).filter(file => !file.endsWith(".js") && !file.endsWith(".json"));
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./${directory}/${folder}`).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
// Find the command object
const command = require(`../../${directory}/${folder}/${file}`);
// Ensure the command supports slash
if (!command.slashInfo?.enabled) return;
// Get the slash data
let data = command.getSlashInfo();
// Post the command to Discord
if (command.slashInfo.public) { // If the slash command is public
// Post command to all guilds
postSlashCommand(data, null, client);
} else { // If the slash command is in testing
// Post command for use only in the dev server
postSlashCommand(data, require("../../utils/config.json").DevServer, client);
}
}
}
}
module.exports = (client) => setupSlashCommands("commands", client);
Full error:
throw new DiscordAPIError.DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^
DiscordAPIError[50035]: Invalid Form Body
0[LIST_TYPE_CONVERT]: Only iterables may be used in a ListType
at SequentialHandler.runRequest (D:\Projects\...\node_modules\#discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:287:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async SequentialHandler.queueRequest (D:\Projects\...\node_modules\#discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14)
at async REST.request (D:\Projects\...\node_modules\#discordjs\rest\dist\lib\REST.cjs:52:22) {
rawError: {
code: 50035,
errors: {
_errors: [
{
code: 'LIST_TYPE_CONVERT',
message: 'Only iterables may be used in a ListType'
}
]
},
message: 'Invalid Form Body'
},
code: 50035,
status: 400,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/<application_id>/guilds/<guild_id>/commands',
requestBody: {
files: undefined,
json: {
options: [],
name: 'ping',
name_localizations: undefined,
description: 'View the reaction times',
description_localizations: undefined,
default_permission: undefined,
default_member_permissions: undefined,
dm_permission: true
}
}
}
Is there any way to simply create a slash command without providing the options property?
That endpoint takes an array of application commands. Instead of attempting to put a single command at a time, put all of them at once. Add the data to an array, then you will register them
const globalCommands = [],
guildCommands = [];
// Loop through known command files
const commandFolders = fs.readdirSync(`./${directory}`).filter(file => !file.endsWith(".js") && !file.endsWith(".json"));
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./${directory}/${folder}`).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`../../${directory}/${folder}/${file}`);
// Ensure the command supports slash
if (!command.slashInfo?.enabled) return;
// Add the slash data to the array
if (command.slashInfo.public) globalCommands.push(command.getSlashInfo());
else guildCommands.push(command.getSlashInfo())
}
}
Afterward, simply put the commands, and with what I see, you want to do global and guild commands, separately
postSlashCommand(guildCommands, require("../../utils/config.json").DevServer, client)
postSlashCommand(globalCommands, null, client)
Related
I am trying to make a discord bot with discordJS (v14) and I followed the tutorial from
https://discordjs.guide/creating-your-bot/slash-commands.html#individual-command-files and created a slash command. However, when I call the slash command, it always shoe DiscordAPIError[10062]: Unknown interaction I tried to use the command await interaction.deferReply() but it still failed.
Here are my code of that function
play.js
const { SlashCommandBuilder } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("play")
.setDescription("Play the song from the url or search query")
.addStringOption(option =>
option.setName("query")
.setDescription("The url or search query")
.setRequired(true)
),
async execute(interaction) {
console.log("interaction received"); // This part can run
await interaction.deferReply()
console.log("play command executed"); // cannot run
await interaction.editReply({ content: interaction.options.getString("query"), ephemeral: true });
}
}
index.js
const fs = require('node:fs');
const path = require('node:path');
const {
Client,
Collection,
Events,
GatewayIntentBits,
REST,
Routes,
} = require('discord.js');
require("dotenv").config();
// Tokens
const BOT_TOKEN = process.env.DISCORD_TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const rest = new REST({ version: 10 }).setToken(BOT_TOKEN);
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
// Create a new collection for commands
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
console.log(interaction);
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
console.log(`Executing ${interaction.commandName} command.`);
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
const commands = [];
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
console.log(command.data.toJSON());
commands.push(command.data.toJSON());
}
// try to register the slash commands
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationCommands(CLIENT_ID),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
// Log in to Discord with your client's token
client.login(BOT_TOKEN);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
The terminal result shows that it received the message but it continue to have the error
DiscordAPIError[10062]: Unknown interaction
May I ask how can I solve the problem and why it would happen?
I have been trying to make a slash command with subcommand that sends message to specified channel.
When i try to deploy it, i get:
DiscordAPIError[50035]: Invalid Form Body
options[1][APPLICATION_COMMAND_OPTIONS_TYPE_INVALID]: Sub-command and sub-command group option types are mutually exclusive to all other types
at SequentialHandler.runRequest (D:\projs\cpp\tbot\node_modules\#discordjs\rest\dist\index.js:659:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async SequentialHandler.queueRequest (D:\projs\cpp\tbot\node_modules\#discordjs\rest\dist\index.js:458:14)
at async REST.request (D:\projs\cpp\tbot\node_modules\#discordjs\rest\dist\index.js:902:22)
at async D:\projs\cpp\tbot\deploy-commands.js:24:16 {
requestBody: { files: undefined, json: [ [Object], [Object] ] },
rawError: {
code: 50035,
errors: { options: [Object] },
message: 'Invalid Form Body'
},
code: 50035,
status: 400,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/752621311494455367/guilds/1039125828790919240/commands'
}
My code:
const { SlashCommandBuilder } = require('discord.js');
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
module.exports = {
data: new SlashCommandBuilder()
.setName('say')
.setDescription('Make bot say anything the executor wants')
.addSubcommand(subcommand =>
subcommand
.setName('channel')
.setDescription('Says ')
.addChannelOption(option =>
option
.setName('target_channel')
.setDescription('Channel to send message in.')
.setRequired(true)))
.addStringOption(option =>
option
.setName('text')
.setDescription("Message to be sent in specified channel")
.setRequired(true)),
async execute(interaction) {
const channel = interaction.options.getChannel('target_channel');
const text = interaction.options.getString('text');
client.channels.cache.get(channel).send(text);
},
};
I have no clue about it. I didn't find anything about it anywhere. I expected it to deploy the commands via node deploy-commands.js but it resulted in error above.
The error is emitted when you attempt to add an option to a command which also has a subcommand. To illustrate this better I've create an ascii table, note how channel and text options are on the same level, which would result in an error.
say
|- channel: SUB_COMMAND
| |- name: STRING
| |- target_channel: TEXTCHANNEL
|- text: STRING
| |- option: STRING
If I've interpreted your code correctly you may want to add the string option to the subcommand, as so:
data: new SlashCommandBuilder()
.setName('say')
.setDescription('Make bot say anything the executor wants')
.addSubcommand(subcommand =>
subcommand
.setName('channel')
.setDescription('Says ')
.addStringOption(option =>
option
.setName('text')
.setDescription("Message to be sent in specified channel")
.setRequired(true)
)
.addChannelOption(option =>
option
.setName('target_channel')
.setDescription('Channel to send message in.')
.setRequired(true)
)
)
discord js v13.3.1
I have a set up with my bot that allows me to update and push slash commands via a command, deploy. The deploy command looks like this:
module.exports = {
name: "deploy",
description: "deploys slash commands",
disabled: false,
async execute(interaction, args, client) {
if (interaction.author.id !== process.env.OWNERID) return interaction.reply('You must be the owner to use this!');
const cmds = client.commands
.filter(command => command.slash)
.map(command => {
let {
name,
description = "missing description",
options = [],
slash = false,
defaultPermission = true,
slashPermissions = [],
} = command;
if (typeof name === "string") name = [name];
const cmd = { name: name[0], description, options, defaultPermission, permissions: slashPermissions };
return cmd;
});
await setCommands(interaction.guild?.commands)
.then(interaction.reply(`Registered ${cmds.length} commands to this guild!`));
async function setCommands(commandManager) {
const appCommands = await commandManager.set(
commandManager?.guild ? cmds : cmds.filter(cmd => !cmd.permissions.length)
);
if (commandManager?.guild) {
const fullPermissions = appCommands
.map(appCommand => {
const permissions = cmds.find(cmd => cmd.name === appCommand.name).permissions;
return { id: appCommand.id, permissions };
})
.filter(appCommand => appCommand.permissions.length);
await commandManager.permissions.set({ fullPermissions });
}
}
}
}
I stopped work on my bot awhile back, and now am trying to update the rest of my commands to have slash functionality. I have slash commands registered to my guild, so this command has worked in the past as is. Now, when I try to deploy my slash commands, I am getting this error in my console:
main\node_modules\discord.js\src\managers\ApplicationCommandManager.js:246
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
^
TypeError: command.options?.map is not a function
at Function.transformCommand (main\node_modules\discord.js\src\managers\ApplicationCommandManager.js:246:33)
at main\node_modules\discord.js\src\managers\ApplicationCommandManager.js:163:48
at Array.map (<anonymous>)
at GuildApplicationCommandManager.set (main\node_modules\discord.js\src\managers\ApplicationCommandManager.js:163:22)
at setCommands (main\commands\admin\deploy.js:34:54)
at Object.execute (main\commands\admin\deploy.js:29:19)
at module.exports (main\events\guild\messageCreate.js:28:51)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
For reference, my commands are built as such:
module.exports = {
name: "move",
description: "Move all users from one vc to another",
usage: `\`${process.env.PREFIX}move <vc1> <vc2>\``,
alias: ["mv"],
disabled: false,
slash: true,
options: [
{
name: 'target',
type: 'CHANNEL',
channelTypes: ['GUILD_VOICE'],
description: 'Channel to move users from',
required: true
},
{
name: 'destination',
type: 'CHANNEL',
channelTypes: ['GUILD_VOICE'],
description: 'Channel to move users into',
required: true
}
],
permission: ['MOVE_MEMBERS'],
async execute(interaction, args){
}
}
Is this an issue with how I am building my options blocks in my commands themselves, or how I am parsing them to send to guildCommandManager? I'm assuming the former because the error is a TypeError, but I am historically bad at working with maps and objects, so I could be wrong, and it's hard for me to figure out since the error is being thrown from the djs module and not my code itself
Just use the rest method which you can view the code of it here on the discord.js guide.
https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script
You can create global commands with: applicationCommands
and create local commands for 1 server with: applicationGuildCommands
I'm having some trouble coding prefix changing in my Discord bot. I've got all the basic functionality working:
The prefix is saved in a config file
The bot can write to the file and save it for later use
However, I can't seem to get the bot to use the new prefix after it's changed until I restart the bot. The config file shows that the prefix has been changed, but the bot doesn't respond to it.
So, my question is either, how can I refresh the memory so that the config is reloaded, or how can I get the bot to read my config again and use the new prefix?
Thanks!
prefix.js:
const fs = require('fs'); // node.js file system module
config = require('../config.json');
const Discord = require("discord.js");
module.exports = {
name: 'prefix', // command keyword
description: 'Changes the bot prefix', // info about command
group: 'settings', // command group (not displayed in !help [command name])
aliases: ['botprefix', 'newprefix'], // using these keywords also triggers command
usage: '[new prefix]', // how command is supposed to be used
cooldown: '1', // time command cannot be reused after it has been called
args: true, // are arguments required
execute(message, args) {
fs.exists("../config.json", function (error) {
if (error) {
console.log(error);
}
})
? fs
.readFile("../config.json", function (error) {
if (error) {
console.log(error);
}
})
.toString()
: config.prefix;
const newPrefix = args.shift().toString();
newConfig = {
token: config.token,
prefix: newPrefix,
};
fs.writeFile("config.json", JSON.stringify(newConfig, null, 2), function (
error
) {
if (error) {
console.log(error);
}
});
}
config.json:
{
"token": "token",
"prefix": "!"
}
If you want to access data via require, it won't be updated later as the function is run once at the beginnign of the project, even if you re-require it. This article can give you a bit more background on it. Basically: Always use fs.readFile and you're fine
I am using the source code from a security rules tutorial to attempt to do integration testing with Jest for my Javascript async function async_create_post, used for my firebase HTTP function create_post The files involved has a directory structure of the following:
Testing file: root/tests/handlers/posts.test.js
File to be tested: root/functions/handlers/posts.js
Helper code from the tutorial: root/tests/rules/helpers.js
And here is the source code that is involved:
posts.test.js
const { setup, teardown} = require("../rules/helpers");
const {
async_get_all_undeleted_posts,
async_get_post,
async_delete_post,
async_create_post
} = require("../../functions/handlers/posts");
describe("Post Creation", () => {
afterEach(async () => {
await teardown();
});
test("should create a post", async () => {
const db = await setup();
const malloryUID = "non-existent uid";
const firstPost = {
body: "First post from Mallory",
author_id: malloryUID,
images: ["url1", "url2"]
}
const before_post_snapshot = await db.collection("posts").get();
expect(before_post_snapshot.docs.length).toBe(0);
await async_create_post(firstPost); //fails at this point, expected to create a new post, but instead threw an error
const after_post_snapshot = await db.collection("posts").get();
expect(after_post_snapshot.docs.length).toBe(1);
});
});
posts.js
const {admin, db } = require('../util/admin');
//admin.initializeApp(config); //my credentials
//const db = admin.firestore();
const { uuid } = require("uuidv4");
const {
success_response,
error_response
} = require("../util/validators");
exports.async_create_post = async (data, context) => {
try {
const images = [];
data.images.forEach((url) => {
images.push({
uid: uuid(),
url: url
});
})
const postRecord = {
body: data.body,
images: images,
last_updated: admin.firestore.FieldValue.serverTimestamp(),
like_count: 0,
comment_count: 0,
deleted: false,
author_id: data.author_id
};
const generatedToken = uuid();
await db
.collection("posts")
.doc(generatedToken)
.set(postRecord);
// return success_response();
return success_response(generatedToken);
} catch (error) {
console.log("Error in creation of post", error);
return error_response(error);
}
}
When I run the test in Webstorm IDE, with 1 terminal running Firebase emulators:start , I get the following error message.
console.log
Error in creation of post TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Object
at validateString (internal/validators.js:120:11)
at Object.basename (path.js:1156:5)
at GrpcClient.loadProto (/Users/isaac/Desktop/project/functions/node_modules/google-gax/src/grpc.ts:166:23)
at new FirestoreClient (/Users/isaac/Desktop/project/functions/node_modules/#google-cloud/firestore/build/src/v1/firestore_client.js:118:38)
at ClientPool.clientFactory (/Users/isaac/Desktop/project/functions/node_modules/#google-cloud/firestore/build/src/index.js:330:26)
at ClientPool.acquire (/Users/isaac/Desktop/project/functions/node_modules/#google-cloud/firestore/build/src/pool.js:87:35)
at ClientPool.run (/Users/isaac/Desktop/project/functions/node_modules/#google-cloud/firestore/build/src/pool.js:164:29)
at Firestore.request (/Users/isaac/Desktop/project/functions/node_modules/#google-cloud/firestore/build/src/index.js:961:33)
at WriteBatch.commit_ (/Users/isaac/Desktop/project/functions/node_modules/#google-cloud/firestore/build/src/write-batch.js:485:48)
at exports.async_create_post (/Users/isaac/Desktop/project/functions/handlers/posts.js:36:5) {
code: 'ERR_INVALID_ARG_TYPE'
}
at exports.async_create_post (/Users/isaac/Desktop/project/functions/handlers/posts.js:44:13)
Error: expect(received).toBe(expected) // Object.is equality
Expected: 1
Received: 0
<Click to see difference>
at Object.<anonymous> (/Users/isaac/Desktop/project/tests/handlers/posts.test.js:59:45)
Error in creation of post comes from the console.log("Error in creation of post", error); in posts.js, so the error is shown in the title of this post.
I want to know why calling the async_create_post from posts.test.js will cause this error and does not populate my database with an additional record as expected behaviour. Do inform me if more information is required to solve the problem.
Here are some code snippets that may give more context.
helpers.js [Copied from the repository]
const firebase = require("#firebase/testing");
const fs = require("fs");
module.exports.setup = async (auth, data) => {
const projectId = `rules-spec-${Date.now()}`;
const app = firebase.initializeTestApp({
projectId,
auth
});
const db = app.firestore();
// Apply the test rules so we can write documents
await firebase.loadFirestoreRules({
projectId,
rules: fs.readFileSync("firestore-test.rules", "utf8")
});
// write mock documents if any
if (data) {
for (const key in data) {
const ref = db.doc(key); // This means the key should point directly to a document
await ref.set(data[key]);
}
}
// Apply the actual rules for the project
await firebase.loadFirestoreRules({
projectId,
rules: fs.readFileSync("firestore.rules", "utf8")
});
return db;
// return firebase;
};
module.exports.teardown = async () => {
// Delete all apps currently running in the firebase simulated environment
Promise.all(firebase.apps().map(app => app.delete()));
};
// Add extensions onto the expect method
expect.extend({
async toAllow(testPromise) {
let pass = false;
try {
await firebase.assertSucceeds(testPromise);
pass = true;
} catch (error) {
// log error to see which rules caused the test to fail
console.log(error);
}
return {
pass,
message: () =>
"Expected Firebase operation to be allowed, but it was denied"
};
}
});
expect.extend({
async toDeny(testPromise) {
let pass = false;
try {
await firebase.assertFails(testPromise);
pass = true;
} catch (error) {
// log error to see which rules caused the test to fail
console.log(error);
}
return {
pass,
message: () =>
"Expected Firebase operation to be denied, but it was allowed"
};
}
});
index.js
const functions = require('firebase-functions');
const {
async_get_all_undeleted_posts,
async_get_post,
async_delete_post,
async_create_post
} = require('./handlers/posts');
exports.create_post = functions.https.onCall(async_create_post);
The error message means that a method of the path module (like path.join) expects one of its arguments to be a string but got something else.
I found the offending line by binary search commenting the program until the error was gone.
Maybe one of your modules uses path and you supply the wrong arguments.