Created
July 2, 2016 17:06
-
-
Save ThePengwin/964906e1f34b340f652893c1751d12db to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* FreshBot | |
* | |
* Needs Discord.js | |
* npm install discord.js | |
* | |
* uses a config file called config.json in the same folder for auth | |
* Example config.json | |
{ | |
"auth": { | |
"email": "[email protected]", | |
"password": "password" | |
} | |
} | |
* | |
* | |
**/ | |
var Discord = require("discord.js"); | |
var bot = new Discord.Client(); | |
//load settings | |
bot.loadSettings = function() { | |
var self = this; | |
self.log('Loading Settings'); | |
try { | |
bot.settings = require('./config.json'); | |
} catch (e) { | |
self.log('Failed to load settings'); | |
} | |
}; | |
bot.saveSettings = function() { | |
var fs = require('fs'); | |
var self = this; | |
fs.writeFile('./config.json', JSON.stringify(bot.settings, null, 2), function(err) { | |
if(err) { | |
self.log(err); | |
} else { | |
self.log("config.json saved"); | |
} | |
}); | |
}; | |
bot.log = function(message){ | |
var now = new Date(); | |
var timestamp = '[ '+now.toLocaleTimeString('en-US', { hour12: false })+' ]'; | |
console.log(timestamp,message); | |
}; | |
bot.init = function() { | |
var self = this; | |
self.loadSettings(); | |
self.on("ready",function(){ | |
self.log('Bot ready to serve'); | |
self.on('message',function(m){ | |
//only react when mentioned. | |
if(!m.isMentioned(bot.user)) return; | |
var removeName = new RegExp('^\<\@'+bot.user.id+'\>','i'); | |
var command = m.content.replace(removeName,'').trim(); | |
if (command == 'start') { | |
if (m.author.voiceChannel) { | |
bot.joinVoiceChannel(m.author.voiceChannel).then(function(){ | |
var connection = bot.internal.voiceConnection; | |
var stream = 'http://livestream.freshfm.com.au:8004/;stream.mp3'; | |
connection.playFile(stream); | |
}).catch(function(e){ | |
console.log(e); | |
}); | |
} | |
} | |
if (command == 'stop') { | |
bot.leaveVoiceChannel(m.author.voiceChannel); | |
} | |
if (command.match(/volume 1?[0-9]?[0-9]/)) { | |
var volume = command.split(' ').pop(); | |
volume = parseInt(volume); | |
volume = volume / 100; | |
if (bot.internal.voiceConnection) { | |
var connection = bot.internal.voiceConnection; | |
connection.setVolume(volume); | |
} | |
} | |
}); | |
}); | |
self.log('Starting Discord Bot...'); | |
self.login(self.settings.auth.email, self.settings.auth.password); | |
}; | |
process.on("SIGINT", function () { | |
console.log(''); | |
bot.log('SIGINT Recieved'); | |
bot.saveSettings(); | |
bot.logout(function(){ | |
bot.log('Bot Logged out'); | |
process.exit(); | |
}); | |
}); | |
bot.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment