-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
87 lines (74 loc) · 2.63 KB
/
Copy pathparser.js
File metadata and controls
87 lines (74 loc) · 2.63 KB
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
import { Prefix, Streamer } from './config.js';
import { Database } from './database.js';
import { channel } from './main.js';
export let actions = [];
export let announcements = [];
export const programmables = [];
export const log = (component, text) => console.log(
`[${(new Date()).toLocaleTimeString('en-GB', {
timeZone: 'UTC', hour12: false, hour: 'numeric', minute: 'numeric'
})} UTC] ${component}: ${text}`
);
const hearts = { '🧡': "orange", '💚': "green", '💙': "blue", '💜': "purple" };
const handler = message => {
if (message === undefined) return;
for (const heart in hearts) if (message.startsWith(heart)) {
channel.commands.announce(message.substring(1).trim(), hearts[heart]);
return;
}
channel.send(message);
};
// ==== Twitch Actions =========================================================
const RRSLV = new RegExp(`${Prefix}[A-Za-z0-9_\\.]+`, 'i');
function allowed(tags, permissions) {
if (tags.broadcaster || tags.mod) return true;
switch (permissions) {
case 'mod': return false;
case 'sub': return tags.sub;
case 'vip': return tags.vip;
case 'all': default: return true;
}
}
export function resolve(data, channel) {
if (!data.message.includes(Prefix)) return;
let command = RRSLV.exec(data.message);
if (command === null) return;
if (/\b(command|use|type)\b/i.test(data.message)) return;
command = command[0].trim().replace(Prefix, '').toLowerCase();
for (const action of actions) {
if (!action.commands.includes(command)) continue;
if (!allowed(data.tags, action.permissions)) return;
if (action.reply !== undefined) handler(action.reply);
return;
}
for (const action of programmables) {
if (!action.commands.includes(command)) continue;
if (!allowed(data.tags, action.permissions)) return;
if (action.execute.constructor.name === 'AsyncFunction')
action.execute(data, channel).then(handler);
else handler(action.execute(data, channel));
return;
}
}
export async function reloadActions() {
const A = await Database.get('actions');
if (A === undefined || A === null) return [];
return A;
}
export async function reloadAnnouncements() {
const A = await Database.get('announcements');
if (A === undefined || A === null) return [];
return A;
}
export function programmable(command) {
if (typeof command.execute !== 'function') return;
if (command.commands === undefined) return;
if (typeof command.commands === 'string')
command.commands = command.commands.split(/\s+/g);
if (command.permissions === undefined) command.permissions = 'all';
programmables.push(command);
}
export async function refresh() {
actions = await reloadActions();
announcements = await reloadAnnouncements();
}