Detecting an embed title in discord.js - discord.js

if(message.content.toLowerCase().includes('kuba144i'))
message.channel.send("Key Word Detected ");
else
for(var i = 0; i < message.embeds.length; i++) {
if(message.embeds[i].title.includes("kuba144i") || message.embeds[i].title.includes("kuba144i")) {
message.channel.send("Detected");
break;
}
}
After sending an embed the bot crashes and this error displays in console: "TypeError: Cannot read property 'includes' of null"
can somebody help me with it ?

Try changing your for loop to this:
message.embeds.some((item) => {
if(item.title.includes("kuba144i") || item.title.includes("kuba144i")) {
message.channel.send("Detected");
return false;
}
return true
})

Related

TypeError: Cannot read properties of null (reading 'permissions')

I'm trying to make an automod, which only moderates users who don't have the admin permission, but for some reason keep getting the error "TypeError: Cannot read properties of null (reading 'permissions')". Here's my code:
client.on('messageCreate', message => {
if (!message.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
for (var i = 0; i < bannedWords.length; i++) {
if (message.content.toLowerCase().includes(bannedWords[i])) {
bannedWordEmbed = {
title : "That word is not allowed here!",
color : embedColor,
author : {
name : message.author.username,
icon_url : message.author.displayAvatarURL()
}
}
message.delete();
message.author.send({embeds : [bannedWordEmbed]});
logToStaff(message);
return;
}
}
}
})

I have an array in a json and when I try to access it with the following code it has an error for multiple words. Can anybody help fix the code

