How to make unknown command prompt in discord.js - node.js

I want to have my discord.js bot respond
Unknown command, use !help for available commands" when doing something like, !hep (misspelled), or a different type of command not implemented, like, !lol, or just flat out random letters like !jajaja.
Basically just a response if their message doesn't match any commands available.
const prefix = '&'
if (!message.content.startsWith(prefix)) return
let [command, ...args] = msg.content.slice(prefix.length).split(/\s+/g)
switch(command) {
case "help":
// help code here
break;
case "test":
message.channel.send('test')
break;
default:
message.channel.send(`run ${prefix}help to get a list of commands`)
break;
}
Here is the code I have created. Only issue is that it is doing it for every command, I want it to only do it for the commands that are not available.
I have looked at other posts, but either they give me errors, or give me duplications. Also, lots of post are similar to my issue, it is doing it for every command.

Related

im making a purge command for my discord bot but it aint working i intend it to

im trying to make a purge command for my discord bot in node.js but everytime it removes one less than it should do(cus the bot thinks the message i send is also one of the messages i want 2 delete) but when i try to do some simple math to just make the number one higher i get something like this:
10 + 1 = 101 at least that is what the code does this is the code for the purge command i currently use:
const args = msg.content
.slice(prefix.length)
.trim()
.split(" ");
const Args2 = args[2];
const DeleteAmount = Args2;
if (!args[2]) return msg.channel.send("Please Specify a amount");
if (args[2] > 100) return msg.channel.send("You cant delete a amount higher than 100")
if (args[2] < 1) return msg.channel.send("You cant delete less than one message")
msg.channel.bulkDelete(DeleteAmount);
msg.channel.send(`${DeleteAmount} messages have been deleted`);
} else if(msg.content.startsWith(`${prefix}PurgeTest`)) {
msg.channel.send("You Do not have Permission to delete messages");
msg.channel.send("Only people with the role CustomBotAdmin can delete messages");
}
sorry for the messy text...
edit: ah i got it fixed

Discord.js Problems With Default Command in Switch

So I'm trying to make a discord bot, and I am trying to set up a way to get the bot to ask to use the help command when an input isn't defined, but when I set the default in the switch statement, it just repeats itself over and over, #ing itself with the text inputted.
switch(args[0]){
case 'ping':
message.channel.send('pong!');
break;
case 'rockLink':
message.channel.send('') //text here, deleted to protect link
break;
case 'info':
if(args[1] === 'description'){
message.channel.send('I am eventually going to play rock music, for now I do random stuff.')
}else if(args[1] === 'author'){
message.channel.send('I was made by IAmAGreenFlamingo')
}else if(args[1] === 'version'){
message.channel.send('This bot is in version ' + version)
}else{
message.channel.send('Invalid Argument, please use !help info to see all valid arguments!')
}
break;
case 'help':
if(args[1] === 'info'){
message.reply('Arguments for !info: description, author.')
}else{
message.reply('The commands so far are: !ping, !rockLink, and !info (use !help info for arguments)!')
}
break;
default:
message.reply('Invalid Argument, please use !help to see all commands!')
break;
}
It is good practice to ignore bots and DMs for commands. Add something like this at the top of your .on('message') statement:
if(message.author.bot || !message.guild) return //ignores bot messages and pms
This should also stop it from answering to itself.
Another option is to replace the message.replys with message.sends so it doesn't tag itself or anyone else.

amqp assertQueue bork a connection meaning

