-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
175 lines (148 loc) · 6.36 KB
/
app.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require('dotenv').config();
const { RefreshingAuthProvider } = require('@twurple/auth');
const { ApiClient } = require('@twurple/api');
const { ChatClient } = require('@twurple/chat');
const { EventSubWsListener } = require('@twurple/eventsub-ws');
const smakapi = require('./api/smakapi.js');
const whisperChat = require('./api/whisperchat.js');
const command = require('./commands');
const Autochat = require('./constants/autochat.js');
const http = require('./constants/http.js');
let { greetedUsers, raiders } = require('./constants/users.js');
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
global.messageFailed = false;
const main = async () => {
const userId = process.env.USER_ID;
const channels = {};
let emoteOnlyMode = false;
let followerOnlyMode = false;
let subscriberOnlyMode = false;
const authProvider = new RefreshingAuthProvider(
{
clientId,
clientSecret,
onRefresh: async (userId, newTokenData) => await smakapi('/token', http.POST, newTokenData)
}
);
const initialToken = await smakapi('/token', http.GET);
if (initialToken.startsWith('{')) {
authProvider.addUser(userId, JSON.parse(initialToken), ['chat']);
} else {
//TODO: handle error
console.log('Tokon error');
process.exit();
}
const apiClient = new ApiClient({ authProvider });
const listener = new EventSubWsListener({ apiClient });
listener.start();
listener.onChannelRaidTo(userId, e => {
raiders.push(e.raidingBroadcasterDisplayName);
});
listener.onStreamOffline(userId, e => {
greetedUsers = {};
});
const client = new ChatClient({ authProvider, channels: [ process.env.CHANNEL_NAME ] });
client.connect();
whisperChat(authProvider, client, apiClient);
client.onAuthenticationSuccess(() => {
channels['#smaktalk94'] = true;
console.log('Authentication successful!');
console.log('------------');
});
client.onAuthenticationFailure((text, retryCount) => {
console.log(text);
console.log(retryCount);
console.log('------------');
});
client.onEmoteOnly((channel, enabled) => {
channels[channel] = !enabled;
});
client.onFollowersOnly((channel, enabled, delay) => {
channels[channel] = !enabled;
});
client.onJoin(async (channel, user) => {
if (channel !== '#smaktalk94') {
await checkChannelPermissions(channel);
global.messageFailed = !channels[channel];
const joinedChannel = channel.replace('#', '');
command.auto(Autochat.JOINED, client, '#smaktalk94', joinedChannel);
}
});
client.onJoinFailure((channel, reason) => {
console.log(`Failed to join ${channel} for following reason:`);
console.log(reason);
console.log('------------');
});
client.onMessage(async (channel, user, text, msg) => {
const context = msg.userInfo;
const commandName = text.trim();
if (context.displayName === 'TheSmakBot') { return; }
if (commandName.startsWith('$')) {
if (channels[channel]) {
switch (commandName.split(' ')[0]) {
case '$channel':
command.channels(client, channel, commandName, context);
break;
case '$drawing':
case '$enter':
case '$exit':
command.entries(client, channel, commandName, context);
break;
case '$raid':
command.raids(commandName);
break;
case '$streamer':
command.streamers(commandName);
break;
default:
command.general(client, channel, commandName, context);
}
} else {
const helixUser = await apiClient.users.getUserByName(user);
const mode = emoteOnlyMode ? 'emote-only' : followerOnlyMode ? 'follower-only' : 'subscriber-only';
apiClient.whispers.sendWhisper(userId, helixUser.id, `${channel.substring(1)} has enabled ${mode} mode in their chat`);
}
}
if (channel === '#smaktalk94' && context.displayName !== 'StreamElements' && context.displayName !== 'Sery_Bot' && context.displayName !== process.env.CHANNEL_NAME) {
if (raiders.includes(context.displayName)) {
command.auto(Autochat.RAIDER, client, channel, context.displayName);
} else if (context.isMod && greetedUsers[context.displayName] !== 1) {
command.auto(Autochat.FIRST_MOD, client, channel, context.displayName);
} else if (greetedUsers[context.displayName] !== 1) {
command.auto(Autochat.FIRST, client, channel, context.displayName);
}
}
});
client.onMessageFailed((channel, reason) => {
global.messageFailed = true;
console.log(`Failed to message in ${channel.substring(1)}'s chat for following reason:`);
console.log(reason);
console.log('------------');
});
client.onPart((channel, user) => {
const partedChannel = channel.replace('#', '');
command.auto(Autochat.PARTED, client, '#smaktalk94', partedChannel);
});
client.onRitual((channel, user, ritualInfo, msg) => {
if (channel === '#smaktalk94' && ritualInfo.ritualName === 'new_chatter') {
command.auto(Autochat.NEW, client, channel, context.displayName);
}
});
client.onSubsOnly((channel, enabled) => {
channels[channel] = !enabled;
});
process.on('uncaughtException', error => {
console.log(error);
console.log('------------');
});
const checkChannelPermissions = async (channel) => {
const channelUser = await apiClient.users.getUserByName(channel.substring(1));
const channelSettings = await apiClient.chat.getSettings(channelUser.id);
emoteOnlyMode = channelSettings.emoteOnlyModeEnabled;
followerOnlyMode = channelSettings.followerOnlyModeEnabled;
subscriberOnlyMode = channelSettings.subscriberOnlyModeEnabled;
channels[channel] = !emoteOnlyMode && !followerOnlyMode && !subscriberOnlyMode;
};
};
main();