From 35a64b4f2522394779d198d664667861c9c186a4 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Tue, 26 Jul 2022 21:06:30 +0800 Subject: [PATCH] use `?` prefix for tags --- src/commands/tags.ts | 4 +++- src/index.ts | 50 ++++++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/commands/tags.ts b/src/commands/tags.ts index 6279679..f506368 100644 --- a/src/commands/tags.ts +++ b/src/commands/tags.ts @@ -12,6 +12,7 @@ export const cmd: Command = { for (const i in tags) { const tag = tags[i]; let text = ''; + if (tag.aliases && tag.aliases[0]) { text += '**Aliases**: ' + tag.aliases.join(', ') + '\n'; } @@ -21,7 +22,8 @@ export const cmd: Command = { } else if (tag.embed) { text += '\n[embedded message]'; } - em.addField(tag.name, text); + + em.addField('?' + tag.name, text); } await e.reply({ embeds: [em] }); diff --git a/src/index.ts b/src/index.ts index ab8555a..e9c3344 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import * as BuildConfig from './constants'; import { commands } from './commands'; import { filterMessage } from './filters'; import { parseLog } from './logs'; +import { getLatestMinecraftVersion } from './utils/remoteVersions'; import { parse as discordParse, @@ -19,10 +20,8 @@ import { import random from 'just-random'; import { readFile } from 'fs/promises'; import { join } from 'path'; - import { green, bold, blue, underline, yellow } from 'kleur/colors'; import 'dotenv/config'; -import { getLatestMinecraftVersion } from './utils/remoteVersions'; export interface Command { name: string; @@ -111,8 +110,10 @@ client.once('ready', async () => { ); } - const commanded = await parseMsg(e); + const commanded = await parseMsgForCommands(e); if (commanded) return; + const tagged = await parseMsgForTags(e); + if (tagged) return; const log = await parseLog(e.content); if (log != null) { @@ -122,10 +123,8 @@ client.once('ready', async () => { }); }); -async function parseMsg(e: Message) { - const parsed = discordParse(e, '!', { - allowBots: true, - }); +async function parseMsgForCommands(e: Message) { + const parsed = discordParse(e, '!', { allowBots: true }); if (!parsed.success) return false; const cmd = commands.find( @@ -133,22 +132,6 @@ async function parseMsg(e: Message) { ); if (!cmd) { - // TODO: Do not read tags.json everytime there is a new message - const tag = await getTags().then((r) => - r.find( - (t) => t.name == parsed.command || t.aliases?.includes(parsed.command) - ) - ); - - if (tag) { - if (tag.text) { - e.reply(tag.text); - } else if (tag.embed) { - const em = new MessageEmbed(tag.embed); - e.reply({ embeds: [em] }); - } - return true; - } return false; } @@ -167,4 +150,25 @@ async function parseMsg(e: Message) { return true; } +async function parseMsgForTags(e: Message) { + const parsed = discordParse(e, '?', { allowBots: true }); + if (!parsed.success) return false; + + const tag = await getTags().then((r) => + r.find( + (t) => t.name == parsed.command || t.aliases?.includes(parsed.command) + ) + ); + + if (tag) { + if (tag.text) { + e.reply(tag.text); + } else if (tag.embed) { + const em = new MessageEmbed(tag.embed); + e.reply({ embeds: [em] }); + } + return true; + } +} + client.login(process.env.DISCORD_TOKEN);