commit b72c098fdd3cc7e80b8ed29f8e00635c856f551f
parent bcc5d333138720e3aea9f43cd9be231a87c5abc6
Author: Demonstrandum <moi@knutsen.co>
Date: Thu, 19 Mar 2020 20:05:48 +0000
Fix respond and reject.
Diffstat:
4 files changed, 29 insertions(+), 32 deletions(-)
diff --git a/HELP.md b/HELP.md
@@ -49,11 +49,11 @@
- `!ignore whitelist [type] [@name]` — Will exempt certain users or groups from any of the ignore-rules, ever. (`[type]` is either `user` or `group`)
- `!ignore <ls>` — lists all ignore rules by type.
- `!ignore rm [type] [@name]` — clears all ignore rules for a certain type (types are: `user`, `channel` or `group`).
-- `!respond` **〈not impl.〉** — How the bot should respond to certain messages:
+- `!respond` — How the bot should respond to certain messages:
- `!respond [match] [reply]` — matches an expression said (using regular-expressions, i.e. `/regex/flags`), and replies with a message.
- `!respond <ls>` — list all response rules numerically.
- `!respond rm #[rule-index]` — removes the response-rule by index.
-- `!reject` **〈not impl.〉** — Deletes messages meeting certain patterns:
+- `!reject` — Deletes messages meeting certain patterns:
- `!reject [match] <reply>` — rejects certain messages, matching a regular-expression (specifying a reply is optional).
- `!reject <ls>` — numerically lists all rejection rules.
- `!reject rm #[rule-index]` — removes the rejection-rule specified by a numerical index.
@@ -70,7 +70,7 @@
- `!gif [gif-search-terms]` **〈not impl.〉** — Searches for and returns a GIF matching your search.
- `!cat` **〈not impl.〉** — Pussycat pictures...
- `!news [news-search-term]` **〈not impl.〉** — Sends you the most relevant news on the specified topic area.
-- `!youtube [youtube-search-terms]` **〈not impl.〉** — Searches for and returns a relevant _YouTube_ video.
+- `!youtube [youtube-search-terms]` — Searches for and returns a relevant _YouTube_ video.
- `!wikipedia` **〈not impl.〉** — Search through Wikipedia, returning the most relevant wiki-link.
- `!translate <language> [phrase]` **〈not impl.〉** — Translate a phrase from a language (if none specified, it will auto-detect).
- `!wolfram` **〈not impl.〉** — Query Wolfram|Alpha.
diff --git a/lib/api/google.ts b/lib/api/google.ts
@@ -37,6 +37,8 @@ const web_search = (param : CSE) => new Promise((resolve, reject) => {
delete CACHE[cache_keys[2]];
}
+ const num_match = param.query.trim().match(/[ ]+(\d+)$/);
+ const result_index = Math.abs(num_match ? Number(num_match[1]) - 1 : 0);
const cs = google.customsearch('v1');
cs.cse.list({
@@ -45,13 +47,13 @@ const web_search = (param : CSE) => new Promise((resolve, reject) => {
q: param.query,
searchType: (param.kind === 'web') ? undefined : param.kind,
start: 0,
- num: 1,
+ num: result_index + 1,
safe: param.nsfw ? 'off' : 'active'
}).then(res => {
if (!res.data || !res.data.items || res.data.items.length === 0)
return reject('No such results found.');
- const item = res.data.items[0];
+ const item = res.data.items[result_index];
const answer = `Search for ‘${param.query}’: ${item.link}\n>>> ${item.title}`;
// Cache this query (DO NOT CACHE NSFW)
if (!param.nsfw)
diff --git a/lib/main.ts b/lib/main.ts
@@ -36,17 +36,6 @@ let CONFIG = deep_merge(
DEFAULT_CONFIG,
JSON.parse(read_file('./bot.json', 'utf-8')));
-// CONFIG will eventually update to the online version.
-pastebin_latest().then(res => {
- CONFIG = deep_merge(CONFIG, res);
- // Remove any duplicates.
- CONFIG = export_config(CONFIG, { ugly: true });
- CONFIG = JSON.parse(CONFIG);
- // Precompile all regular-expressions in known places.
- ['respond', 'reject', 'replace']
- .each(name => CONFIG.rules[name].mut_map(compile_match));
-}).catch(console.warn);
-
// Store secrets in an object, retrieved from shell's
// environment variables.
const SECRETS = JSON.parse(shell('sh ./generate_secrets.sh').toString());
@@ -77,12 +66,6 @@ const KNOWN_COMMANDS = HELP_SECTIONS.map(e =>
const GIT_URL = 'https://github.com/Demonstrandum/Simp-O-Matic';
-const HOMESCOPE = {
- HELP_SOURCE, HELP_KEY, GIT_URL,
- HELP_MESSAGES, HELP_SECTIONS, ALL_HELP,
- CONFIG, SECRETS, KNOWN_COMMANDS
-};
-
// Log where __dirname and cwd are for deployment.
console.log('File/Execution locations:', {
'__dirname': __dirname,
@@ -94,18 +77,15 @@ export class SimpOMatic {
private static _CLIENT : Client;
private _COMMAND_HISTORY : Message[] = [];
- constructor() {
- console.log('Secrets:', pp(SECRETS));
- console.log('Configured Variables:', pp(CONFIG));
- console.log('Known commands:', pp(KNOWN_COMMANDS));
- }
-
static start() {
this._CLIENT = new Client();
this._CLIENT.login(
SECRETS.api.token,
`${__dirname}/*Discord.ts`
);
+ console.log('Secrets:', pp(SECRETS));
+ console.log('Configured Variables:', pp(CONFIG));
+ console.log('Known commands:', pp(KNOWN_COMMANDS));
}
expand_alias(operator, args) {
@@ -185,7 +165,10 @@ export class SimpOMatic {
if (commands.includes(operator))
return import(`./commands/${operator}`).then(mod =>
mod.default({ // Basic 'home-scope' is passed in.
- message, args, ...HOMESCOPE}));
+ message, args,
+ HELP_SOURCE, HELP_KEY, GIT_URL,
+ HELP_MESSAGES, HELP_SECTIONS, ALL_HELP,
+ CONFIG, SECRETS, KNOWN_COMMANDS }));
switch (operator) {
case 'commands': {
@@ -447,6 +430,7 @@ export class SimpOMatic {
const { content } = message;
if (!content) return; // Message with no content (deleted)...
for (const responder of CONFIG.rules.respond) {
+ console.log('CHECKING RESPOND:', responder);
if (!responder) continue; // Sparse arrays!
const match = content.match(responder.match);
const { response } = responder;
@@ -594,8 +578,19 @@ const on_termination = () => {
}, 6000));
};
-// Start The Simp'O'Matic.
-SimpOMatic.start();
+// CONFIG will eventually update to the online version.
+pastebin_latest().then(res => {
+ CONFIG = deep_merge(CONFIG, res);
+ // Remove any duplicates.
+ CONFIG = export_config(CONFIG, { ugly: true });
+ CONFIG = JSON.parse(CONFIG);
+ // Precompile all regular-expressions in known places.
+ ['respond', 'reject', 'replace']
+ .each(name => CONFIG.rules[name].mut_map(compile_match));
+ // Start The Simp'O'Matic.
+ SimpOMatic.start();
+}).catch(console.warn);
+
// Handle exits.
process.on('exit', on_termination);
diff --git a/lib/rule.ts b/lib/rule.ts
@@ -60,7 +60,7 @@ export const rule = (rule_kind: string) => home_scope => {
// such that it will have to be matched on its own, not
// surrounded by other letters or numbers, OR, it may exits
// at the begging or end of the line.
- regex = `(^|[^\\p{L}\\p{N}])+${args[0]}?([^\\p{L}\\p{N}]|$)+`,
+ regex = `(^|[^\\p{L}\\p{N}])+${args[0]}s?([^\\p{L}\\p{N}]|$)+`,
options = 'ui';
response = args.tail().join(' ').trim();
}