The error code it shows is:
msg.channel.send(spell.spelldictionary[i][1])
^
TypeError: Cannot read property '1' of undefined
The index code is:
const Discord = require('discord.js')
const bot = new Discord.Client()
const token = '';
const PREFIX = '%';
const spell = require('./spells.json')
bot.on('message', msg =>{
if(!msg.author.bot && msg.content.startsWith(PREFIX))
{
let args = msg.content.substring(PREFIX.length).split(" ");
switch(args[0])
{
case 'D&D':
switch(args[1])
{
case 'spellinfo':
let spellname = args.slice(2);
for(i = 0; i < spell.spelldictionary.length; i++)
{
if(spellname == spell.spelldictionary[i][0])
{
break;
}
}
msg.channel.send(spell.spelldictionary[i][1])
break;
}
break;
}
}
}
bot.login(token)
and the JSON file is the following:
{
"spelldictionary": [
["Acid Splash","a"],
["Aid","a"],
["Alarm","a"],
["Alter Self","a"],
["Animal Friendship","a"],
["Animal Messenger","a"],
["Animal Shapes","a"],
["Animate Dead","a"],
["Animate Objects","a"],
["Antilife Shell","a"],
["Antimagic Field","a"],
["Antipathy","a"],
["Arcane Eye","a"],
["Arcane Gate","a"],
["Arcane Lock","a"],
["Armour of Agathys","a"],
["Arms of Hadar","a"],
["Astral Projection","a"],
["Augury","a"],
["Aura of Life","a"],
["Aura of Purity","a"],
["Aura of Vitality","a"],
["Awaken","a"],
["Bane","a"]
]
}
Any help would be appreciated, however I don't understand much JavaScript as I am a beginner so can you try not to make answers too complicated.
If spellname is not in spell.spelldictionary, i becomes spell.spelldictionary.length after the for loop, and the execution of msg.channel.send(spell.spelldictionary[i][1]) causes the error.
You can avoid it by moving msg.channel.send before break in the for loop, so that no message will be sent in this case. Also, it is better to explicitly declare i before using it, or it may cause some unexpected bugs after your code becomes more complicate.
let spellname = args.slice(2);
for(let i = 0; i < spell.spelldictionary.length; i++) // <<
{
if(spellname == spell.spelldictionary[i][0])
{
msg.channel.send(spell.spelldictionary[i][1]); // <<
break;
}
}
// do nothing here
Advanced solution
To prevent this kind of error, you can try to use some methods of Array. In this case, you may want to use array.find().
It returns the first element that satisfies the test function, or undefined if no such element exists.
In your case, the test function is elm => elm[0]==spell, so you can rewrite it to:
// `elm` is the same as `spell.spelldictionary[i]` in your code
const elm = spell.spelldictionary.find(elm => spellname==elm[0]);
if (elm !== undefined) { // if found
msg.channel.send(elm[1]);
}

Protractor Failed: The "url" argument must be of type string. Received type undefined

I am new to Protractor and wiritng an AngularJS protractor test. I am trying to read a Url from Google Chrome console but getting an error "Failed: The "url" argument must be of type string. Received type undefined". My code is :
let address;
.
.
function readingUrl() {
browserA.manage().logs().get('browser')
.then(function (browserLog) {
var i = 0;
for (i; i <= browserLog.length - 1; i++) {
if (browserLog[i].message.indexOf('My Url:') != -1) {
address = browserLog[i].message.split('My Url:')[1].toString();
return address;
}
}
});
}
I am calling this function like this:
browserB.get(readingUrl()).then(() => {
browserB.sleep(3000);
});
The url in the Chrome console looks like this:
My Url:https://www.google.com/
Can someone help me, I am trying to do this and stuck here like forever
this should work
readingUrl().then(url =>
browserB.get(url)
.then(() => browserB.sleep(3000))
)
Try below function:
Instead of returning from "then block" return from readingUrl main function.
let address;
.
.
function readingUrl() {
browserA.manage().logs().get('browser')
.then(function (browserLog) {
var i = 0;
for (i; i <= browserLog.length - 1; i++) {
if (browserLog[i].message.indexOf('My Url:') != -1) {
address = browserLog[i].message.split('My Url:')[1].toString();
}
}
});
return address;
}

Error while trying to delete multiple values from the DB in a loop using Sequelize

I am trying to delete values selected by the user in a loop.
for (var i = 0; i < myArray.length; i++) {
MyService.myMapService.delete({id: myArray[i].myId }, myArray[i], function (response) {
if (response.isSuccessful) {
MyService.removeMyValue(myArray[i].myId);
}
})
}
However, I keep receiving the following error
angular.js:13920 TypeError: Cannot read property 'myId' of
undefined
Please help!
What happening is, your for loop is getting executed for all MyService.myMapService.delete function call and your i is becoming myArray.length. There after when myMapService.delete async call completed, it tries to do myArray[i].myId(where i is array length, then myArray[i] would be undefined). In this case you have to preserve value of by wrapping it to self executing function.
Code
for (var i = 0; i < myArray.length; i++) {
(function(j) {
MyService.myMapService.delete({
id: myArray[j].myId
}, myArray[j], function(response) {
if (response.isSuccessful) {
MyService.removeMyValue(myArray[j].myId);
}
})
})(i);
}

screeps.com: simple script not working, issue with bi-dimensional array

I get the following error from the console: "TypeError: Cannot read property '0' of undefined", it has something to do with the array but I cannot find the mistake.
module.exports =
{
create_creeps: function()
{
var aHarvester = [[TOUGH,TOUGH, MOVE, CARRY, WORK, MOVE]["harvester"]];
Spawn.prototype.createMyCreep = function(aCreep,sRole) {
if (!Game.spawns.Spawn1.spawning){
var nameCount = 0;
var name = null;
while(name == null)
{
nameCount++;
var tryName = sRole + nameCount;
if(Game.creeps[tryName] == undefined)
name = tryName;
}
var dbg= this.canCreateCreep(aCreep, name);
if(dbg == OK) {
console.log("Creating creep " + sRole);
return this.createCreep(aCreep , name,{ role:sRole });
} else {
console.log("error "+dbg+" "+sRole);
}
}
};
Game.spawns.Spawn1.createMyCreep(aHarvester[0],aHarvester[1][0]);
};
}
Please separate arrays with a comma
So
[[TOUGH,TOUGH, MOVE, CARRY, WORK, MOVE]["harvester"]]
Should be
[[TOUGH,TOUGH, MOVE, CARRY, WORK, MOVE],["harvester"]]

Resources