-
Notifications
You must be signed in to change notification settings - Fork 0
/
botCommandHandler.ts
136 lines (105 loc) · 4.42 KB
/
botCommandHandler.ts
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
import { ChatClient, ChatUser } from "@twurple/chat";
import { addChannel, getFollowages, getSOChannel, log, removeChannel, saveSoChannelSettings } from "./utils";
import { newChannel } from "./sohandler";
let serviceCommands: Array<ServiceCommand> = [];
export function init() {
serviceCommands = [{
command: "!join",
handler: joinChannel
}, {
command: "!leave",
handler: leaveChannel
}, {
command: "!msg",
handler: fetchSOMsg
}, {
command: "!setmsg",
handler: setSOMsg
}, {
command: "!followage",
handler: getFollowage
}
]
}
export async function handleMessage(user: string, message: string, channel: string, chatClient: ChatClient) {
if (!message.startsWith("!")) return; // message is not a command
// for adding and editing commands
serviceCommands.forEach(svcCommand => {
if (message.toLowerCase().startsWith(svcCommand.command)) {
log("handling bot command " + svcCommand.command);
svcCommand.handler(user, message, channel, chatClient);
}
})
if(message.startsWith("!link")) {
chatClient.say(channel, "https://bot-ng-bayan-web.herokuapp.com");
}
if(message.startsWith("!kofi")) {
chatClient.say(channel, "https://ko-fi.com/speeeedtv");
}
}
async function joinChannel(user: string, message: string, channel: string, chatClient: ChatClient): Promise<void> {
//check user
log("checking user");
chatClient.say(channel, "Check out my client version @ https://speeeedtv.itch.io/bot-ng-bayan. Details are in the discord https://discord.com/invite/NJNAGqcCDB.")
// let isAdded = await addChannel(user);
// console.log(isAdded);
// //join channel
// chatClient.join(user).catch((reason: any) => {
// chatClient.say(channel, "Bot failed to join " + user +"'s chat.[" + reason + "]");
// }).finally(() => {
// chatClient.say(channel, "Bot joined " + user + "'s chat. Kindly give it a bit of time to boot up. Check on your next stream.");
// });
// await newChannel(user);
}
async function leaveChannel(user: string, message: string, channel: string, chatClient: ChatClient): Promise<void> {
//check user
log("checking user");
let isExists = await getSOChannel(user);
if(isExists.status === undefined && isExists.enabled){
//add user
log("channel found, removing channel")
let isRemoved = await removeChannel(user);
//leave channel
console.log(isRemoved);
chatClient.part(user);
chatClient.say(channel, "Bot left " + user + "'s chat. Thank you for trying it out.");
}
}
async function fetchSOMsg(user: string, message: string, channel: string, chatClient: ChatClient): Promise<void> {
//check user
log("checking user");
let isExists = await getSOChannel(user);
if(isExists.enabled){
console.log(isExists);
//add user
// log("channel found, removing channel")
// let isRemoved = await removeChannel(user);
// //leave channel
// console.log(isRemoved);
// chatClient.part(user);
log("Hi there " + user + "." + " Your current SO message is set as:" + isExists.soMessageTemplate);
chatClient.say(channel, "Hi there " + user + "." + " Your current SO message is set as:" + isExists.soMessageTemplate);
}
}
async function setSOMsg(user: string, message: string, channel: string, chatClient: ChatClient): Promise<void> {
//check user
log("checking user");
let isExists = await getSOChannel(user);
if(isExists.enabled){
console.log(isExists);
let soMsg = message.replace("!setmsg ","");
let updated = await saveSoChannelSettings(user, { "soMessageTemplate" : soMsg});
console.log(updated);
chatClient.say(channel, "Hi there " + user + "." + " Your current SO message is set as:" + updated.soMessageTemplate);
}
}
async function getFollowage(user: string, messsage: string, channel: string, chatClient: ChatClient): Promise<void> {
log("getting followage");
let followage = await getFollowages(user);
console.log(channel + ":" + followage);
chatClient.say(channel, "" + followage);
}
interface ServiceCommand {
command: string;
handler: (user: string, messsage: string, channel: string, chatClient: ChatClient) => Promise<void>;
}