DiscordJS v13 Invalid Form Body - javascript

I am trying to make a toggle able slash command, if they pick the disable option it turns it off but when if you pick the enable option it asks to pick a channel but it gives this error
Error:
DiscordAPIError[50035]: Invalid Form Body
23.name[BASE_TYPE_REQUIRED]: This field is required
rawError: {
code: 50035,
errors: { '23': [Object] },
message: 'Invalid Form Body'
},
code: 50035,
status: 400,
method: 'put',
url: 'https://discord.com/api/v9/applications/971024098098569327/commands'
Code:
module.exports = {
name: 'welcomer',
permissions: 'MANAGE_CHANNELS',
description: 'Set Where Welcome Messages Get Sent To.',
options: [
{
name: 'toggle',
description: 'Toggle On/Off The Welcomer',
type: 3,
required: true,
choices: [
{
name: 'disable',
value: 'off',
},
{
name: 'enable',
value: 'on',
choices: [
{
name: 'channel',
description: 'Select channel to send welcome messages to',
type: 7,
required: true,
},
]
},
],
},
],

Those would be an example of a subcommand and need to be indicated as such and will need descriptions in a couple places.
module.exports = {
name: 'welcomer',
permissions: 'MANAGE_CHANNELS',
description: 'Set Where Welcome Messages Get Sent To.',
options: [{
name: 'disable',
description: `'Disable welcomer`, // Added needed description
type: 1, //converted to subcommmand
}, {
name: 'enable',
description: `'Enable welcomer`, // Added needed description
type: 1, //converted to subcommmand
options: [{
name: 'channel',
description: 'Select channel to send welcome messages to',
type: 7,
required: true,
channel_types: [0] // allows only text channels to be selected
}]
}],
// run command pick only one of the below two
// if command.execute()
async execute(client, interaction, args)
// if command.run()
run: async (client, interaction, args) =>
// command code below here assumes you have the code in your `interactionCreate` listener to set up args
{
if (args.disable) {
// Code to turn off
} else if (args.enable) {
const channel = args.channel
// Code to turn on
};
}
}

Related

discord.js - TypeError [COMMAND_INTERACTION_OPTION_TYPE]: Option "user" is of type: STRING; expected USER

When I run the script for my discord bot and use /warn remove it keeps throwing an error that user is of type string expected USER. It says the error is happening between lines 69:42 and I can't find the issue.
module.exports = {
category: 'Moderation',
description: 'Warn a user',
permissions: ['ADMINISTRATOR'],
slash: true,
guildOnly: true,
options: [
{
type: 'SUB_COMMAND',
name: 'remove',
description: 'Removes a warning from the user',
options: [
{
name: 'user',
type: 'USER',
description: 'The user to remove the warning from',
required: true,
},
{
name: 'id',
type: 'STRING',
description: 'The ID of the warning to remove',
required: true,
},
],
}
],
callback: async ({ guild, member: staff, interaction }) => {
const user = interaction.options.getUser('user')
if (subCommand === 'remove') {
const warning = await warnSchema.findByIdAndDelete(id)
return {
custom: true,
content: `Removed warning ${warning.id} from <#${user?.id}>`,
allowedMentions: {
users: []
}
}
}
}
}
I didn't post all the code because it seemed like it would be too much searching but this is the code that is causing the error.

how to set time to xmpp stanzas object

i have a xmpp server, then i have function to send message via xmpp and i use npm (#xmpp/client) for nodejs
var message = 'test';
const stanzas = xml("message", {
to: receiver,
type: "notification",
time: time.datetime()* //not work
}, xml("body", null, message),
xml("time", "urn:xmpp:time")) // not work
await xmpp.send(stanzas).catch(console.error);
*(2022-06-02T13:39:31Z)
This function works and send correct
The problem is that i cant set timeStamp for this
I just try to send time in main object of message, send in xml - no success
my result stanzas is:
Element {
name: 'message',
parent:
Element {
name: 'stream:stream',
parent: null,
children: [],
attrs:
{ version: '1.0',
xmlns: 'jabber:client',
'xmlns:stream': 'http://etherx.jabber.org/streams',
to: 'localhost',
'xml:lang': undefined } },
children:
[ Element { name: 'body', parent: [Circular], children: [Array], attrs: {} },
Element {
name: 'time',
parent: [Circular],
children: [],
attrs: [Object] } ],
attrs:
{ to: 'testJabber',
type: 'notification',
time: '2022-06-02T13:39:31Z' } }
What i get as a function success result:
()testServer : test
What i should get:
(02.06.2022 13:39:31)testServer : test

FCM IOS notification

The code is working fine when I try to send notification on android but it does not work for IOS notification. Getting the below error:
{
"results": [
{
"error": {
"code": "messaging/internal-error",
"message": "An internal error has occurred. Please retry the request."
}
}
],
"canonicalRegistrationTokenCount": 0,
"failureCount": 1,
"successCount": 0,
"multicastId": 8118740071861272000
}
This is my code sample, please take a look and help me to resolve the issue.
also, I get a notification when I try to send notification from cloud messages console.
return await admin.messaging()
.sendToDevice(deviceToken, {
data: {
title: 'Test Fcm',
body: 'tesing fmc body',
content_available: 'true',
priority: 'high',
action_type: 'TEST'
},
notification: {
title: 'Test Fcm',
body: 'tesing fmc body',
content_available: 'true',
priority: 'high'
}
}
, {
priority: "high",
timeToLive: 60 * 60 * 24,
contentAvailable:true,
}).catch(err => {
console.log(err);
})
i have encount 2 days ago this issue.
just remove 'content_available' from 'notification', it's working.
example for your json =>
{
data: {
title: 'Test Fcm',
body: 'tesing fmc body',
content_available: 'true',
priority: 'high',
action_type: 'TEST'
},
notification: {
title: 'Test Fcm',
body: 'tesing fmc body',
=> content_available: 'true', // remove this
priority: 'high'
}
}
regards.

Unable to execute a promise in node.js

I am completely new to node.js, All I want to do is to use the inquirer NPM to ask a few questions, get the answers to those questions plug into a template literals and create a readme.md file from it. for some reason, my code doesn't work. Could someone point out what have I done wrong here? I feel like I'm missing some parameter when creating the promise maybe??
const inquirer = require("inquirer")
const fs = require("fs")
const util = require("util")
const readme = () => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\```
${data.usage}
\```
## License
${license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `
}
// array of questions for user
const questions = [
{
type: "input",
name: "title",
message: "What is the title of your project",
default: "Not Available"
},
{
type: "input",
name: "username",
message: "What is your Github username?",
default: "Not Available"
},
{
type: "input",
name: "email",
message: "What is your email address?",
default: "Not Available"
},
{
type: "input",
name: "contact",
message: "How do you want people to reach you?",
default: "Not Available"
},
{
type: "input",
name: "description",
message: "Please type a short description of your project ( When you are done, hit ESC & type \":wq\" to exit editor )",
default: "Not Available"
},
{
type: "input",
name: "installation",
message: "How do you install this application? ( When you are done, hit ESC type \":wq\" to exit editor) ",
default: "Not Available"
},
{
type: "input",
name: "usage",
message: "Give example of some ways you can use this application ( When you are done, hit ESC & type \":wq\" to exit editor)",
default: "Not Available"
},
{
type: "list",
name: "license",
message: "What kind of license would you like to use?",
choices: ["MIT","Microsoft Public License","Mozilla Public License 2.0","Academic Free License v3.0","Open Software License 3.0","Creative Commons Attribution 4.0"],
default: "Not Available"
},
{
type: "confirm",
name: "contribution",
message: "Are you open to contribution?",
default: "Not Available"
},
{
type: "input",
name: "contributionRequirement",
message: "If Yes, What are your requirement for giving contribution?",
default: "Not Available"
},
{
type: "input",
name: "test",
message: "Please give instructions for testing of this project ( When you are done, hit ESC & type \":wq\" to exit editor)",
default: "Not Available"
}
]
// function to write README file
function writeToFile(fileName, data) {
fs.writeFile(fileName, data, "utf8", function(err) {
if (err) {
console.log(err);}
})}
// function to initialize program
const init = new Promise(function(resolve,reject) {
resolve(inquirer.prompt(questions))
})
// function call to initialize program
init.then(writeToFile("test.md",readme)
readme should run with result when inquirer get all input.
const inquirer = require ('inquirer');
const fs = require ('fs');
const util = require ('util');
const readme = data => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\`\`\`
${data.usage}
\`\`\`
## License
${data.license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `;
};
// array of questions for user
const questions = [
{
type: 'input',
name: 'title',
message: 'What is the title of your project',
default: 'Not Available',
},
{
type: 'input',
name: 'username',
message: 'What is your Github username?',
default: 'Not Available',
},
{
type: 'input',
name: 'email',
message: 'What is your email address?',
default: 'Not Available',
},
{
type: 'input',
name: 'contact',
message: 'How do you want people to reach you?',
default: 'Not Available',
},
{
type: 'input',
name: 'description',
message: 'Please type a short description of your project ( When you are done, hit ESC & type ":wq" to exit editor )',
default: 'Not Available',
},
{
type: 'input',
name: 'installation',
message: 'How do you install this application? ( When you are done, hit ESC type ":wq" to exit editor) ',
default: 'Not Available',
},
{
type: 'input',
name: 'usage',
message: 'Give example of some ways you can use this application ( When you are done, hit ESC & type ":wq" to exit editor)',
default: 'Not Available',
},
{
type: 'list',
name: 'license',
message: 'What kind of license would you like to use?',
choices: [
'MIT',
'Microsoft Public License',
'Mozilla Public License 2.0',
'Academic Free License v3.0',
'Open Software License 3.0',
'Creative Commons Attribution 4.0',
],
default: 'Not Available',
},
{
type: 'confirm',
name: 'contribution',
message: 'Are you open to contribution?',
default: 'Not Available',
},
{
type: 'input',
name: 'contributionRequirement',
message: 'If Yes, What are your requirement for giving contribution?',
default: 'Not Available',
},
{
type: 'input',
name: 'test',
message: 'Please give instructions for testing of this project ( When you are done, hit ESC & type ":wq" to exit editor)',
default: 'Not Available',
},
];
// function to write README file
function writeToFile (fileName, data) {
fs.writeFile (fileName, data, 'utf8', function (err) {
if (err) {
console.log (err);
}
});
}
// function call to initialize program
inquirer.prompt(questions).then(answer => {
writeToFile('test.md', readme(answer))
});

inquirer package, present questions based on previous answers

I'm using NPM 'inquirer' package in order to present the user various questions. One of them is a 'choices' selection.
Is there a way to present follow up questions based on the 'choices' selection?
Here's my code:
const { prompt } = require('inquirer');
require('colors');
const questions = [
{
type: 'input',
name: 'appName',
message: 'Enter application name:'
},
{
type: 'input',
name: 'appDescription',
message: 'Enter app description:'
},
{
type: 'input',
name: 'appAuthor',
message: 'Enter app author:'
},
{
type: 'checkbox',
name: 'plugins',
message: 'Select plugins to install:',
choices: [
{
name: 'Cassandra'
}
]
}
];
module.exports.performQuestions = async () => {
console.log('\nWelcome to Chef!\n'.underline.italic.cyan);
const answers = await prompt(questions);
if (!answers.appName) {
console.error('\nPlease provide app name!\n'.red);
process.exit(1);
}
answers.appType = 'service';
return answers;
};
Here I want to present a few more questions if the user selects 'Cassandra' is that possible?
Thanks.
You can use "when" and like in the example bellow, the second question will popup only if "Cassandra" is selected:
const QUESTIONS = [
{
name: 'your-name',
type: 'list',
message: 'Your name:',
choices: ['Batman', 'Superman', 'Ultron', 'Cassandra'],
},
{
name: 'hello-cassandra',
type: 'confirm',
message: 'Oh, hello Cassandra!',
when: (answers) => answers['your-name'] === 'Cassandra',
},
];
inquirer.prompt(QUESTIONS)
.then(answers =>
{
console.log(answers);
});

Categories