Why Discord.js API dont see message.channel.id? - javascript

I have just created a simple bot with that code:
client.on('message', async message => {
if (!message.author.bot) {
if (!getClub(message) && (message.channel.id == channel_bot || message.channel.type != "dm")) { //
getRadio(message);
}
} else if (message.channel.id = channel_dev && message.author.bot) {
getDevCommands(message);
}
});
and I check bot command with
function getClub(msg) {
const args = msg.content.slice(msg.content.includes(config.prefix) ? config.prefix.length : 0).trim().split(/ +/g);
let isClub = false;
club_commands.forEach(function (element) {
if (element.id == "club" && element.commands.includes(args[0])) {
isClub = true;
}
});
if (!isClub) {
return false;
}
club_commands.forEach(function (element) {
// element is parsed object from JSON: {"id":"join", "commands":"join,attach,invite..."}
if (element.commands.includes(args[1])) {
switch (element.id) {
case "stats":
clubStats(msg);
return true;
case "join":
clubParticipation(msg, 1);
return true;
case "leave":
clubParticipation(msg, 0);
return true;
default:
// do nothing
break;
}
}
});
return false;
}
So in clubPartisipation() im getting in msg.channel.id - actual channel id but only "true" for the all next messages
function clubParticipation(msg, status) {
const args = msg.content.trim().split(/ +/g).splice(2, 2).join("");
if (args.length <= 3) {
msg.channel.send("test0");
} else {
let member = guild.members.get(msg.author.id);
if (status == "1") {
msg.channel.send("test1").catch(console.log);
} else {
msg.channel.send("test3").catch(console.log);
}
getHTTPResponce(config.server_url + 'add/club/participation?channel_id=' + msg.channel.id + '&status=' + status + '&user_id=' + member.id + '&club_id=' + Base64.encode(Base64.encode(args)) + '&token=' + config.server_token, msg)
.catch(console.log);
}
}
Error code is
{ DiscordAPIError: Invalid Form Body
channel_id: Value "true" is not snowflake.
at item.request.gen.end (/root/curatortojps/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15
)
at then (/root/curatortojps/node_modules/snekfetch/src/index.js:215:21)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
name: 'DiscordAPIError',
message: 'Invalid Form Body\nchannel_id: Value "true" is not snowflake.',
path: '/api/v7/channels/true/messages',
code: 50035,
method: 'POST' }

In your first block of code, you have:
(message.channel.id = channel_dev && message.author.bot)
= is an assignment operator. This means that you're setting message.channel.id to the value of channel_dev && message.author.bot, a boolean (true or false).
You should use an equality operator like == or === to compare the value of message.channel.id. For the difference between the two, check out this answer.
(message.channel.id === channel_dev && message.author.bot)

Related

How can I optimize this javascript method code?

In checkData method I am checking 2 API's status as fulfilled or rejected and based on that I am sending API Data and FailFlag. How can i optimize it as I have used if else statements.
const checkData = (data, allData, failFlag) => {
if (data && data[0].status === 'fulfilled' && data[1].status === 'fulfilled') {
if (data[0].value && data[1].value) {
allData.push(...data[0].value, ...data[1].value);
failFlag = 'NONE';
}
} else if (data[0].status === 'rejected' && data[1].status === 'fulfilled') {
allData.push(...data[1].value);
failFlag = 'INTERNAL';
} else if (data[0].status === 'fulfilled' && data[1].status === 'rejected') {
allData.push(...data[0].value);
failFlag = 'EXTERNAL';
console.log('All data with fail flag', allData, failFlag);
} else {
Promise.reject('Both are rejected');
failFlag = 'ALL';
}
return { errorFlag: failFlag, accounts: allData };
};
I'm not sure why you are passing in failFlag, because you're never using that variable. I'm also not sure why you have written Promise.reject('Both are rejected');. However, the code can be simplified as follows:
const checkData = ([{status:s0, value:v0}, {status:s1, value:v1}]=[{},{}], allData) => {
let errorFlag = 'NONE';
let f0 = s0 === 'fulfilled';
let f1 = s1 === 'fulfilled';
if(f0) allData.push(...v0);
if(f1) allData.push(...v1);
if(!f0 && !f1) errorFlag = 'ALL';
if(!f0 && f1) errorFlag = 'INTERNAL';
if( f0 && !f1) errorFlag = 'EXTERNAL';
return { errorFlag, accounts: allData };
};

