-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.js
154 lines (137 loc) · 5.88 KB
/
bot.js
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
const dotenv = require('dotenv');
const tmi = require('tmi.js');
const GphApiClient = require('giphy-js-sdk-core')
dotenv.config();
const express = require('express');
const app = express();
const port = 3000;
let gifs = [];
let commandMode = false;
let edgeMode = false;
let streamWidth = process.env.STREAM_WIDTH;
let streamHeight = process.env.STREAM_HEIGHT;
let gifRating = process.env.GIF_RATING; // options: "y", "g", "pg", "pg-13", "r", "unrated", "nsfw", ""
let numGifsDisplayed = 10;
let eraseDelaySecs = 30;
let intervalId = setInterval(shiftGifs, eraseDelaySecs * 1000);
app.get('/', function (req, res) {
res.render('gifPage', {"gifs": gifs});
});
app.listen(port, function() {
console.log('Gif page displaying on port ' + port);
});
app.set('view engine', 'ejs');
// Define configuration options
const opts = {
identity: {
username: process.env.TWITCH_USERNAME,
password: process.env.TWITCH_OAUTH
},
channels: [
process.env.TWITCH_CHANNEL
]
};
// Create a client with our options
const twitchClient = new tmi.client(opts);
// Register our event handlers (defined below)
twitchClient.on('message', onMessageHandler);
twitchClient.on('connected', onConnectedHandler);
// Connect to Twitch:
twitchClient.connect();
function onMessageHandler (target, context, msg, self) {
if(self) {return;} // Ignore messages from the bot
// Remove whitespace from chat message
const command = msg.trim();
const channelName = process.env.TWITCH_CHANNEL.toLocaleLowerCase();
// Commands: !gif <gifname>, !source, !edgeMode, !gifRating <rating>, !numGifs <gifs>, !gifTimeout <secs>,
if (command.startsWith('!gif ')) {
findGif(command.substring(4).trim(), target, context["display-name"]);
console.log(`* Finding gif with command ${command} target: ${target} user: ${context.username}`);
} else if(command === ("!source")) {
twitchClient.say(target, `Source code for this bot can be found at https://github.com/ngittlen/TwitchAutoGifBot`);
} else if(command === "!commandMode" && context.username === channelName) {
commandMode = !commandMode;
console.log(`* Switching commandMode to ${commandMode}`);
} else if(command === "!edgeMode" && context.username === channelName) {
edgeMode = !edgeMode;
console.log(`* Switching edgeMode to ${edgeMode}`);
} else if(command.startsWith("!gifRating ") && context.username === channelName) {
gifRating = command.substring(10).trim();
console.log(`* Switching gifRating to ${gifRating}`);
} else if(command.startsWith("!numGifs ") && context.username === channelName) {
numGifsDisplayed = command.substring(8).trim();
console.log(`* Setting numGifsDisplayed to ${numGifsDisplayed}`);
} else if(command.startsWith("!gifTimeout ") && context.username === channelName) {
eraseDelaySecs = command.substring(11).trim();
clearInterval(intervalId);
intervalId = setInterval(shiftGifs, eraseDelaySecs * 1000);
console.log(`* Setting eraseDelaySecs to ${eraseDelaySecs}`);
} else if(!commandMode && !command.startsWith("!")) {
findGif(command, target, context["display-name"]);
console.log(`* Finding gif with command ${command} target: ${target} user: ${context.username} rating: ${gifRating}`);
}
}
function findGif (command, target, username) {
let giphyClient = GphApiClient(process.env.GIPHY_API_KEY);
const numGifs = 30;
giphyClient.search('gifs', {"q": command, "limit": numGifs, "rating": gifRating})
.then((response) => {
if(response.data === null || response.data.length < 1) {
twitchClient.say(target, `Cannot find gif for ${command}`);
console.log(`Cannot find gif for ${command}`);
return;
}
if(numGifs > response.data.length) {
numGifs = response.data.length;
}
let gifNumber = randomNum(numGifs);
// console.log("Used keywords " + command + " " + JSON.stringify(response.data, null, 4) + " number: " + gifNumber);
// twitchClient.say(target, response.data[gifNumber].images.original.url);
let width = parseInt(response.data[gifNumber].images.original.width);
let height = parseInt(response.data[gifNumber].images.original.height);
let top, left;
if(edgeMode) {
let side = randomNum(4);
switch(side) {
case 0:
top = 0;
left = randomNum(streamWidth - width);
break;
case 1:
top = randomNum(streamHeight - height);
left = streamWidth - width;
break;
case 2:
top = streamHeight - height;
left = randomNum(streamWidth - width);
break;
case 3:
top = randomNum(streamHeight - height);
left = 0;
}
} else {
top = randomNum(streamHeight - height);
left = randomNum(streamWidth - width);
}
gifs.push({"src":response.data[gifNumber].images.original.url, "top": top,
"left": left,"width": width, "height": height, "prompt": command, "username": username});
if(gifs.length > numGifsDisplayed) {
gifs.shift();
}
})
.catch((err) => {
console.error(err);
});
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler (addr, port) {
console.log(`* Connected to ${addr}:${port}`);
}
function randomNum(maxNum) {
return Math.floor(Math.random() * maxNum);
}
function shiftGifs() {
if(gifs.length > 0){
gifs.shift();
}
}