mirror of
https://github.com/gizmo4487/O-Bot.git
synced 2025-05-30 20:22:31 -05:00
Major update for O-Bot
Added new commands and updated version number in package.json to 0.1.0
This commit is contained in:
parent
33323182b4
commit
51a71a1274
209
o-bot.js
209
o-bot.js
@ -1,6 +1,10 @@
|
|||||||
const Discord = require('discord.js');
|
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');
|
const auth = require('./auth.json');
|
||||||
var sound;
|
var sound;
|
||||||
|
const {exec} = require("child_process");
|
||||||
|
|
||||||
// Create bot object
|
// Create bot object
|
||||||
const bot = new Discord.Client({autoReconnect:true});
|
const bot = new Discord.Client({autoReconnect:true});
|
||||||
@ -8,7 +12,20 @@ const bot = new Discord.Client({autoReconnect:true});
|
|||||||
// Display console message when logged in
|
// Display console message when logged in
|
||||||
bot.once('ready', () => {
|
bot.once('ready', () => {
|
||||||
console.log('Connected');
|
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('debug', console.log);
|
||||||
bot.on('message', message => {
|
bot.on('message', message => {
|
||||||
// Prefix is 'o!'
|
// Prefix is 'o!'
|
||||||
@ -23,7 +40,7 @@ bot.on('message', message => {
|
|||||||
|
|
||||||
// o!help
|
// o!help
|
||||||
case '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;
|
break;
|
||||||
|
|
||||||
// o!pi
|
// o!pi
|
||||||
@ -33,7 +50,63 @@ bot.on('message', message => {
|
|||||||
|
|
||||||
// o!voice
|
// o!voice
|
||||||
case '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;
|
break;
|
||||||
|
|
||||||
|
|
||||||
@ -50,25 +123,28 @@ async function play(message, sound){
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if(!VC){
|
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')) {
|
else if (!VC.permissionsFor(message.client.user).has('CONNECT') || !VC.permissionsFor(message.client.user).has('SPEAK')) {
|
||||||
message.channel.send('Missing join/speak permissions!');
|
message.channel.send('Missing join/speak permissions!');
|
||||||
}
|
}
|
||||||
|
|
||||||
else{
|
else{
|
||||||
// Here we try to join the voicechat and save our connection into our object.
|
// Here we try to join the voicechat and save our connection into our object.
|
||||||
var connection = await VC.join();
|
var connection = await VC.join();
|
||||||
message.channel.send("Playing!");
|
//message.channel.send("Playing!");
|
||||||
|
console
|
||||||
const dispatcher = connection.play(sound);
|
const dispatcher = connection.play(sound);
|
||||||
dispatcher.on("finish", () => {
|
dispatcher.on("finish", () => {
|
||||||
message.channel.send("Should be disconnecting now");
|
//message.channel.send("Disconnecting!");
|
||||||
dispatcher.destroy();
|
dispatcher.destroy();
|
||||||
VC.leave();
|
//VC.leave();
|
||||||
})
|
})
|
||||||
.on('error', error => {
|
.on('error', error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch(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);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "O-Bot",
|
"name": "O-Bot",
|
||||||
"version": "0.0.2",
|
"version": "0.1.0",
|
||||||
"description": "Pipipi-pipipipi!",
|
"description": "Pipipi-pipipipi!",
|
||||||
"main": "o-bot.js",
|
"main": "o-bot.js",
|
||||||
"author": "gizmo4487",
|
"author": "gizmo4487",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user