Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*jshint esversion: 6 */
- /* jshint node: true */
- const webSocket = require('ws');
- const tmi = require('tmi.js');
- const fs = require('fs');
- const locallydb = require('locallydb');
- var db = new locallydb('./coinflipdb');
- var flipData = db.collection('currFlipsData');
- var options = {
- options: {
- debug: false
- },
- connection: {
- cluster: "aws",
- reconnect: true
- },
- identity: {
- username: '#################',
- password: 'oauth:##############'
- },
- channels: ['#onscreenlol', '#onscreenbot']
- };
- var client = new tmi.client(options);
- var inPlay = [];
- var joinInProg = false;
- var firstJoin = true;
- client.connect();
- console.log('Coinflip bot is online.');
- client.on("join", function (channel, username, self) {
- if (channel == "#onscreenlol" && firstJoin === true) {
- client.say('#onscreenlol', 'Coinflip bot is online. Use "!coinflip help" to see commands.');
- firstJoin = false;
- }
- });
- client.on("chat", function(channel, user, message, self) {
- var mesArray = message.split(" ");
- var twitchUser = user.username;
- var command = mesArray[1];
- var entryGold = 0;
- var joinedFlip = 0;
- var currentDate = (new Date()).toString();
- if (mesArray[0] == "!coinflip") {
- console.log(`===================================================`);
- console.log(`${twitchUser} sent a coinflip request: ${command}.`);
- if (user.subscriber === true) {
- if (command == "start" || command == "join") {
- deepbotFunc('get_points', twitchUser, null, function(goldString) {
- var gold = parseInt(goldString);
- console.log(`${twitchUser} has ${gold}.`);
- console.log(`Join in progress: ${joinInProg}.`);
- if (command == "start") {
- if (flipData.where({
- starterName: twitchUser
- }).items.length === 0) {
- console.log('Coinflip start requested.');
- entryGold = Math.floor(parseInt(mesArray[2]));
- if (isNaN(entryGold) === false) {
- team = mesArray[3].toLowerCase();
- if (entryGold < 50) {
- outputSend(channel, user, 'The minimum for a coinflip is 50 gold.');
- } else {
- if (gold < entryGold) {
- outputSend(channel, user, 'You do not have enough gold to start this coinflip.');
- } else {
- if (team == "ct" || team == "t") {
- deepbotFunc('add_to_escrow', twitchUser, entryGold, function(response) {
- newID(inPlay, function(flipID) {
- inPlay.push(flipID);
- console.log(inPlay);
- flipData.insert([{
- starterName: twitchUser,
- flipGold: entryGold,
- starterTeam: team,
- joinID: flipID
- }]);
- client.say('#onscreenlol', `Coinflip with ID ${flipID} has begun with ${twitchUser} for ${entryGold} gold on side ${team.toUpperCase()} - join with "!coinflip join ${flipID}".`);
- console.log(`Coinflip with ID ${flipID} has begun with ${twitchUser} for ${entryGold} gold on side ${team.toUpperCase()} - join with "!coinflip join ${flipID}".`);
- });
- });
- } else {
- outputSend(channel, user, 'Team must be T or CT.');
- }
- }
- }
- } else {
- console.log('Entry Gold must be an integer.');
- outputSend(channel, user, 'Entry Gold must be an integer.');
- }
- } else {
- outputSend(channel, user, 'You already have a coinflip in progress.');
- }
- } else if (command == "join" && joinInProg === false) {
- joinInProg = true;
- console.log('Coinflip join requested.');
- joinedFlipID = parseInt(mesArray[2]);
- joinedFlipData = flipData.where({
- joinID: joinedFlipID
- }).items[0];
- if (typeof joinedFlipData == 'object') {
- if (joinedFlipData.starterName != twitchUser) {
- entryGold = joinedFlipData.flipGold;
- if (joinedFlipData.starterTeam == 't') {
- team = 'ct';
- } else if (joinedFlipData.starterTeam == 'ct') {
- team = "t";
- }
- if (gold >= entryGold) {
- deepbotFunc('add_to_escrow', twitchUser, entryGold, function(response) {
- result = (Math.floor(Math.random() * 2) === 0) ? 't' : 'ct';
- winnerGold = Math.floor(entryGold * 2 * 0.95);
- console.log(result);
- opponent = joinedFlipData.starterName;
- setTimeout(function() {
- if (result == team) {
- client.say('#onscreenlol', `Coinflip with ID ${joinedFlipID} resulted in the coin landing on ${result.toUpperCase()}! It was won by ${twitchUser} defeating ${opponent}. The winner received ${winnerGold} gold.`);
- console.log(`Coinflip with ID ${joinedFlipID} resulted in the coin landing on ${result.toUpperCase()}! It was won by ${twitchUser} defeating ${opponent}. The winner received ${winnerGold} gold.`);
- deepbotFunc('commit_user_escrow', opponent, null, function(response) {
- deepbotFunc('commit_user_escrow', twitchUser, null, function(response) {
- deepbotFunc('add_points', twitchUser, winnerGold, function(response) {
- console.log('Transaction Complete.');
- flipData.remove(joinedFlipData.cid);
- inPlay.splice(inPlay.indexOf(joinedFlipID), 1);
- joinInProg = false;
- });
- });
- });
- } else if (result == joinedFlipData.starterTeam) {
- client.say('#onscreenlol', `Coinflip with ID ${joinedFlipID} resulted in the coin landing on ${result.toUpperCase()}! It was won by ${opponent}, defeating ${twitchUser}. The winner received ${winnerGold} gold.`);
- console.log(`Coinflip with ID ${joinedFlipID} resulted in the coin landing on ${result.toUpperCase()}! It was won by ${opponent}, defeating ${twitchUser}. The winner received ${winnerGold} gold.`);
- deepbotFunc('commit_user_escrow', twitchUser, null, function(response) {
- deepbotFunc('commit_user_escrow', opponent, null, function(response) {
- deepbotFunc('add_points', opponent, winnerGold, function(response) {
- console.log('Transaction Complete.');
- flipData.remove(joinedFlipData.cid);
- inPlay.splice(inPlay.indexOf(joinedFlipID), 1);
- joinInProg = false;
- });
- });
- });
- }
- }, 2000);
- });
- } else {
- outputSend(channel, user, 'You do not have enough gold to join this coinflip.');
- joinInProg = false;
- }
- } else {
- outputSend(channel, user, 'You cannot join your own coinflip.');
- }
- } else {
- outputSend(channel, user, 'This coinflip does not exist.');
- joinInProg = false;
- }
- }
- });
- } else if (command == "join" && joinInProg === true) {
- console.log('Join is currently in progress.');
- outputSend(channel, user, 'Join is currently in progress.');
- } else if (command == "info") {
- console.log('Coinflip info requested.');
- selectedFlip = parseInt(mesArray[2]);
- selectedFlipData = flipData.where({
- joinID: selectedFlip
- }).items[0];
- if (typeof selectedFlipData == 'object') {
- outputSend(channel, user, `Coinflip ${selectedFlip} was created by ${selectedFlipData.starterName}. Entry is ${selectedFlipData.flipGold} and their chosen side was ${selectedFlipData.starterTeam.toUpperCase()}.`);
- console.log(`Message sent to ${twitchUser}: Coinflip ${selectedFlip} was created by ${selectedFlipData.starterName}. Entry is ${selectedFlipData.flipGold} and their chosen side was ${selectedFlipData.starterTeam.toUpperCase()}.`);
- } else {
- outputSend(channel, user, `The selected flip does not exist.`);
- console.log('The selected flip does not exist.');
- }
- } else if (command == "help") {
- console.log('Coinflip help requested.');
- outputSend(channel, user, "Commands for coinflips: !coinflip start [gold] [t or ct] - starts a coinflip for a gold amount, with you on side T or CT; !coinflip join [flipID] - joins a coinflip with the matching id; !coinflip cancel [flipID] - allows some mods or the user who created the flip with that ID to cancel it; !coinflip info [flipID] - gives information on the selected flip; !coinflip list - list all in-progress coinflips.");
- } else if (command == "list") {
- console.log('Coinflip list requested.');
- flipNumber = flipData.items.length;
- userOutput = "Flips currently in progress: ";
- if (flipNumber > 0) {
- for (var i=0; i < flipNumber; i++) {
- userOutput += `${i+1}: Flip ID - ${flipData.items[i].joinID}, Starter - ${flipData.items[i].starterName}, Gold - ${flipData.items[i].flipGold}, Chosen Side - ${flipData.items[i].starterTeam.toUpperCase()}; `;
- }
- console.log(userOutput);
- outputSend(channel, user, `${userOutput.slice(0, -2)}.`);
- } else {
- outputSend(channel, user, 'There are no flips currently in progress.');
- console.log('There are no flips currently in progress.');
- }
- } else if (command == "cancel") {
- console.log('Coinflip cancel requested.');
- selectedFlip = parseInt(mesArray[2]);
- selectedFlipData = flipData.where({
- joinID: selectedFlip
- }).items[0];
- if (typeof selectedFlipData == 'object')
- if (twitchUser == selectedFlipData.starterName || twitchUser == "artemisbot" || twitchUser == "deelmo" || twitchUser == "ilkleytom") {
- deepbotFunc('cancel_escrow', twitchUser, null, function(response) {
- flipData.remove(selectedFlipData.cid);
- client.say('#onscreenlol', `Coinflip with ID ${selectedFlip} has been cancelled.`);
- inPlay.splice(inPlay.indexOf(selectedFlip), 1);
- console.log(`Coinflip with ID ${selectedFlip} has been cancelled.`);
- });
- } else {
- client.say('#onscreenlol', `You cannot delete another user's coinflip.`);
- console.log('User does not own coinflip.');
- }
- else {
- outputSend(channel, user, 'The selected coinflip does not exist.');
- console.log('Unrecognised coinflip.');
- }
- } else if (command == "shutdown" && (twitchUser == "artemisbot" || twitchUser == "deelmo" || twitchUser == "ilkleytom")) {
- client.say('#onscreenlol', 'Coinflip bot shutting down.....');
- console.log('Bot Shutting Down.');
- process.exit();
- } else {
- client.say('#onscreenlol', 'That command is unrecognised. Check !coinflip help.');
- console.log('Unrecognised command.');
- }
- } else {
- outputSend(channel, user, `Coinflips are a sub only system.`);
- console.log('Plebs cannot do coinflips.');
- }
- }
- flipData.save();
- });
- // ============================= Global Functions =======================================
- function outputSend(channel, user, msg) {
- //console.log("Message sending...");
- //console.log(user);
- if (user['user-type'] == 'mod' || user['user-id'] == user['room-id'] || channel == '#onscreenbot') {
- client.say(channel, user['display-name'] + ', ' + msg);
- } else {
- client.whisper(user['display-name'], msg);
- }
- }
- function newID(inPlay, cb) {
- for (var i = 1; true; i++) {
- if (inPlay.indexOf(i) < 0) {
- break;
- }
- }
- cb(i);
- }
- function deepbotFunc(scope, param1, param2, cb) {
- //console.log(cb);
- param1 = param1 || null;
- param2 = param2 || null;
- var ws = new webSocket('ws://############:3337/');
- ws.on('open', function() {
- //console.log('sending API registration');
- ws.send('api|register|##################');
- //console.log('registration sent');
- var noParam = ['get_song_count'];
- var singleParam = ['get_points', 'cancel_escrow', 'commit_user_escrow'];
- if (singleParam.indexOf(scope) >= 0) {
- ws.send(`api|${scope}|${param1}`);
- } else if (noParam.indexOf(scope) >= 0) {
- ws.send(`api|${scope}`);
- } else {
- //console.log('sent request');
- ws.send(`api|${scope}|${param1}|${param2}`);
- //console.log('sent request');
- }
- });
- ws.on('message', function(message) {
- //console.log('message received')
- //console.log(message)
- messageParse = JSON.parse(message);
- if (messageParse['function'] == 'register') {
- //console.log('auth successful')
- } else {
- ws.terminate();
- cb(messageParse.msg);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement