commit 768a72aec9fc5677b926dee4944e20ce6c4ba337
parent c3d21c6676d615a89fcea063f8bea89ef05e68ac
Author: Demonstrandum <moi@knutsen.co>
Date: Sat, 16 May 2020 19:26:12 +0100
Trigger command, should work.
Diffstat:
4 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/lib/commands/listen.ts b/lib/commands/listen.ts
@@ -12,7 +12,7 @@ export default (home_scope: HomeScope) => {
+ " be either `respond`, `reject`, `replace` or `trigger`"
+ "\nSee `help` page for `listen` for more information.");
- const index_str = args[1] || "";
+ const index_str = args[1] || "NaN";
const index = Number(index_str[0] === '#'
? index_str.tail()
: index_str);
diff --git a/lib/commands/trigger.ts b/lib/commands/trigger.ts
@@ -0,0 +1,3 @@
+import { rule } from '../rule';
+
+export default rule('trigger');
diff --git a/lib/main.ts b/lib/main.ts
@@ -345,7 +345,7 @@ Would you like to slow down a little?`.squeeze());
process_generic(message : Message) {
const CONFIG = GLOBAL_CONFIG.guilds[message.guild.id];
- const { content } = message;
+ const { content } = message; // Original content.
if (!content) return; // Message with no content (deleted)...
for (const responder of CONFIG.rules.respond) {
if (!responder) continue; // Sparse arrays!
@@ -359,6 +359,23 @@ Would you like to slow down a little?`.squeeze());
if (match && response) message.answer(response);
}
+ for (const triggerer of CONFIG.rules.trigger) {
+ if (!triggerer) continue; // Sparse arrays!
+ const match = content.match(triggerer.match);
+ const { response } = triggerer;
+
+ if (triggerer.listens
+ && triggerer.listens.length > 0
+ && !triggerer.listens.includes(message.author.id))
+ continue;
+
+ if (match && response) {
+ const p = CONFIG.commands.prefix;
+ message.content = `${p}${response}`;
+ // Send it back as a command.
+ this.on_message(message, SimpOMatic._CLIENT);
+ }
+ }
for (const rejecter of CONFIG.rules.reject) {
if (!rejecter) continue; // Sparse arrays!
const match = content.match(rejecter.match);
diff --git a/lib/rule.ts b/lib/rule.ts
@@ -73,6 +73,10 @@ export const rule = (rule_kind: string) => (home_scope: HomeScope) => {
options = 'ui';
response = args.tail().join(' ').trim();
}
+ const p = CONFIG.commands.prefix;
+ if (response.startsWith(p) && rule_kind === 'trigger') {
+ response = response.slice(p.length);
+ }
// Add the rule to the CONFIG.rules.
try {
CONFIG.rules[rule_kind].push({