Major update for O-Bot

Added new commands and updated version number in package.json to 0.1.0
This commit is contained in:
Max Gorley 2020-04-26 01:24:17 -04:00 committed by GitHub
parent 33323182b4
commit 51a71a1274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 203 additions and 8 deletions

209
o-bot.js
View File

@ -1,6 +1,10 @@
const Discord = require('discord.js');
const Constants = require('discord.js/src/util/Constants.js');
const fs = require('fs');
Constants.DefaultOptions.ws.properties.$browser='Discord Android';
const auth = require('./auth.json');
var sound;
const {exec} = require("child_process");
// Create bot object
const bot = new Discord.Client({autoReconnect:true});
@ -8,7 +12,20 @@ const bot = new Discord.Client({autoReconnect:true});
// Display console message when logged in
bot.once('ready', () => {
console.log('Connected');
bot.user.setActivity('Puyo Puyo\u2122Tetris\u00AE',{type: 'PLAYING'});
});
// Reconnecting
bot.on("reconnecting", () => {
console.log("[" + getTime() + "] Reconnecting!");
});
// Resume
bot.on("resume", () => {
console.log("[" + getTime() + "] Connection restored!");
bot.user.setActivity('Puyo Puyo\u2122Tetris\u00AE',{type: 'PLAYING'});
});
//bot.on('debug', console.log);
bot.on('message', message => {
// Prefix is 'o!'
@ -23,7 +40,7 @@ bot.on('message', message => {
// o!help
case 'help':
message.channel.send('Commands:\no!help: Display this message\no!pi: Make O speak\no!voice: Make O speak for real');
message.channel.send("Commands:\n\n**General**\n``o!help``: Display this message\n``o!pi``: Make O speak\n\n**Voice**\n``o!voice``: Make O speak for real\n``o!altvoice``: Make O speak in his alternate voice\n``o!jvoice``: Play one of O's Japanese voice lines\n``o!jaltvoice``: Play one of O's alternate Japanese voice lines\n``o!listen``: O-Bot will record your voice and send it back to you! (These recordings are NOT stored locally.)\n``o!dc``: Make O-Bot leave the voice channel\n\n**Other**\n``o!twitch``: Get link for O-Bot creator's Twitch channel\n``o!donate``: Feed O-Bot");
break;
// o!pi
@ -33,7 +50,63 @@ bot.on('message', message => {
// o!voice
case 'voice':
play(message, "o_sound/ready.wav");
play(message, "o_sound/000000" + randomHex() + ".wav");
break;
// o!altvoice
case 'altvoice':
play(message, "o_sound/alt/000000" + randomHex() + ".wav");
break;
// o!jvoice
case 'jvoice':
play(message, "o_sound/j/000000" + randomHex() + ".wav");
break;
// o!jaltvoice
case 'jaltvoice':
play(message, "o_sound/j_alt/000000" + randomHex() + ".wav");
break;
// o!twitch
case 'twitch':
message.channel.send('Pipi!\nhttps://twitch.tv/notengonombreusario');
break;
// o!donate
case 'donate':
message.channel.send('Pipipiii!!\nhttps://streamlabs.com/notengonombreusario');
break;
// o!dc
case 'dc':
if(!message.member.voice.channel){
message.channel.send("``o!dc`` can only be run while you are in a voice channel!");
}
else{
message.channel.send("Disconnecting!");
message.member.voice.channel.leave();
}
break;
// o!randomhex
case 'randomhex':
message.channel.send("Your random hexadecimal number is " + randomHex());
break;
// o!time
case 'time':
message.channel.send("The current time is " + getTime());
break;
// o!listen
case 'listen':
listen(message);
break;
// Invalid command
default:
message.channel.send("Pipi...\nInvalid command! Use ``o!help`` to list available commands!");
break;
@ -50,25 +123,28 @@ async function play(message, sound){
}
else if(!VC){
message.channel.send("You are not in a voice channel!");
message.channel.send("Join a voice channel and try again!");
}
else if (!VC.permissionsFor(message.client.user).has('CONNECT') || !VC.permissionsFor(message.client.user).has('SPEAK')) {
message.channel.send('Missing join/speak permissions!');
}
else{
// Here we try to join the voicechat and save our connection into our object.
var connection = await VC.join();
message.channel.send("Playing!");
//message.channel.send("Playing!");
console
const dispatcher = connection.play(sound);
dispatcher.on("finish", () => {
message.channel.send("Should be disconnecting now");
//message.channel.send("Disconnecting!");
dispatcher.destroy();
VC.leave();
//VC.leave();
})
.on('error', error => {
console.error(error);
});
}
} catch(error){
@ -76,4 +152,123 @@ async function play(message, sound){
}
}
bot.login(auth.token);
function randomHex(){
let hex="";
let randNum = 0;
randNum = (Math.floor(Math.random()*Math.floor(41)));
if(randNum<16){
hex = "0" + randNum.toString(16);
}
else{
hex = randNum.toString(16);
}
return hex;
}
function getTime(){
var date = new Date();
var hr = date.getHours();
var AMPM = hr>=12?"PM":"AM";
hr=((hr+11)%12+1);
var min = date.getMinutes();
if(min < 10){
min = "0" + min;
}
var sec = date.getSeconds();
if(sec < 10){
sec = "0" + sec;
}
var time = hr + ":" + min + ":" + sec + " " + AMPM;
return time;
}
async function listen(message){
try{
var date = new Date();
const VC = message.member.voice.channel;
if(!message.guild){
message.channel.send("The bot cannot join private voice chats.");
}
else if(!VC){
message.channel.send("Join a voice channel and try again!");
}
else if (!VC.permissionsFor(message.client.user).has('CONNECT') || !VC.permissionsFor(message.client.user).has('SPEAK')) {
message.channel.send('Missing join/speak permissions!');
}
else{
// Play silence, then start listening
var connection = await VC.join();
var user = message.member.user;
play(message, "ready.wav");
setTimeout(async () => {
message.channel.send("Speak now!");
const audio = connection.receiver.createStream(user,{mode: 'pcm'});
var filename = message.member.user.username + "_" + date.getTime();
audio.pipe(fs.createWriteStream(filename));
var cmd = "sox -t raw -r 48000 -e signed -b 16 -c 2 " + filename + " " + filename + ".wav";
var delcmd = "rm " + filename + ".wav" + " && rm " + filename;
var wavfile = filename + ".wav";
audio.on('end', async (filename) => {
message.channel.send("Finished!");
audio.destroy();
exec(cmd, (error, stdout, stderr) => {
if(error){
console.log(error.message);
return;
}
if(stderr){
console.log(stderr);
return;
}
console.log(stdout);
});
setTimeout(() =>{
message.channel.send({
files: [wavfile]
});
},500);
setTimeout(() => {
exec(delcmd, (error, stdout, stderr) => {
if(error){
console.log(error.message);
return;
}
if(stderr){
console.log(stderr);
return;
}
console.log(stdout);
})
},1000);
})
.on('error', error => {
console.error(error);
});
}, 200);
}
}
catch(error){
console.log(error);
}
}
bot.login(auth.token);

View File

@ -1,6 +1,6 @@
{
"name": "O-Bot",
"version": "0.0.2",
"version": "0.1.0",
"description": "Pipipi-pipipipi!",
"main": "o-bot.js",
"author": "gizmo4487",