Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit b4a67c297f6e8a7309f3f158c63ee396a5f76c25
parent 7ae6f4c9b650c556115c3206643912fb5d043ce2
Author: Demonstrandum <moi@knutsen.co>
Date:   Fri, 15 May 2020 23:14:30 +0100

Don't index null match.

Diffstat:
Mlib/api/google.ts | 4++--
Alib/commands/listen.ts | 38++++++++++++++++++++++++++++++++++++++
Mlib/default.ts | 19+++++++++++++++----
Mlib/extensions.ts | 3++-
Mlib/main.ts | 14+++++++++++++-
5 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/lib/api/google.ts b/lib/api/google.ts @@ -42,7 +42,7 @@ const web_search = (param : CSE) => new Promise((resolve, reject) => { if (num_match) query = query.slice(0, -num_match[1].length).trim(); - const result_num : number = Number(num_match[1]) || 1; + const result_num : number = num_match ? Number(num_match[1]) : 1; if (result_num > 10) return reject("Can only query up to 10th result (API restriction)."); @@ -63,7 +63,7 @@ const web_search = (param : CSE) => new Promise((resolve, reject) => { return reject('No such results found.'); const item = res.data.items[result_index]; - const answer = `Search for ‘${param.query}’ (result no. ${result_num}) ${item.link}\n>>> ${item.title}`; + const answer = `Search for ‘${query}’ (result no. ${result_num}) ${item.link}\n>>> ${item.title}`; // Cache this query (DO NOT CACHE NSFW) if (!param.nsfw) CACHE[param.query] = answer; diff --git a/lib/commands/listen.ts b/lib/commands/listen.ts @@ -0,0 +1,38 @@ +const RULES = [ + 'reply', 'reject', + 'replace', 'trigger' +]; + +export default (home_scope: HomeScope) => { + const { message, args, CONFIG } = home_scope; + + const rule_type = args[0]; + if (!RULES.includes(rule_type)) + return message.answer("`listen` command first argument must" + + " be either `reply`, `reject`, `replace` or `trigger`" + + "\nSee `help` page for `listen` for more information."); + + const index = Number(args[1].trim()); + if (!index) + return message.answer("Second argument must be a number" + + " (greater than zero), that represents the index of the rule"); + + const rule_no = CONFIG.rules[rule_type].length; + if (index > rule_no) + return message.answer(`Index (${index}) is out of range.\n` + + `Only ${rule_no} elements exist in ${rule_type} rules.`); + + const ids = message.mentions.users + .map(user => user.id); + + if (args.length < 3 || ids.length === 0) + return message.answer("Please provide at least one username" + + " (in form of a mention)."); + + const rule = CONFIG.rules[rule_type][index - 1]; + + if (!rule.listens) + rule.listens = []; + + rule.listens.push.apply(rule.listens, ids); +}; diff --git a/lib/default.ts b/lib/default.ts @@ -6,7 +6,8 @@ const DEFAULT_GUILD_CONFIG : Types.Config = { main_channel: null, system_channel: null, pp_sizes: { - '541761315887120399': 16 + '541761315887120399': 16, + '265958795254038535': 36 }, weather_locations: { '541761315887120399': 'Moscow' @@ -60,15 +61,25 @@ const DEFAULT_GUILD_CONFIG : Types.Config = { respond: [ { match: "/^\\s*thanks\\p{P}*\\s*$/iu", - response: 'Obama.' + response: 'Obama.', + listens: [] }, { match: "/(^|[^\\p{L}\\p{N}])+bots?([^\\p{L}\\p{N}]|$)+/iu", - response: "The hell you sayn' about bots?" + response: "The hell you sayn' about bots?", + listens: [] }, { match: "/Good (Morning|Day) (Star|Sun)shine/i", - response: "The Earth says Hello!" + response: "The Earth says Hello!", + listens: [] + }, + { + match: "/\b(hello|hi)\b/i", + response: "Hello my lord and master.", + listens: [ + '265958795254038535' + ] } ], reject: [ diff --git a/lib/extensions.ts b/lib/extensions.ts @@ -15,7 +15,8 @@ declare global { namespace Types { export type Match = { match: string | RegExp, - response: string + response: string, + listens?: string[] }; export type Ignore = { diff --git a/lib/main.ts b/lib/main.ts @@ -351,12 +351,24 @@ Would you like to slow down a little?`.squeeze()); if (!responder) continue; // Sparse arrays! const match = content.match(responder.match); const { response } = responder; + + if (responder.listens + && responder.listens.length > 0 + && !responder.listens.includes(message.author.id)) + continue; + if (match && response) message.answer(response); } for (const rejecter of CONFIG.rules.reject) { if (!rejecter) continue; // Sparse arrays! const match = content.match(rejecter.match); const { response } = rejecter; + + if (rejecter.listens + && rejecter.listens.length > 0 + && !rejecter.listens.includes(message.author.id)) + continue; + if (match) { if (response) message.answer(response); if (message.deletable) { @@ -406,7 +418,7 @@ Would you like to slow down a little?`.squeeze()); async expand(message : Message) : Promise<string> { // History expansion with !!, !!@, !!^@, !!<ordinal>, etc. const expansions = message.content - .replace(/(!!@?\^?\d*)/g, '@EXP$1@EXP') + .replace(/(!![@\^]?[\^@]?\d*)/g, '@EXP$1@EXP') .replace(/(\s)/g, '@EXP$1@EXP') .split('@EXP').squeeze();