Run a script before Command Prompt stops - javascript

I have a Discord Bot that edits a message every second. So if the bot goes off, the message will display the last edit.
I would like to, before closing the Node JS process (using Ctrl + C, closing the Command Prompt or any type of closing the process), edit the message to say something like: "The bot is off".
Example:
// something using discord.js
bot.on("close", myFunction);
// or in node, like in browser 'window.onbeforeunload'
node.beforecloseprocess = myFunction;
If theres a way with discord.js, that would be cool and resolve my problem, but if there a way of using Node Js it self or something in the Command Prompt would be better for future projects aside discord.js.

I would use the discord.js event 'close' and call a function that would change the message to 'The bot is off'.
also:
process.on('SIGINT', function() {
console.log("Closing")
}

Related

Node.js - Serverside Command Promp Commands input?

I´m writing a Web MMO with Node.js and for debug and maintenance reasons i would love to have the ability to open my SSH connection, where i also start my server, and write a command like "logout all".
So what are my options to get command prompt input and use it while my server runs?
Example:
I start my server with "node app.js" - server starts.
Now i want to write something into the command prompt - how do i get said input so i can use it in my code?
I was trying to read into node.js-readline but i cant seem to find much about it and everything that i found about it needs a "question" to be asked so it can get the input.
I would love to have something like this:
var command = getCommandlineInput(); //command = "logout all"
It need to get any input any time. A callback function would be good aswell, so i can direktly run it throu a checkCommand() function.
So i just figured out how to do this:
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.on('line', (input) => {
useCommand(input);
});
function useCommand (input)
{
switch (input) {
case "show userlist":
CMD_showUserlist();
break;
}
}
First we initiate readline.
With readline.on we listen for any input you type into the command prompt.
useCommand() takes this input and chose what function to run.
How to work with the given input string is up to you, in my example i just made a hard coded command "show userlist".

Is there a way to make my discord bot differentiate between successful and unsuccessful commands for another discord bot?

I'm trying to make a reminder bot for EPICRPG and other bots, but if for example, I type RPG Hunt, it will start a timer, be it successful or not. Successful means used the cd, unsuccessful means the
hunt was still on cd or the bot didn't accept the command(spam or in event).
I tried having my bot detect the message that starts with RPG hunt, but it will detect unsuccessful commands too.
Then I tried having the bot detect the reply/messages of EPICRPG, so like
user: rpg hunt
EPICRPG: user found and killed...
I tried having the bot detect messages that includes "found and killed" sent by EPICRPG. But there is more than 1 message by EPICRPG that includes "found and killed".
3.Then next I wanted to try having the bot detect message that starts with RPG hunt and any reply that includes "found and killed", so basically a command that detects 2 authors in 2 messages, but someone told me that can't happen so I didn't even try to code that.
I'm using discord.js
1st try code :
if (message.content.toLowerCase().startsWith('rpg hunt')) {
client.command.get('hunt').execute(message, args);
}
2nd try code :
if (message.content.toLowerCase().includes('found and killed')) {
if (message.content.author.id('<#555955826880413696>').toLowerCase().includes('found and killed')) {
client.command.get('hunt').execute(message, args);
}
}
hunt.js:
module.exports = {
name: 'hunt',
description: 'Reminds for hunt commands.',
execute(message, args) {
setTimeout(function(){
message.channel.send(`Time to Hunt, ${message.author}!`);
}, 60000);
}
}

How to send a startup message when my bot gets online in all servers it is in discord. Js?

So I'm basically trying to send my bot update log of commands I have removed and added along with its new version info and data as soon as I start it in my terminal or gets online in all server it's available in currently I'm trying this code :-
Client.on{message.channel.send('Bot Name:- My bot \n Bot Verison :- 1.0.0 \n Owner :- Rega!
You need to add a listener from the client to the ready event. Here's your code, corrected and reformated.
// The callback gets called when the bot is ready
client.on('ready', () => {
// Get the channel from its ID
const logChannel = client.channels.cache.get('channel-id')
// Send the message
logChannel.send('The bot is up!')
})
You naturally need to replace channel-id with the ID of your channel. You can find more about it in this Discord Support article.

discord.js Sending Custom Messsage and Bot Mirrors message

I am newer to coding Discord.js and Im trying to figure this out. This specific command should repeat what the user said and then delete the user message.
For example: !gc Hello World!
Bot responses: Hello World!
Bot Deletes user command and keeps the Bot message.
bot.on('message', message => {
if (!message.content.startsWith(prefix)) return;
message.channel.send(message)
message.delete('message')
});
Hmmm... in order to get the message content, you should use Message.content. Problem is, this will contain all the content, including prefix and command name so you'll have to splice it out.
To delete the message you don't need to pass anything as a parameter. Although you can pass a timer if you don't want to delete the message instantly. For example Message.delete(1000) will delay the message deletion for 1 second.
Hard coding the command you could do it like this:
bot.on('message', message => {
if (!message.content.startsWith(prefix)) return;
if(message.content.startsWith(`${prefix}gc`)) {
// Substring the message so you can rip off the prefix and command name
let msg = message.content;
msg = msg.substring(prefix.length + 3, msg.length);
message.channel.send(msg);
message.delete();
}
});
Notice that this is a suggestion to work as an explanation and isn't a good way to code commands.
You can learn how to create a proper bot and manage the project directory here.
If you want to learn specifically command handling you can go directly to here.

Input through node JS in Terminal

I am trying to make a program in Node.JS that will display some text, using console.log("");, then wait for the user to input some commands. First of all, I want to run this through the Linux Terminal on Cloud9 IDE, which does not pause long enough to input anything. Second of all, I want it to be like its own little command line. (I mean respond to certain case-sensitive commands, and ignore anything else.) Can anyone help with this?
Check out prompt. https://www.npmjs.com/package/prompt It works like:
var prompt = require('prompt');
prompt.start();
prompt.get(['hello'], function (err, result) {
console.log('you typed ' + result.hello);
});
Will do:
$ nodejs prompt.js
prompt: hello: world
you typed world
Happy coding ^^

Categories