Function argument in nodejs

Variable when passed through a function returns property of value undefined. I'm passing variable ApiId or ApiName. Should the variable name be wrapped some other way inside the function?
I'm using this answer method and using it inside a function.
function dimensionValue(name) {
console.log("Testing")
return sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
}
If I try to console.log then it works fine.
console.log(sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId').value)
Error:
ERROR Invoke Error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'value' of undefined",
"stack": [
"TypeError: Cannot read property 'value' of undefined",
" at dimensionValue (/var/task/index.js:20:80)",
" at Runtime.exports.handler (/var/task/index.js:37:36)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
code:
function dimensionValue(name) {
console.log("Testing")
//var dimensionsValue = sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
return sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
}
if (sns.Trigger.Namespace == "AWS/ApiGateway") {
console.log("Testing2")
if (sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId') && sns.Trigger.Dimensions.find(dimension => dimension.name === 'Stage')) {
console.log('ApiId and Stage')
var sns_DimensionsValue = dimensionValue('ApiId') + '_' + dimensionValue('Stage')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiName') && sns.Trigger.Dimensions.find(dimension => dimension.name === 'Stage')) {
console.log('ApiName and Stage')
var sns_DimensionsValue = dimensionValue('ApiName') + '_' + dimensionValue('Stage')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId')) {
console.log('ApiId')
var sns_DimensionsValue = dimensionValue('ApiId')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiName')) {
console.log('ApiName')
var sns_DimensionsValue = dimensionValue('ApiName')
}
}
The problem are those sns_DimensionsValue == parts at the start of every condition. Just remove them.
You essentially do if (undefined == findAppId() && findStage()), which will be true if Stage was found and AppId wasn't, but then you attempt to access the value of AppId and fail.
Let me suggest a generally simplified approach:
const dimensions = Object.fromEntries(sns.Trigger.Dimensions.map(d => [d.name, d.value]))
let sns_DimensionsValue = dimensions.ApiId ?? dimensions.ApiName
if (!sns_DimensionsValue) throw new Error('No API ID/API name found')
if (dimensions.Stage) sns_DimensionsValue += '_' + dimensions.Stage

gettin this error in node jd error: TypeError: Cannot read property '0' of undefined

hi i am trying to implement a local orderbook using the binance api however i keep getting this error every now and then it does not always happen but if it does it will happen early on please help
this is the main file that runs calling an exports function from an external file to process the data coming from the web socket on message
wsClient.subscribeSpotDiffBookDepth("btcusdt");
wsClient.on('message', (data) => {
// Setup and process order book information
order_book.update_orderbook(data);
if(order_book.ready === 1){
order_book.get_orderbook_history();
}
});
wsClient.on('error', err => {
/* handle error */
console.log("this is it 2 " + err);
});
wsClient.on('close', () => {
/* ... */
});
exports function in external file
exports.update_orderbook = function(data) {
if(data.e == "depthUpdate"){
if(this.ready === 0){
this.ready = 1;
console.log("Stage 1 in play");
}else{
this.buffer += data;
}
if(this.ready === 2 && this.asks != null && this.bids != null){
if(undefined !== this.asks && undefined !== this.bids && undefined !== data.a && undefined !== data.b){
if(data.U <= this.lastUpdateId + 1 && data.u >= this.lastUpdateId + 1){
// error is coming from calling this function-------------------------------------------------------
var temp_array1 = sort_array(this.asks, data.a);
var temp_array2 = sort_array(this.bids, data.b);
this.asks = temp_array1;
this.bids = temp_array2;
this.lastUpdateId = data.u;
console.log("Stage 3");
}
}
}else{
this.buffer += data;
}
}
}
function to sort and update the array
function sort_array(array1, array2){
for(let x in array2){
if(array2[x][1] == 0){
for(let i in array2){
if(array1[i][0] === array2[x][0]){
array1.splice(i, 1);
}
}
}else{
var in_array = false;
for(let i in array1){
// this seems to be the problem area---------------------------------------------------------
if(array1[i][0] === array2[x][0]){
array1[i] = array2[x];
in_array = true;
}
}
if(!in_array){
array1.push(array2[x]);
}
}
}
return array1;
}
error log

Cannot read property 'first' of null

So I'm currently making a discord bot so whenever someone types !true #USER [Location] it will DM the #USER a message, add a role and then nickname the user to [Location] with the following code :
const mention = message.mentions.members.first();
msg = message.content.toLowerCase();
if (msg.startsWith(prefix)){
if(message.channel.id === '12345'){
if (msg.startsWith (prefix + "true")){
if(!message.member.hasPermission("MANAGE_NICKNAMES")) return message.reply("You have no permission!");
if (mention == null) { return; }
let args = message.content.split(" ").slice(2);
if ((mention.displayName + " " + args.join(" ")).length > 32) return message.channel.send("The nickname exceeds 32 characters")
if(mention.roles.cache.has('1234567890')){
message.reply("User is already accepted.")
} else {
mention.roles.add('1234567890').then(() => mention.setNickname(mention.displayName+" "+args.join(' ')))
.catch((e) => {
console.log('handle error here: ', e.message)
})
}
}}}
However, most of the time it will return with Cannot read property 'first' of null and it won't change the user's nickname (only roles and DM). Is there anything wrong with my code? Thanks.
const mention = message.mentions.members.first();
In the above code, message.mentions.members is null so you can't access a property (here first) in a null object.
The culprit is probably somewhere you set members property, not the code you provided here. do some debugging and console.log and you'll fix the issue.
You got error because you can`t get first of null. Better check mention in a command, like this.
if (msg.startsWith(prefix)) {
if (message.channel.id === '12345') {
if (msg.startsWith(prefix + 'true')) {
if (args.length < 0) return message.reply('PLS mention a member or write his ID');
const mention = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
if (!mention) return message.reply('Can`t find a member');
msg = message.content.toLowerCase();
if (!message.member.hasPermission('MANAGE_NICKNAMES')) return message.reply('You have no permission!');
if (mention == null) {
return;
}
let args = message.content.split(' ').slice(2);
if ((mention.displayName + ' ' + args.join(' ')).length > 32) return message.channel.send('The nickname exceeds 32 characters');
if (mention.roles.cache.has('1234567890')) {
message.reply('User is already accepted.');
} else {
mention.roles
.add('1234567890')
.then(() => mention.setNickname(mention.displayName + ' ' + args.join(' ')))
.catch(e => {
console.log('handle error here: ', e.message);
});
}
}
}
}

