aboutsummaryrefslogtreecommitdiff
path: root/twitch-bot/bot.js
blob: cdc978049e0938bf6632dcb765689613de0e4b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const tmi = require('tmi.js');
const timed = require('./timedMessages.js');
const fs = require('fs');

// read settings and reactions json files
fs.readFile('./settings.json', 'utf8', (err, data) => {
  if (err) {
    console.log(`Error reading settings file from disk: ${err}`);
    process.exit(1);
	} else {
		// parse JSON string to JSON object
		const settings = JSON.parse(data);
	}
});

fs.readFile('./reacts.json', 'utf8', (err, data) => {
  if (err) {
    console.log(`Error reading reacts file from disk: ${err}`);
    process.exit(1);
	} else {
		// parse JSON string to JSON object
		const reacts = JSON.parse(data);
	}
});

fs.readFile('./commands.json', 'utf8', (err, data) => {
  if (err) {
    console.log(`Error reading commands file from disk: ${err}`);
    process.exit(1);
	} else {
		// parse JSON string to JSON object
		const commands = JSON.parse(data);
	}
});

fs.readFile('./mod_commands.json', 'utf8', (err, data) => {
  if (err) {
    console.log(`Error reading mod commands file from disk: ${err}`);
    process.exit(1);
	} else {
		// parse JSON string to JSON object
		const mod_commands = JSON.parse(data);
	}
});

const opts = {
  identity: {
    username: settings.bot_username,
    password: settings.bot_token
  },
  channels: settings.channels
};

var modString = "";
var mods;

try {
  modString = fs.readFileSync('', 'utf8');
  modString = modString.replace(/(\r\n|\n|\r)/gm, "");
  mods = modString.split(",");
} catch (err) {
  console.error(err)
}

var modCommand = false;

// Twitch bot initialization
const client = new tmi.client(opts);

// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);

// Connect to Twitch:
client.connect();

// Notify when connected
function onConnectedHandler (addr, port) {
  console.log(`* Main Bot successfully connected to ${addr}:${port}`);
}

function onMessageHandler (target, context, msg, self) {
  if (self) { return; } // Ignore messages from the bot
  const user = context.username;
  msg = msg.toLowerCase();
  var message = msg.trim();

  if (message.charAt(0) === settings.command_char) {
    message = message.substr(1);
    valid = commands(target, message, user, mods);
  } else {
    reactions(target, message, user);
  }
}

function commands (target, commandName, user, mods) {
  modCommand = false;
  var valid = false;
  var isMod = false;
  if (mods.indexOf(user) >= 0) isMod = true;
  
  if (commandName === "help") {
    var allCommands = [];
    var finalString = "";
    commands.forEach(command => {
      allCommands.push(command.command);
    });
    allCommands.forEach(command => {
      finalString = finalString + command.command;
    });
    client.say(target, `Here is the list of commands: ${finalString}`);
  } else {
    commands.forEach(command => {
      if (message.search(command.command) !== -1) {
        client.say(target, `${command.reply}`);
        valid = true;
        break;
      }
    });
  }

  if (valid === false) {
    if (isMod) {
      valid = modCommands(target, commandName, isMod);
    }
  }

  if (valid) {
    console.log(`* ${user} executed !${commandName}`);
  } else {
    if (modCommand === false) {
      client.say(target, `@${user} Unknown command "${settings.command_char}${commandName}". Type ${settings.command_char}help for all commands.`);
    }
    console.warn(`! ${user} tried to execute unknown/banned command !${commandName}`);
  }
}

function modCommands(target, commandName, isMod) {
  var valid = false;
  mod_commands.forEach(command => {
    if (message.search(command.command) !== -1) {
      if (!isMod) {
        client.say(target, `Only moderators can run this command...`);
      } else {
        client.say(target, `${command.reply}`);
        valid = true;
        break;
      }
    }
  });
  return valid;
}

function reactions (target, message, user) {
  reacts.forEach(react => {
    if (message.search(react.trigger) !== -1) {
      client.say(target, `${react.reply}`);
    }
  });
}