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.
Related
This question already has answers here:
Discord.js - Cooldown for a command for each user not all users
(3 answers)
Closed 2 years ago.
So i want to upload my discord bot online but i want to prevent users from spamming it's commands.
So let the delay be 5 seconds, if user runs !help the bot answers, but if user run !help before the delay is expired bot says: wait some time before using this command again.
Also i want the delay to work only for message author and not affect other users.
I'm using command handler so is it possible to make something like module.exports.delay?
to do that you can use timestamps like:
let msgTimestamp = [];
if(message.content === "?" + "help") {
if(typeof msgTimestamp[0] !== "undefined") {
if(msgTimestamp[0] + 5000 < Date.now()) {
message.channel.send("Here embed of command help.");
msgTimestamp = [];
} else {
message.channel.send("wait some time before using this command again.");
}
} else {
msgTimestamp.push(Date.now());
message.channel.send("Here embed of command help.");
}
}
In this coode you use timestamps, you check if the content of the message is eualt to the command you want to use, if the user had already sent a message, the variable which containing the message timestamp is full, you check then if the the typeof of the variable is not undefined and then you check id the timestamp of when the user sent the message is lower than the actual timestamp + how many delay you want!
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.
I'm making a discord bot with some messageCollector. This messageCollector is created when the bot detects the !Hello command, but if you write again !Hello, another messageCollector is created.
I checked in the discord.js documentation if it's possible to verify if a collector is already open, but I didn't find it.
I thought to make:
if (message === "!Hello") collector.stop("Stopped");
But I don't know if it's the right solution.
Thanks for your help.
You could make a variable that contains a boolean and just make it set to true when someone does !Hello.
let toggled = false;
if (message === "!Hello" && toggled = true) {
return;
} else if (message === "!Hello" && toggled = false) {
toggled = true;
//run the !Hello code here
}
Hope this works, not sure if it's the answer you wanted but this will stop it from creating more messageCollector's.
My bot (using MS BotFramework) is supposed to be hearing the conversation stream. If someone mentions 'chatbot' it should say 'Here I am!', otherwise stays quiet. It seems to be very simple and maybe it is but I am having a hard time trying to implementing it. Here is what I have:
bot.add('/', function(session) {
if (someoneSaidChatbot) {
session('Here I am!")
} else {
// session.reset(), maybe? No!
// session.endDialog() then? Uh...nope.
// nothing? Hmmm. negative
}
});
So, nothing works. If I leave there the bot just hangs and it stops listening to the stream or answering commands.
Any thoughts?
This code ends a dialog when someone types "chatbot" as part of the utterance. Is this what you are looking for?
bot.add('/', function (session) {
if (session.message.text.search("chatbot") >= 0) {
session.endDialog("Here I am");
}
});
I'd like to suggest use endConversationAction() to register a Bots Global Actions
bot.endConversationAction(
'enddialog', //dialog Id
'Here I am', //message
{ matches: /^.*chatbot/i } //match pattern
);
since this is global action, anytime when bot heard "Chatbot", it will say "Here I am" , if there are some dialog in stack, your proposed solution might not work.
It may also depend on which channel you're using. Some channels don't offer the ability for the Bot to listen to all of the messages in the conversation.
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
}