Browse Source

Merge pull request #1 from calexil/tim

Add last.fm support
pull/2/head
JD 5 years ago committed by GitHub
parent
commit
23576c2702
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .gitignore
  2. 61
      lib/BansheeBot.js
  3. 17
      now-playing-bot.js
  4. 21
      package-lock.json
  5. 1
      package.json

3
.gitignore vendored

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
# JetBrains IDEs
/.idea
/node_modules

61
lib/BansheeBot.js

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
/**
* @author Timur Kuzhagaliyev <tim.kuzh@gmail.com>
* @copyright 2017
* @license MIT
*/
const Discord = require("discord.js");
const LastFmNode = require('lastfm').LastFmNode;
class BansheeBot {
/**
* @param {{discordToken: string, lastFmApiKey: string, lastFmSecret: string, lastFmUserAgent: string, lastFmUsername: string}} config
*/
constructor(config) {
this.config = config;
this.client = new Discord.Client();
this.lastfm = new LastFmNode({
api_key: this.config.lastFmApiKey,
secret: this.config.lastFmSecret,
useragent: this.config.lastFmUserAgent,
});
}
start() {
return Promise.resolve()
.then(() => console.log('Connecting to Discord...'))
.then(() => this.client.login(this.config.discordToken))
.then(() => console.log('Connected!'))
.then(() => console.log('Bootstrapping Last.fm...'))
.then(() => this.findMusicChannel())
.then(musicChannel => this.prepareLastFm(musicChannel))
.then(() => console.log('Done!'))
.catch(error => console.error(error));
}
findMusicChannel() {
return Promise.resolve()
.then(() => this.client.channels.find(channel => channel.name && channel.name.match(/^music/i)))
.then(channel => {
if (!channel) throw new Error('Could not find the music channel!');
return channel;
});
}
prepareLastFm(musicChannel) {
return Promise.resolve()
.then(() => this.lastfm.stream(this.config.lastFmUsername))
.then(lastFmStream => {
lastFmStream.on('nowPlaying', (track) => {
musicChannel.send('Now playing: ' + track.name);
});
})
}
}
module.exports = BansheeBot;

17
now-playing-bot.js

@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
/**
* @author Timur Kuzhagaliyev <tim.kuzh@gmail.com>
* @copyright 2017
* @license MIT
*/
const BansheeBot = require('./lib/BansheeBot');
let config = {
discordToken: process.env.BOT_TOKEN,
lastFmApiKey: process.env.LASTFM_API_KEY,
lastFmSecret: process.env.LASTFM_SECRET,
lastFmUserAgent: process.env.LASTFM_USER_AGENT,
lastFmUsername: process.env.LASTFM_USERNAME
};
let bot = new BansheeBot(config);
bot.start();

21
package-lock.json generated

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
{
"name": "BansheeBot",
"version": "0.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"lastfm": {
"version": "0.9.2",
"resolved": "https://registry.npmjs.org/lastfm/-/lastfm-0.9.2.tgz",
"integrity": "sha1-0Ayi47MOtITlEHkodVJZAOTXfYg=",
"requires": {
"underscore": "1.6.0"
}
},
"underscore": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz",
"integrity": "sha1-izixDKze9jM3uLJOT/htRa6lKag="
}
}
}

1
package.json

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
},
"dependencies": {
"discord.js": "11.1.0",
"lastfm": "^0.9.2",
"request": "2.81.0"
}
}

Loading…
Cancel
Save