BulkDelete Discord.js (How to add interger parsing) - discord.js

How to bulkDelete in discord.js. Please give me a program for it...
bot.on('message', message => {
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case 'Ping':
message.channel.send('Pong!');
break;
case 'clear':
if (!args[1]) return message.reply('Error please mention second arg');
message.delete.bulkDelete(args[1]);
break;
}
})
I've tried the above code and found " TypeError: message.delete.bulkDelete is not a function "
as error.

It's quite simple, you replace:
message.delete.bulkDelete(args[1]);
with:
message.channel.bulkDelete(Number.parseInt(args[1]));

Related

Arguments for Discord.js

how do I read args in discord.js? I am trying to create a support bot and I want to have an !help {topic} command. how do I do that?
my current code is very basic
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = ("!")
const token = ("removed")
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
if (msg.content === 'help') {
msg.reply('type -new to create a support ticket');
}
});
client.login(token);
You can make use of a prefix and arguments like so...
const prefix = '!'; // just an example, change to whatever you want
client.on('message', message => {
if (!message.content.startsWith(prefix)) return;
const args = message.content.trim().split(/ +/g);
const cmd = args[0].slice(prefix.length).toLowerCase(); // case INsensitive, without prefix
if (cmd === 'ping') message.reply('pong');
if (cmd === 'help') {
if (!args[1]) return message.reply('Please specify a topic.');
if (args[2]) return message.reply('Too many arguments.');
// command code
}
});
you can use Switch statement instead of
if (command == 'help') {} else if (command == 'ping') {}
client.on ('message', async message => {
var prefix = "!";
var command = message.content.slice (prefix.length).split (" ")[0],
topic = message.content.split (" ")[1];
switch (command) {
case "help":
if (!topic) return message.channel.send ('no topic bro');
break;
case "ping":
message.channel.send ('pong!');
break;
}
});
let args = msg.content.split(' ');
let command = args.shift().toLowerCase();
this is the simplified answer from #slothiful.
usage
if(command == 'example'){
if(args[0] == '1'){
console.log('1');
} else {
console.log('2');
You can create a simple command/arguments thing (I don't know how to word it correctly)
client.on("message", message => {
let msgArray = message.content.split(" "); // Splits the message content with space as a delimiter
let prefix = "your prefix here";
let command = msgArray[0].replace(prefix, ""); // Gets the first element of msgArray and removes the prefix
let args = msgArray.slice(1); // Remove the first element of msgArray/command and this basically returns the arguments
// Now here is where you can create your commands
if(command === "help") {
if(!args[0]) return message.channel.send("Please specify a topic.");
if(args[1]) return message.channel.send("Too many arguments.");
// do your other help command stuff...
}
});
You can do
const args =
message.content.slice(prefix.length).trim().split(' ');
const cmd = args.shift().toLocaleLowerCase();
Word of advice, use a command handler and slash commands - this will solve both the need for a help command and reading arguments. Also helps with readability.
Anyways...
message.content.split(' '): This will split your string into an array of sub-strings, then return a new array.
.shift(): This will remove the first index in the array.
Combining this will get you your arguments: const args = message.content.split(' ').shift()

Discord.JS code wont work?

So im making a myth hunters bot for roblox, and I want to copy all users with a certain rank, but thatch not what I want to do, Im just wondering why this will not work. No errors and no reply
var args = message.content.substring(prefix.length).split(" ");
switch (args[0].toLowerCase()) {
case "info":
let mythUser = message.content.replace("mh>info ", "");
if (mythUser === "fodloca") {
var fodLoceEmb = new discord.RichEmbed()
.setTitle("User: Fodloca")
.addField("ID: 663751421", "Description: Hey, I'm Fodloca..")
.setThumbnail("https://www.roblox.com/outfit-thumbnail/image?userOutfitId=663751421&width=420&height=420&format=png")
message.channel.send(forLoceEmb);
}
break;
}
Use == not ===
var args = message.content.substring(prefix.length).split(" ");
switch (args[0].toLowerCase()) {
case "info":
let mythUser = message.content.replace("mh>info ", "");
if (mythUser == "fodloca") {
var fodLoceEmb = new discord.RichEmbed()
.setTitle("User: Fodloca")
.addField("ID: 663751421", "Description: Hey, I'm Fodloca..")
.setThumbnail("https://www.roblox.com/outfit-thumbnail/image?userOutfitId=663751421&width=420&height=420&format=png")
message.channel.send(forLoceEmb);
}
break;
}

Compare values in an array using if conditions in a for loop

so I'm utilizing the Ocrad.js library in an ionic project to be able to read text from images. In the app, the user can add 'items' (as in words) to an array and then I would like to check whether or not these items (words) are present in the text from the image. For instance: if the image has a sentence: 'I like football' and the user added the word 'football' to the list, on the press of a button, the app would check whether or not 'football' exists in 'I like football' and would say so to the user. In an else occasion, it would also say so.
So far I came up with this situation:
for(let item of this.itemList) {
if(text.indexOf(item.name)>=0){
alert('word found');
} else {
alert('word not found');
}
}
The idea was to loop through the list of items(words) that the user had added to the array and give them the appropriate response. While it is working if I add just one word, like the football example I mentioned above, if I add more words to the list, the loop will obviously give me the 2 alerts. So if add the word 'soccer' to the list and therefore have an array with 'football' and 'soccer', I would get the 2 alerts, which makes sense because I wasn't stopping the loop. So I tried a switch case, which did not work for some reason (I don't really know why, but it kept giving me the two alerts). This is what my switch looked like:
for(let item of this.itemList){
switch(true){
case (text.indexOf(item.name)>=0): {
alert('word found');
break;
}
case (text.indexOf(item.name) <= 0):{
alert('word not found');
break;
}
default: {
alert('please add a word to the list');
break;
}
}
}
So after playing around and researching, I could not really find something that helped me very well. The idea again is if the image text says 'I like football' and I add 3 items to the array: 'soccer', basketball', 'football', the answer would be 'word found' only, whereas if the word is not there, I would get 'word not found' only once! I believe I might be doing something really stupid that I can't see with my noobs' eyes haha. I hope my question made sense. Can anyone help me with this? Cheers guys!
Edit - This is the function I am actually using with the camera feature:
onAnalyze() {
let matchFound = false;
let loader = this.loadingCtrl.create({
content: 'Please wait...'
});
loader.present();
(<any>window).OCRAD(document.getElementById('image'), text => {
loader.dismissAll();
alert(text);
console.log(text);
for(const item of this.itemList)
{
if(text.indexOf(item) >=0){
matchFound = true;
break;
}
}
if(found){
alert('word found!');
} else{
alert('word not found!');
}
}
);
}
In the first example you provided, the for-loop continues to evaluate the input string, even though you've already found a match (which I believe is what you want, to see only one alert for any number of successful matches). The below example introduces a boolean matchFound, which is set to true on the first match. If a match is found, we break out of the for-loop, terminating its execution (and preventing the N number of alerts):
const itemList = ['football', 'nachos', 'sports'];
const textFromImage = 'I like football and nachos';
isTextPresent(itemList, textFromImage);
function isTextPresent(itemList: string[], textFromImage: string) {
let matchFound = false;
for (const item of this.itemList) {
if (textFromImage.indexOf(item) >= 0) {
matchFound = true;
break;
}
}
if (matchFound) {
alert('word found');
} else {
alert('word not found');
}
}
FWIW, Array.prototype.some() could also be used here as well. On the first match from the textFromImage, it terminates (much like the for-loop that breaks in the first example):
const itemList = ['football', 'nachos', 'sports'];
const textFromImage = 'I like football and nachos';
isTextPresent(itemList, textFromImage);
function isTextPresent(itemList: string[], textFromImage: string) {
const matchFound = itemList.some((item: string) => {
return textFromImage.indexOf(item) >= 0;
});
if (matchFound) {
alert('word found');
} else {
alert('word not found');
}
}
You should use an nested for loop.
let words = ['soccer', basketball', 'football']
let foundedWords = []
for(let item of this.itemList) {
for(let word in words){
if(text.indexOf(word)>=0){
foundedWords.push(word)
alert('word found');
} else {
alert('word not found');
}
}
}
And finally you will get a list of founded words.
I think this should be fine for you.

AngularJS: When applying custom filter to data, i get an empty output

I'm trying to apply a filter to some data that I'm pulling out of Firebase.
My HTML look like this:
<span ng-bind-html="game.rating | ratings"></span>
Note: If I remove the "| ratings" it does work, and output the original text from firebase.
But once I apply this filter, I don't get any output, it's just empty. :(
I'm totally new to both Angular, javascript and firebase. So any advice is very appreciated
'use strict';
MyApp.filter('ratings', function() {
return function(rating) {
switch(rating) {
case 1:
return "1star";
case 2:
return "2stars";
case 3:
return "3stars";
case 4:
return "4stars";
}
}
})
Have any of you seen this before?
Maybe the output is not an int? Parse it. And use a default, its good practice.
MyApp.filter('ratings', function() {
return function(rating) {
switch(parseInt(rating)) {
case 1:
return "1star";
case 2:
return "2stars";
case 3:
return "3stars";
case 4:
return "4stars";
default:
return "Not set";
}
}
})

Extjs - newbie, Switch case function not working

In this following code I am getting the desired output
fn: function(btn) {
switch(btn){
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?');
break;
case 'no':
Ext.Msg.alert('Milton',
'Im going to burn the building down!');
break;
case 'cancel':
Ext.Msg.wait('Saving tables to disk...','File Copy');
break;
}
}
This Works just fine. Now i am trying to do a function call in the switch 'yes' but i don't get any output in the screen.
This is the code i am using.
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?', function(btn,txt)
{
if (txt.toLowerCase() == 'the office') {
Ext.get('my_id').dom.innerHTML = 'Dull Work';
}else{
Ext.get('my_id').dom.innerHTML = txt;
}
Ext.DomHelper.applyStyles('my_id',{
background: 'transparent
url(images/stapler.png) 50% 50% no-repeat'
});
});
break;
Using this code inside the swich case 'yes' , i am getting a blank screen. Even the dialog box has dissapeared. Please help.
It is a very basic javascript error. A string cannot extend over the end of the line. This will work.
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?', function(btn,txt){
if (txt.toLowerCase() == 'the office') {
Ext.get('my_id').dom.innerHTML = 'Dull Work';
}else{
Ext.get('my_id').dom.innerHTML = txt;
}
Ext.DomHelper.applyStyles('my_id',{
background: 'transparent url(images/stapler.png) 50% 50% no-repeat'
});
});
break;
In the console I got the error SyntaxError: Unexpected token ILLEGAL

Resources