Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
164 lines (138 loc) · 5.68 KB
/
Copy pathindex.js
File metadata and controls
164 lines (138 loc) · 5.68 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
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
"use strict";
const axios = require("axios");
const BASE_URL = "https://www.yomama-jokes.com";
const VALID_CATEGORIES = [
"fat", "ugly", "stupid", "poor", "old",
"skinny", "hairy", "short", "nasty", "bald", "tall"
];
const NICK_PREFIX = "--";
const red = "04";
const headers = { Accept: "application/json" };
async function getRandomJoke() {
const res = await axios.get(`${BASE_URL}/api/random`, { headers });
return extractJoke(res);
}
async function getCategoryJoke(category) {
const url = `${BASE_URL}/api/random/${encodeURIComponent(category)}`;
const res = await axios.post(url, null, { headers });
return extractJoke(res);
}
function extractJoke(response) {
if (response && response.data && typeof response.data.joke === "string") {
const joke = response.data.joke.trim();
if (joke.length > 0) return joke;
}
throw new Error("Unexpected response from Yo Mama API");
}
function makePossessive(nick) {
const n = (nick || "").trim();
if (!n) return n;
return /s$/i.test(n) ? `${n}'` : `${n}'s`;
}
function personalizeJoke(joke, nick) {
const p = makePossessive(nick);
if (!p) return joke;
// Replace leading "Yo mama" or "Yo momma" (case-insensitive)
return joke.replace(/^\s*yo\s+(mama|momma)\b/i, `${p} $1`);
}
function printHelp(client, target) {
client.sendMessage("Yo Mama plugin usage:", target.chan);
client.sendMessage("/yomama help → Show this help message", target.chan);
client.sendMessage("/yomama categories → List available categories", target.chan);
client.sendMessage("/yomama random → Get a random joke", target.chan);
client.sendMessage("/yomama <category> → Get a random joke from a category", target.chan);
client.sendMessage("/yomama --<nick> → Random joke personalized for <nick>", target.chan);
client.sendMessage("/yomama random --<nick> → Random joke personalized for <nick>", target.chan);
client.sendMessage("/yomama <category> --<nick> → Category joke personalized for <nick>", target.chan);
client.sendMessage(`Available categories: ${VALID_CATEGORIES.join(", ")}`, target.chan);
}
function parseNickAndArgs(args) {
const cleanArgs = (args || []).map((a) => String(a || "").trim()).filter(Boolean);
let nick = null;
let removalIndexes = new Set();
for (let i = 0; i < cleanArgs.length; i++) {
const tok = cleanArgs[i];
// Case 1: --nick (single token)
if (tok.startsWith(NICK_PREFIX) && tok.length > NICK_PREFIX.length) {
nick = tok.slice(NICK_PREFIX.length).trim();
removalIndexes.add(i);
break;
}
// Case 2: "--" followed by "nick" (two tokens)
if (tok === NICK_PREFIX && i + 1 < cleanArgs.length) {
const next = cleanArgs[i + 1].trim();
if (next) {
nick = next;
removalIndexes.add(i);
removalIndexes.add(i + 1);
break;
}
}
}
const baseArgs = removalIndexes.size
? cleanArgs.filter((_, idx) => !removalIndexes.has(idx))
: cleanArgs;
return { baseArgs, nick };
}
const yomamaCommand = {
input: async function (client, target, command, args) {
try {
const { baseArgs, nick } = parseNickAndArgs(args);
const firstArg = (baseArgs[0] || "").toLowerCase().trim();
// If user only provided a --nick, treat as random-for-nick (must precede help)
if (baseArgs.length === 0 && nick) {
const joke = await getRandomJoke();
const out = personalizeJoke(joke, nick);
client.runAsUser(out, target.chan.id);
return;
}
// Help
if (firstArg === "help" || baseArgs.length === 0) {
printHelp(client, target);
return;
}
if (firstArg === "categories") {
client.sendMessage(`Available categories: ${VALID_CATEGORIES.join(", ")}`, target.chan);
return;
}
if (firstArg === "random") {
const joke = await getRandomJoke();
const out = nick ? personalizeJoke(joke, nick) : joke;
client.runAsUser(out, target.chan.id);
return;
}
// Category mode (single, valid category)
if (baseArgs.length === 1 && VALID_CATEGORIES.includes(firstArg)) {
const joke = await getCategoryJoke(firstArg);
const out = nick ? personalizeJoke(joke, nick) : joke;
client.runAsUser(out, target.chan.id);
return;
}
// Unsupported arguments:
// - If no nick: just tip (no random fallback)
// - If nick present: tip + random personalized fallback
const shownArg = baseArgs.join(" ").trim();
let tip = `${red}Unsupported argument "${shownArg}". Try /yomama help or /yomama categories.`;
if (nick) {
tip += " Sending a random personalized joke instead…";
}
client.sendMessage(tip, target.chan);
if (nick) {
const fallback = await getRandomJoke();
const out = personalizeJoke(fallback, nick);
client.runAsUser(out, target.chan.id);
}
} catch (err) {
const msg = err && err.message ? err.message : String(err);
client.sendMessage(`${red}Error getting joke: ${msg}`, target.chan);
}
},
allowDisconnected: true,
};
module.exports = {
onServerStart: (api) => {
api.Commands.add("yomama", yomamaCommand);
},
// For tests
_command: yomamaCommand,
};