commit 2be7df5ba89aaae4ce300fd1c3de72d9ed2f5e08
parent 43eb392dcd08a61ffdc15b3e5f655d39319a40fd
Author: Demonstrandum <moi@knutsen.co>
Date: Wed, 18 Mar 2020 18:08:22 +0000
Expand aliases deeply.
Diffstat:
6 files changed, 58 insertions(+), 39 deletions(-)
diff --git a/lib/api/google.ts b/lib/api/google.ts
@@ -50,7 +50,7 @@ const web_search = (param : CSE) => new Promise((resolve, reject) => {
return reject('No such results found.')
const item = res.data.items[0];
- const answer = `${item.link}\n>>> ${item.title}`;
+ const answer = `Search for ‘${param.query}’: ${item.link}\n>>> ${item.title}`;
// Cache this query
CACHE[param.query] = answer;
return resolve(answer);
diff --git a/lib/api/yt_scrape.ts b/lib/api/yt_scrape.ts
@@ -17,8 +17,9 @@ const yt_search = (params: YTSearch) => new Promise((resolve, reject) => {
const { channel: by, title, views,
upload_date, link: url } = res[0];
- return resolve(`${url}\n> ${title} | ${views.to_metric()} views | \
- uploaded ${upload_date} | by: ${by}.`.squeeze());
+ return resolve(`Searh for ‘${params.query}’: ${url}\n> ${title} | \
+ ${views.to_metric()} views | uploaded ${upload_date} | \
+ by: ${by}.`.squeeze());
});
});
diff --git a/lib/commands/echo.ts b/lib/commands/echo.ts
@@ -0,0 +1,4 @@
+export default home_scope => {
+ const { message, args } = home_scope;
+ message.channel.send(args.join(' '));
+};
diff --git a/lib/commands/weather.ts b/lib/commands/weather.ts
@@ -23,19 +23,17 @@ export default home_scope => {
const date = new Date();
const tz = d.timezone / 3600; // TODO: What if `tz` has a fractional part...
const hour = (24 + date.getUTCHours() + tz) % 24;
- console.log('UTC:', date.getUTCHours(), 'tz:', d.timezone);
const country = !d.sys ? 'somewhere' : d.sys.country;
- if (d.main) {
- message.answer(`${hour}:${date.getMinutes()} ${d.name}, \
- ${country}: ${d.main.temp}°C \
- (feels like ${d.main.feels_like}°C) \
- ${d.weather[0].description}, \
- ${d.main.temp_max}°C max, \
- ${d.main.temp_min}°C min`.squeeze());
- } else {
- message.answer(`Cannot get weather information`
- + ` from ${location}.`);
- }
+
+ if (d.main)
+ message.answer(`${hour}:${date.getMinutes()} ${d.name}, \
+ ${country}: ${d.main.temp}°C \
+ (feels like ${d.main.feels_like}°C) \
+ ${d.weather[0].description}, \
+ ${d.main.temp_max}°C max, \
+ ${d.main.temp_min}°C min`.squeeze());
+ else message.answer(`Cannot get weather information`
+ + ` from ${location}.`);
})
.catch(error);
}
diff --git a/lib/default.ts b/lib/default.ts
@@ -56,18 +56,18 @@ export default {
// Below are the different kinds of _rules_.
respond: [
{
- match: "/^\\s*thanks\p{P}*\\s*$/i",
+ match: "/^\\s*thanks\\p{P}*\\s*$/iu",
response: 'Obama.'
},
{
- match: "/(^|[^\\p{L}\\p{N}])+bot([^\\p{L}\\p{N}]|$)+/ui",
- respond: "The hell you sayn' about bots?"
+ match: "/(^|[^\\p{L}\\p{N}])+bot([^\\p{L}\\p{N}]|$)+/iu",
+ response: "The hell you sayn' about bots?"
}
],
reject: [
{
- match: "/\\.{4,}/",
- response: null
+ match: "/\\.{8,}/",
+ response: "Too many dots..."
},
],
replace: [ // Message editing functionality not a thing yet...
diff --git a/lib/main.ts b/lib/main.ts
@@ -106,7 +106,31 @@ export class SimpOMatic {
);
}
+ expand_alias(operator, args) {
+ const expander = unexpanded => {
+ let expanded = unexpanded;
+ if (CONFIG.operators.aliases.hasOwnProperty(unexpanded))
+ expanded = CONFIG.commands.aliases[unexpanded].trim().squeeze();
+
+ const expanded_command_words = expanded.split(' ');
+ if (expanded_command_words.length > 1) {
+ // This means the alias has expanded to more than just one word.
+ expanded = expanded_command_words.shift();
+ expanded_command_words.each(e => args.unshift(e));
+ }
+ return expanded
+ };
+
+ // Continue expanding until we have no more change.
+ const expanded = expander(operator);
+ if (expanded === operator)
+ return operator;
+ return this.expand_alias(operator, args)
+ }
+
process_command(message : Message) {
+ if (message.content.startsWith('..')) return;
+
const last_command = this._COMMAND_HISTORY.last();
this._COMMAND_HISTORY.push(message);
if (this._COMMAND_HISTORY.length > CONFIG.commands.max_history) {
@@ -138,31 +162,23 @@ export class SimpOMatic {
const words = content.tail().split(' ');
const args = words.tail();
- let command = words[0].toLowerCase();
- if (CONFIG.commands.aliases.hasOwnProperty(command))
- command = CONFIG.commands.aliases[command].trim().squeeze();
-
- const expanded_command_words = command.split(' ');
- if (expanded_command_words.length > 1) {
- // This means the alias has expanded to more than just one word.
- command = expanded_command_words.shift();
- expanded_command_words.each(e => args.unshift(e));
- }
+ let operator = words[0].toLowerCase();
+ operator = this.expand_alias(operator, args);
- command = command.toLowerCase();
- console.log('Received command:', [command, args]);
+ operator = operator.toLowerCase();
+ console.log('Received command:', [operator, args]);
// This should have most immediate access.
- if (command === 'ping') return message.answer('PONGGERS!');
+ if (operator === 'ping') return message.answer('PONGGERS!');
const commands = read_dir(`${__dirname}/commands`)
.map(n => n.slice(0, -3));
- if (commands.includes(command))
- return require(`./commands/${command}`)
+ if (commands.includes(operator))
+ return require(`./commands/${operator}`)
.default({ // Basic 'home-scope' is passed in.
message, args, ...HOMESCOPE});
- switch (command) {
+ switch (operator) {
case 'commands': {
const p = CONFIG.commands.prefix;
const joined_commands = KNOWN_COMMANDS.slice(0, -1)
@@ -479,9 +495,9 @@ export class SimpOMatic {
message.answer("That's an empty command...");
break;
} default: {
- if (KNOWN_COMMANDS.includes(command)) {
+ if (KNOWN_COMMANDS.includes(operator)) {
const p = CONFIG.commands.prefix;
- message.reply(`:scream: *gasp!* — The \`${p}${command}\` \
+ message.reply(`:scream: *gasp!* — The \`${p}${operator}\` \
command has not been implemented yet. \
Quick send a pull request! Just type in \
\`${p}fork\`, and get started...`.squeeze());
@@ -489,7 +505,7 @@ export class SimpOMatic {
}
message.answer(`
:warning: ${CONFIG.commands.not_understood}.
- > \`${CONFIG.commands.prefix}${command}\``.squeeze());
+ > \`${CONFIG.commands.prefix}${operator}\``.squeeze());
break;
}
}