In amqp's assertQueue API Documentation, it states:
Assert a queue into existence. This operation is idempotent given identical arguments; however, it will bork the channel if the queue already exists but has different properties (values supplied in the arguments field may or may not count for borking purposes; check the borker's, I mean broker's, documentation).
http://www.squaremobius.net/amqp.node/channel_api.html#channel_assertQueue
I am asking what it means by bork(ing) the channel. I tried google but can't find anything relevant.
Bork: English meaning is to obstruct something.
As per the documentation in the question, it says
however, it will bork the channel if the queue already exists but has
different properties
this means if you try to create a channel which has the same properties of a channel which already exits, nothing would happen cause it is idempotent (meaning repeating the same action with no different result, e.g. a REST API GET request which fetches data for id say 123, will return the same data every time unless updated, a pretty funny video explaining the impotent concept), but if you try to create a channel with the same name but different properties, the channel creation shall be "borked" i.e. obstructed.
In the code below, we create the channel again,
var ok0 = ch.assertQueue(q, {durable: false});// creating the first time
var ok1 = ch.assertQueue(q, {durable: true});// creating the second time again with different durable property value
it throws an error
"PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'hello' in
vhost '/': received 'true' but current is 'false'"
This means the you are trying to make the same channel with different properties, i.e. the durable property is different to the existing channel and hence it has been borked.
[2]: Answer by #Like Bakken
The RabbitMQ team monitors this mailing list and only sometimes answers questions on StackOverflow.
Having said that, did you try calling assertQueue twice, with different properties the second time? You would have answered your own question very quickly.
I used this code to create this test program:
#!/usr/bin/env node
var amqp = require('amqplib');
amqp.connect('amqp://localhost').then(function(conn) {
return conn.createChannel().then(function(ch) {
var q = 'hello';
var ok0 = ch.assertQueue(q, {durable: false});
return ok0.then(function(_qok) {
var ok1 = ch.assertQueue(q, {durable: true});
return ok1.then(function(got) {
console.log(" [x] got '%s'", got);
return ch.close();
});
});
}).finally(function() { conn.close(); });
}).catch(console.warn);
Then, start RabbitMQ and run your test code. You should see output like this:
$ node examples/tutorials/assert-borked.js
events.js:183
throw er; // Unhandled 'error' event
^
Error: Channel closed by server: 406 (PRECONDITION-FAILED) with message "PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'hello' in vhost '/': received 'true' but current is 'false'"
at Channel.C.accept

how to move to other section in script

I'm building a bot using gupshup.io using the scripting way ... but handling some things in default.js file as mentioned in the docs
I'm trying in a handler function to check if the event.message equals specific string to go to another section in the script
can anybody please help ?
thanks a lot
So to achieve that you can create a child state to go another section and just set options.next_state to that state. I mean suppose you have a script like this
[main]
inputParser:Welcome to New Bot.
thisFlow:
This is a output of this flow.
callAnotherFlow:
:call default.anotherFlow
[anotherFlow]
This is another flow.[[Wow, No]]
Wow
Thanks
No
Oh!
So in case if the message is 'another flow' you want the second flow to begin. So in the input parser you can create something like.
module.exports.main = {
inputParser: (options, event, context, callback)=>{
if(event.message.toLowerCase() === "another flow"){
options.next_state = 'callAnotherFlow';
}else{
options.next_state = 'thisFlow';
}
callback(options, event, context);
}
}
I think this is what you are looking for.

Node.js - How to parse arguments passed via an IRC chat message

EDIT: I am using the twitch-irc library from Github for Node.js, installed via npm.
This is such a basic question and I feel like a bit of an idiot for asking, but...
if (message.toLowerCase().indexOf('!os') === 0) {
}
Let's say I used this code, how would I add if statements for the arguments that the user provides? By this, I mean after typing !os, they would create a space and type something else, this would either be the 0th or 1st argument.
Would it be possible to just use this?:
if (message.toLowerCase().indexOf('kernel') === 4) {
}
In pseudo code, I want to do:
if (firstargument === 'kernel') {
//Do stuff.
}
Thank you for your time.
You could simply use var args = message.match(/\S+/g) to get all the arguements (including '!os') in an array and use them as you wish.
e.g.
var args = message.match(/\S+g/);
var cmd = args[1]; //do a length check before accessing args[1] though
switch (cmd) {
case 'kernel':
// do stuff
break;
case 'process':
// do stuff
break;
default:
// invalid command
}
What you said would make sense to do, but you should remove white space (spaces) so that a command like !os kernel will still work (note the two spaces).
Do that like this:
if (message.replace(/\s\g, '').toLowerCase().indexOf('kernel') === 3) {
//Do stuff
}

Resources