unexpected token error for catch javascript

I am banging my head trying to find the error in this code. I have checked it so many times can someone point out where the problem is?
$(function() {
try {
function endswith(str, ends) {
if (ends === '') return true;
if (str == null || ends == null) return false;
str = String(str);
ends = String(ends);
return str.length >= ends.length && str.slice(str.length - ends.length) === ends;
}
var referrer = new URL(document.referrer).domain;
if (endswith(referrer, "xyz.com")) {
$(".logo .logo-external").remove();
} else {
$(".logo .logo-internal").remove();
}
} catch () {}
});
catch (e) {} You missed the variable e
$(function() {
try {
function endswith(str, ends) {
if (ends === '') return true;
if (str == null || ends == null) return false;
str = String(str);
ends = String(ends);
return str.length >= ends.length && str.slice(str.length - ends.length) === ends;
}
var referrer = new URL(document.referrer).domain;
if (endswith(referrer, "xyz.com")) {
$(".logo .logo-external").remove();
} else {
$(".logo .logo-internal").remove();
}
} catch (e) {}
});
As per MDN, try...catch syntax is defined similar to the following:
try {
try_statements
}
...
[catch (exception_var) {
catch_statements
}]
[finally {
finally_statements
}]
This means the exception_var is NOT optional. Otherwise, it would look like this:
...
[catch ([exception_var]) { // Uncaught SyntaxError: Unexpected token )
catch_statements
}]
...

Categories