From b47831b5e15d8681326eb0da96e6404aaa609962 Mon Sep 17 00:00:00 2001 From: Maxwell Gorley <38299798+gizmo4487@users.noreply.github.com> Date: Sun, 15 Mar 2020 13:17:28 -0400 Subject: [PATCH] Added o!voice and removed o!ping --- README.md | 2 +- o-bot.js | 69 ++++++++++++++++++++++++++++++++++++++++------------ package.json | 3 ++- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 58b9229..03dd127 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ A Discord bot themed around O, a character in SEGA's Puyo Puyo Tetris Run ``npm install`` in the bot folder to install dependencies. ## Commands * ``o!help``: Display help message -* ``o!ping``: Test if bot is alive * ``o!pi``: Make O speak +* ``o!voice``: O-Bot will join a voice channel and play one of O's voice lines, then leave *More commands to follow* diff --git a/o-bot.js b/o-bot.js index e976b95..d4948d4 100644 --- a/o-bot.js +++ b/o-bot.js @@ -1,5 +1,6 @@ const Discord = require('discord.js'); const auth = require('./auth.json'); +var sound; // Create bot object const bot = new Discord.Client({autoReconnect:true}); @@ -8,8 +9,9 @@ const bot = new Discord.Client({autoReconnect:true}); bot.once('ready', () => { console.log('Connected'); }); +//bot.on('debug', console.log); bot.on('message', message => { - // Prefix is '!' + // Prefix is 'o!' if (message.content.substring(0, 2) == 'o!') { var args = message.content.substring(2).split(' '); var cmd = args[0]; @@ -18,23 +20,60 @@ bot.on('message', message => { // Commands switch(cmd) { - // o!ping - case 'ping': - message.channel.send('Pong!'); - break; - // o!help - case 'help': - message.channel.send('Commands:\no!help: Display this message\no!ping: Test if bot is alive\no!pi: Make O speak'); - break; - - // o!pi - case 'pi': - message.channel.send('Pipi? Pipi-pipipipipi!'); - break; + // 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'); + break; + + // o!pi + case 'pi': + message.channel.send('Pipi? Pipi-pipipipipi!'); + break; + + // o!voice + case 'voice': + play(message, "o_sound/ready.wav"); + break; + + // End of command list - } + } } }); +async function play(message, sound){ + try{ + 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("You are not in a voice channel!"); + } + + 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!"); + const dispatcher = connection.play(sound); + dispatcher.on("finish", () => { + message.channel.send("Should be disconnecting now"); + dispatcher.destroy(); + VC.leave(); + }) + .on('error', error => { + console.error(error); + }); + } + + } catch(error){ + console.log(error); + } + +} bot.login(auth.token); \ No newline at end of file diff --git a/package.json b/package.json index 0047b87..b9ecd56 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,11 @@ { "name": "O-Bot", - "version": "0.0.1", + "version": "0.0.2", "description": "Pipipi-pipipipi!", "main": "o-bot.js", "author": "gizmo4487", "dependencies": { + "@discordjs/opus": "^0.1.0", "bufferutil": "^4.0.1", "discord.js": "^12.0.2", "erlpack": "github:discordapp/erlpack",