Discord.js creating channel error - Invalid Form Body - node.js

I've been trying to create a ticket bot and create channels that have tickets within them. However, when I try and create them it returns an error
Code:
const channel = await interaction.guild.channels.create(`ticket-${interaction.user.username}`, {
parent: '929135588949512232',
topic: interaction.user.id,
type: 'GUILD_TEXT',
})
Error:
DiscordAPIError[50035]: Invalid Form Body
name[BASE_TYPE_REQUIRED]: This field is required
Any help would be appreaciated

If you look at the documentation, only one parameter is required for the channel create method, therefore the code should look like this:
const channel = await interaction.guild.channels.create({
name: `ticket-${message.interaction.username}`,
parent: "929135588949512232",
topic: interaction.user.id,
type: Discord.ChannelType.GuildText,
});
Refer to the docs here
https://discord.js.org/#/docs/discord.js/main/class/GuildChannelManager?scrollTo=create
The error is caused, because you passed in information, such as the type, in the second parameter, not the first.

Related

Mongoose query with await returns undefined

Following code is written in a loop in a express route handler;
const projects = await Projects.find({});//working fine
for (let i = 0; i < projects.length; i++) {
const project = projects[i];
const build = await Build.find({ id: project.latest_build});
//logger.debug(build[0].status); //error
const features = await Feature.find({ build_id: project.latest_build});
logger.debug(features[0].status);
}
The above code gives error at liine 4.
(node:672375) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'status' of undefined
However, It is correctly printed in the logs. Seems like the value of the variable is being filled by a promise lazily.
But Line number 6 always works fine.
Note: I'm not performing any other read/write operations on above collections.
Update
id is unique key for Build collection.
build_id is normal field for Feature collection.
Schemas and Documents are created like this
Schemas.Projects = new mongoose.Schema({
id: {
type: String,
unique: true
},
title: String,
latest_build: String
});
Schemas.Build = new mongoose.Schema({
id: {
type: String,
unique: true
},
run_date: Date,
status: String,
});
Schemas.Feature = new mongoose.Schema({
id : String,
build_id: String,
summary : String,
status: String,
flows: Number,
});
const Projects = mongoose.model('Projects', Schemas.Projects);
const Build = mongoose.model(`Build_${projId}`, Schemas.Build);
const Feature = mongoose.model(`Feature_${projId}`, Schemas.Feature);
The problem with the code in question is that it was overly optimistic in its expectations. This line...
const build = await Build.find({ id: project.latest_build });
logger.debug(build[0].status)
... assumed the query always finds at least one Build document. But, as the code was running as a part of the loop, the results were actually mixed:
at the first iteration, the query did find the Build object by the first project's data, and correctly logged its status
at the second iteration it gave back an empty array instead. Then the code threw an Error on attempting to access status property of build[0] (which was undefined).
The appropriate solution for this issue depends on how you should treat those cases. If each Project must have a corresponding Build (it's a strictly 1-1 relation), then having an Error thrown at you is actually fine, as you just have to fix it.
Still, it might be worth treating it as a 'known error', like this:
const buildId = project.latest_build;
const builds = await Build.find({ id: buildId });
if (!builds.length) {
logger.error(`No build found for ${buildId}`);
continue;
}
In this case (assuming logger is set up correctly) you won't have your code blowing up, yet the error will be logged. But if it's actually an ok situation to have no builds yet, just drop the logging - and treat this case as a known edge case.

How to delete user from channel

When trying to delete a user, an error message is displayed:
Error removing user from conversation: Error: StreamChat error code 4: UpdateChannel failed with error: "cannot add or remove members in a distinct channel, please create a new distinct channel with the desired members"
Below is a sample code to delete a user:
try {
const response = await conversation.removeMembers(
[user.id],
{ text: `${user.name} was removed from conversation`}
);
console.log('Response: ', response);
console.log(`${user.name} was removed from conversation`);
} catch (e) {
console.log(`Error removing user from conversation: ${e}`);
}
Conversation:
On deletion, the first request returns OK status with the conversation
We ran into this issue also. Had to change the way that were creating channels. The docs make this really hard to find but on this page it says You cannot add/remove members for channels created this way.
Meaning if you want to create a channel in which members can be added and removed you have to create it like shown here where you assign the channel a unique ID that you create (we are using a UUID). Once it's been created you can add your desired members to it... and then later add or remove them.

I'm trying to make sense of session, but cannot get any data out of it

Currently using LUIS in a bot that connects to Slack. Right now I'm using interactive messages and trying to respond to user input correctly. When I click an item from the drop down LUIS receives it as a message. I can get the text with session.message.text, however I need to get the callback_id of the attachment as well as the channel it was sent from.
I've used console.log(session) to get an idea of what session looks like. From there I've seen that session.message.sourceEvent contains the data I need, however I can't use indexOf() or contains() to actual extrapolate the data. I've also tried session.message.sourceEvent.Payload but end up getting "[object [Object]]". I've tried searching for documentation on session formatting but to no avail.
Below is a snippet of what is returned when I run console.log(session.message.sourceEvent).
{ Payload:
action_ts: '1513199773.200354',
is_app_unfurl: false,
subtype: 'bot_message',
team: { id: 'T03QR2PHH', domain: 'americanairlines' },
user: { id: 'U6DT58F2T', name: 'john.cerreta' },
message_ts: '1513199760.000073',
attachment_id: '1',
ts: '1513199760.000073' },
actions: [ [Object] ],
callback_id: 'map_selection1',
original_message:
username: 'Rallybot',
response_url: 'https://hooks.slack.com/actions/T03QR2PHH/287444348935/Y6Yye3ijlC6xfmn8qjMK4ttB',
type: 'message',
{ type: 'interactive_message',
channel: { id: 'G6NN0DT88', name: 'privategroup' },
token: 'removed for security',
{ text: 'Please choose the Rally and Slack team you would like to map below.',
bot_id: 'B7WDX03UM',
attachments: [Array],
trigger_id: '285857445393.3841091595.085028141d2b8190b38f1bf0ca47dd88' },
ApiToken: 'removed for security' }
session.message.sourceEvent is a javascript Object, however indexOf or contains are functions of String or Array types.
Any info you required in the object, you should direct use the code <object>.<key> to invoke that value. You can try session.message.sourceEvent.Payload.action_ts for example.
Also, you can use Object.keys(session.message.sourceEvent) to get all the keys in this object.

Missing required field: member

{ errors:
[ { domain: 'global',
reason: 'required',
message: 'Missing required field: member' } ],
code: 400,
message: 'Missing required field: member' }
I get this error when I run the following request:
var request = client.admin.members.insert({
groupKey: "some_group#example.com"
, email: "me#example.com"
});
I was authenticated successfully (I received the access token and so on) but when I execute the request above it callbacks that error.
What member field am I supposed to add?
It works fine in API Explorer using groupKey and email fields.
The documentation at https://developers.google.com/admin-sdk/directory/v1/reference/members/insert for admin.members.insert indicates that it requires a groupKey parameter, but that the body (which the node.js library handles as a separate object) should contain a members object containing the role property. See the API Explorer a the bottom of that page as well.
email is part of the form data. The form data must be passed as object in the second argument:
// create the group insert request
var request = client.admin.members.insert({
groupKey: "some_group#example.com"
}, {
email: "me#example.com"
});

Programming with node.js and mongoose. Error id want to pass the value to update registration

I'm learning to use with mongoose and node.js when editing to generate a content that gives me the error: 500 TypeError: Can not read property 'ObjectID' of undefined. Check whether the parameter that I send comes empty and is not.
Remove the option and gives me that error when saving the data did not find the method I use to save some registry update.
If I can help as I have code in https://github.com/boneyking/PruebaNode.git
First of all, please provide code or at least a link to the code. A link to your github repo is not cool !!!
Now back to the question... Add an id to your model and get it like other types:
var SUPER = mongoose.model('Producto',{
nombre: String,
descripcion: String,
precio: Number,
id: mongoose.Schema.Types.ObjectId
});
SUPER.editar = function(newData, callback){
SUPER.findOne({id: newData.id}, function(e,o){
o.nombre = newData.nombre;
o.descripcion = newData.descripcion;
o.precio = newData.precio;
SUPER.save(o);
callback(o);
});
}

Resources