-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (158 loc) · 4.44 KB
/
index.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
176
177
178
179
180
181
182
183
184
185
186
let thelounge = null;
let USAGE = `
Usage: /prism [-rwmbkg] [-re] [message...]
-v, -version: Display plugin version
-r, -random: Randomize colors
-m, -me: Prepend /me to your message
-b, -background: Randomize background color as well
-k, -black: Black background
-n, -nocolor: disable coloration
-re, -reverse: Reverse string
-g, -gogolize: Gogolize string
-usa, -usa: 🇺🇸
> gogolize("Bonjour, mon nom est SakiiR")
'BoNjOuR, mOn nOm eSt sAkIiR'
`;
const { version: VERSION } = require("./package.json");
function parseOptions(args) {
const options = args
.filter((arg) => arg.startsWith("-"))
.map((o) => o.replace(/^\-/gi, ""));
const hasVersion = !!options.find((o) => o === "v" || o === "version");
const hasRandom = !!options.find((o) => o === "r" || o === "random");
const hasMe = !!options.find((o) => o === "m" || o === "me");
const hasReverse = !!options.find((o) => o === "re" || o === "reverse");
const hasChaoticBgColors = !!options.find(
(o) => o === "b" || o === "background"
);
const hasUsa = !!options.find((o) => o === "usa");
const hasBlackBg = !!options.find((o) => o === "k" || o === "black");
const hasGolgolize = !!options.find((o) => o === "g" || o === "gogolize");
const hasDisableColoration = !!options.find(
(o) => o === "n" || o === "nocolor"
);
return {
hasVersion,
hasRandom,
hasMe,
hasReverse,
hasChaoticBgColors,
hasUsa,
hasBlackBg,
hasGolgolize,
hasDisableColoration,
};
}
class Color {
static Black = 1;
static Red = 5;
static LightRed = 4;
static Brown = 7;
static Yellow = 8;
static Green = 3;
static LightGreen = 9;
static Cyan = 10;
static LightCyan = 11;
static Blue = 2;
static LightBlue = 12;
static Magenta = 6;
static LightMagenta = 13;
}
const colors = [
Color.Red,
Color.LightRed,
Color.Brown,
Color.Yellow,
Color.Green,
Color.LightGreen,
Color.Cyan,
Color.LightCyan,
Color.Blue,
Color.LightBlue,
Color.Magenta,
Color.LightMagenta,
];
const usaColors = [Color.Red, Color.White, Color.Blue];
function pickRandomColor() {
return colors[getRandomInt(colors.length)];
}
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function colorMessage(str, options = {}) {
let output = "";
let index = 0;
for (const c of str) {
let colorStr = colors[index % colors.length].toString().padStart(2, "0");
if (options.hasRandom) {
colorStr = pickRandomColor().toString().padStart(2, "0");
} else if (options.hasUsa) {
colorStr = usaColors[index % usaColors.length].toString().padStart(2, "0");
}
output += `\x03${colorStr}`;
if (options.hasBlackBg) {
output += ",";
output += Color.Black.toString().padStart(2, "0");
} else if (options.hasChaoticBgColors) {
output += ",";
output += pickRandomColor().toString().padStart(2, "0");
}
output += `${c}`;
++index;
}
return output;
}
/**
* Returns an IRC color string from a color code ( specified as Color on top of the script )
*
* @param {Color} color
*/
function pickColor(color) {
const str = color.toString().padStart(2, "0");
return `\x03${str}`;
}
function colorString(str, color) {
return `${pickColor(color)}${str}`;
}
function reverseStr(str) {
return str.split("").reverse().join("");
}
function gogolize(str) {
return str
.split("")
.map((c, i) => (i % 2 == 0 ? c.toUpperCase() : c.toLowerCase()))
.join("");
}
async function prismCallback(client, target, command, args) {
if (args.length === 0) {
for (const line of USAGE.split("\n")) {
client.sendMessage(colorString(line, Color.Red), target.chan);
}
return;
}
const options = parseOptions(args);
let message = args.filter((arg) => !arg.startsWith("-")).join(" ");
if (options.hasVersion) {
return client.sendMessage(
colorString(`Version: ${VERSION}`, Color.Green),
target.chan
);
}
if (options.hasGolgolize) message = gogolize(message);
if (options.hasReverse) message = reverseStr(message);
if (!options.hasDisableColoration) message = colorMessage(message, options);
if (options.hasMe) message = `/me ${message}`;
client.runAsUser(message, target.chan.id);
}
const options = {
input: function (client, target, command, args) {
return prismCallback(client, target, command, args);
},
allowDisconnected: true,
};
module.exports = {
onServerStart: (api) => {
thelounge = api;
thelounge.Commands.add("prism", options);
},
};