-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create revolt-bot-with-cmd-interface-interprocess-ipc-complete-notrev…
…ised.js
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
Prototypes/revolt-bot-with-cmd-interface-interprocess-ipc-complete-notrevised.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const { spawn } = require('child_process'); | ||
const readline = require('readline'); | ||
|
||
function myFunction() { | ||
const { Client } = require('revolt.js'); | ||
console.log('Child process is running'); | ||
console.log('This is stdout'); | ||
console.error('This is stderr'); | ||
|
||
let client = new Client(); | ||
|
||
client.on('ready', async () => console.info(`Logged in as ${client.user.username}!`)); | ||
|
||
client.on('messageCreate', async (message) => { | ||
if (message.content === 'hello') { | ||
message.channel.sendMessage('world'); | ||
} else if (message.content === 'restart') { | ||
process.send('restart'); | ||
message.channel.sendMessage('world'); | ||
} | ||
}); | ||
|
||
client.loginBot('pYX_8GUtHmGJBvP1pvWhYIBzSyVnFhrEZHIp8p7CRDzAXOFLD1AShXJSPq7DZ8I5'); | ||
} | ||
|
||
let child; | ||
|
||
function startChildProcess() { | ||
child = spawn('node', ['-e', `(${myFunction.toString()})();`], { stdio: ['pipe', 'pipe', 'pipe', 'ipc'] }); | ||
|
||
// Listen to stdout | ||
child.stdout.on('data', (data) => { | ||
process.stdout.write(`stdout: ${data}`); | ||
}); | ||
|
||
// Listen to stderr | ||
child.stderr.on('data', (data) => { | ||
process.stderr.write(`stderr: ${data}`); | ||
}); | ||
|
||
// Listen for the child process to exit | ||
child.on('exit', (code, signal) => { | ||
console.log('Child process exited with code', code); | ||
}); | ||
|
||
// Listen for messages from the child process | ||
child.on('message', (message) => { | ||
if (message === 'restart') { | ||
// Kill the current child process | ||
child.kill(); | ||
|
||
// Start a new child process | ||
startChildProcess(); | ||
} | ||
}); | ||
} | ||
|
||
// Start the initial child process | ||
startChildProcess(); | ||
|
||
// Create a readline interface to listen for commands from the console | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
|
||
// Listen for input from the console | ||
rl.on('line', (input) => { | ||
if (input.trim() === 'restart') { | ||
// Kill the current child process | ||
child.kill(); | ||
|
||
// Start a new child process | ||
startChildProcess(); | ||
} | ||
}); | ||
|
||
// Handle the SIGINT signal to properly exit the program | ||
rl.on('SIGINT', () => { | ||
// Kill the child process if it exists | ||
if (child) { | ||
child.kill(); | ||
} | ||
|
||
// Close the readline interface | ||
rl.close(); | ||
|
||
// Exit the program | ||
process.exit(); | ||
}); |