Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const tmi = require('tmi.js');
- const appConfig = require('../config');
- const songRequest = require('./songRequest');
- const rank = require('./rank');
- const gamble = require('./gamble');
- const command = require('./command');
- const channel = appConfig.twitchChannel;
- const config = {
- options: {
- debug: true //TODO: turn this off
- },
- connection: {
- cluster: 'aws',
- reconnect: true
- },
- identity: {
- username: appConfig.botOauth.username,
- password: appConfig.botOauth.oAuthKey
- },
- channels: [channel]
- };
- const client = null;
- const bot = {
- socketApi: null,
- connect: () => {
- client = new tmi.client(config);
- client.connect();
- init();
- }
- };
- function init() {
- rank.init();
- client.on("connected", function (address, port) {
- // client.say(channel, 'Bienvenidos al canal.');
- });
- client.on("chat", function (channel, userstate, message, self) {
- const isModerator = userstate.mod;
- const isBroadcaster = userstate.badges && userstate.badges.broadcaster;
- const isModeratorOrBroadcaster = isModerator || isBroadcaster;
- // Don't listen to my own messages..
- if (self) return;
- const user = userstate['display-name'];
- if (message.includes('!songrequest') || message.includes('!sr')) {
- const query = message.replace('!songrequest', '').replace('!sr', '').trim();
- if (query) {
- songRequest.addSong(user, query, function(songTitle) {
- client.say(channel, 'La canción ' + songTitle + ' ha sido agregada a la lista de reproducción por ' + user);
- });
- }
- }
- if (message.includes('!skip')) {
- songRequest.getCurrentSong(function(currentSong) {
- if (currentSong) {
- let skips = [];
- if (currentSong.skips) {
- skips = JSON.parse(currentSong.skips);
- }
- if (!skips.includes(user)) {
- skips.push(user);
- if (skips.length < songRequest.skipLimit) {
- songRequest.updateSong(currentSong, skips);
- } else {
- songRequest.deleteSong(currentSong._id, function() {
- bot.socketApi.sendMessage('!skip', true);
- });
- }
- client.say(channel, user + ' ha votado por saltar la canción actual. Votos: ' + skips.length + '/' + songRequest.skipLimit);
- }
- }
- });
- }
- if (message.includes('!volume') && isModeratorOrBroadcaster) {
- if (bot.socketApi) {
- const volume = message.replace('!volume', '').trim();
- if (volume.length && volume >= 0 && volume <= 100) {
- bot.socketApi.sendMessage('!volume', volume);
- client.say(channel, user + " ha cambiado el volumen de la música a " + volume);
- }
- }
- }
- if (message.includes('!stop') && isModeratorOrBroadcaster) {
- if (bot.socketApi) {
- bot.socketApi.sendMessage('!stop', true);
- client.say(channel, user + " ha detenido la canción actual." );
- }
- }
- if (message.includes('!play') && isModeratorOrBroadcaster) {
- if (bot.socketApi) {
- bot.socketApi.sendMessage('!play', true);
- }
- }
- if (message.includes('!pause') && isModeratorOrBroadcaster) {
- if (bot.socketApi) {
- bot.socketApi.sendMessage('!pause', true);
- client.say(channel, user + " ha pausado la canción actual." );
- }
- }
- if (message.includes('!currentsong')) {
- songRequest.getCurrentSong(function(currentSong) {
- if (currentSong) {
- client.say(channel, "La canción actual es " + currentSong.title + " y fue agregada por " + currentSong.userName);
- }
- });
- }
- if (message.includes('!points')) {
- rank.getPoints(user, function(points) {
- client.say(channel, user + ' tiene ' + points + ' puntos.');
- });
- }
- if (message.includes('!gamble')) {
- const points = message.replace('!gamble', '').trim();
- command.get('gamble', function (data) {
- if (data && data.enabled) {
- if (parseInt(points) > 0) {
- gamble.run(user, points, function(result, win, points, currentPoints, nextGamble) {
- if (nextGamble) {
- client.say(channel, user + ' no puedes apostar en este momento, debes esperar al menos '+ nextGamble +' minutos.');
- } else if (result === -1) {
- client.say(channel, user + ' no tiene los puntos suficientes para apostar.');
- } else {
- const outcome = win ? ' ganó ' : ' perdió ',
- msg = 'Cayó ' + result + '. ' + user + outcome + points + ' y ahora tiene ' + currentPoints + ' puntos.';
- client.say(channel, msg);
- }
- });
- }
- }
- });
- }
- });
- client.on("hosted", function (channel, username, viewers, autohost) {
- client.say(channel, "Gracias " + username + " por darnos host. Todos denle follow a " +
- username + " (" + channel + ").");
- });
- }
- module.exports = bot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement