From 74336323ec9980f1ec2a0f96588316dadf47f78c Mon Sep 17 00:00:00 2001 From: ender Date: Tue, 7 May 2024 13:50:43 +0200 Subject: [PATCH] refactor and add quotes --- src/plugins/about.ts | 16 +++++------- src/plugins/asco-de-vida.ts | 3 +-- src/plugins/chatgpt.ts | 2 +- src/plugins/cinema.ts | 16 +++++++++--- src/plugins/commit.ts | 7 ++--- src/plugins/github.ts | 46 --------------------------------- src/plugins/reminder.ts | 2 +- src/plugins/urban-dictionary.ts | 2 +- src/utils.ts | 25 +++--------------- 9 files changed, 29 insertions(+), 90 deletions(-) delete mode 100644 src/plugins/github.ts diff --git a/src/plugins/about.ts b/src/plugins/about.ts index 5f83476..2f6b9e4 100644 --- a/src/plugins/about.ts +++ b/src/plugins/about.ts @@ -2,7 +2,7 @@ import format from 'string-format'; import { db } from '../main'; import { PluginBase } from '../plugin'; -import { isCommand } from '../utils'; +import { getUsername, isCommand } from '../utils'; import { Bot } from '../bot'; import { Message } from '../types'; @@ -27,14 +27,11 @@ export class AboutPlugin extends PluginBase { ]; this.strings = { greeting: "Hi! I'm {0}!\nNice to meet you.", - version: 'Running polaris {0} by @endernull.', + version: 'Running polaris {0} by {1}.', license: 'Polaris (including all plugins and documentation) is free software; you are free to redistribute it and/or modify it under the terms of the AGPL-3.0 License.', help: 'Write {0}help to know what I can do!', about: 'Use {0}about to know more about me', - channel: 'Support & suggestions at @PolarisSupport', - notice: - 'You can try my other bots: @Sakubo and @PetoBot', donations: 'You can make {0}donations at https://revolut.me/endernull', stats: 'šŸ‘¤ {0} users\nšŸ‘„ {1} groups', donationsExplanation: 'You can make donations at https://revolut.me/endernull', @@ -44,24 +41,23 @@ export class AboutPlugin extends PluginBase { async run(msg: Message): Promise { let text; if (isCommand(this, 1, msg.content) || isCommand(this, 3, msg.content)) { + const ownerUsername = await getUsername(this.bot, this.bot.config.owner); const users = db[this.bot.platform].collection('users'); const userCount = await users.countDocuments(); const groups = db[this.bot.platform].collection('groups'); const groupCount = await groups.countDocuments(); const greeting = format(this.strings.greeting, this.bot.user.firstName); - const version = format(this.strings.version, process.env.npm_package_version); + const version = format(this.strings.version, process.env.npm_package_version, ownerUsername); const license = this.strings.license; const help = format(this.strings.help, this.bot.config.prefix); const about = format(this.strings.about, this.bot.config.prefix); - const notice = this.strings.notice; - const channel = this.strings.channel; const donations = format(this.strings.donations, this.bot.config.prefix); const stats = format(this.strings.stats, userCount, groupCount); if (isCommand(this, 1, msg.content)) { - text = `${greeting}\n\n${notice}\n${channel}\n\n${help}\n\n${version}\n${donations}\n\n${license}\n\n${stats}`; + text = `${greeting}\n\n${help}\n\n${version}\n${donations}\n\n${license}\n\n${stats}`; } else { - text = `${greeting}\n\n${notice}\n${channel}\n\n${help}\n${about}\n\n${donations}`; + text = `${greeting}\n\n${help}\n${about}\n\n${donations}`; } } else if (isCommand(this, 2, msg.content)) { const donationsExplanation = this.strings.donationsExplanation; diff --git a/src/plugins/asco-de-vida.ts b/src/plugins/asco-de-vida.ts index 79a4a6a..431fd11 100644 --- a/src/plugins/asco-de-vida.ts +++ b/src/plugins/asco-de-vida.ts @@ -29,8 +29,7 @@ export class AscoDeVidaPlugin extends PluginBase { const published = story.find('.pre').text(); let content = story.find('.advlink').text(); content = content.replace('
', '\n'); - content = content.replace('ADV', 'ADV'); - const text = `${content}\n\n${published.trim()}`; + const text = `
${content}
\n\n${published.trim()}`; this.bot.replyMessage(msg, text); } } diff --git a/src/plugins/chatgpt.ts b/src/plugins/chatgpt.ts index 5eccb25..88d8494 100644 --- a/src/plugins/chatgpt.ts +++ b/src/plugins/chatgpt.ts @@ -21,7 +21,7 @@ export class ChatGPTPlugin extends PluginBase { }, ], friendly: `^@${this.bot.user.username}`, - description: 'Use ChatGPT to provide text outputs in response to inputs', + description: 'Use ChatGPT to provide response to an input', }, ]; } diff --git a/src/plugins/cinema.ts b/src/plugins/cinema.ts index e284fbc..1037dda 100644 --- a/src/plugins/cinema.ts +++ b/src/plugins/cinema.ts @@ -49,13 +49,21 @@ export class CinemaPlugin extends PluginBase { .map((session) => { let label = session.time; if (session.type) label = `${label} ${session.type}`; - return `${label}`; + if (this.bot.platform === 'telegram') { + return `${label}`; + } else { + return label; + } }) .join(', '); text += `\n${item.name}\n`; - text += `šŸ”—`; - if (item.trailer) { - text += `šŸŽ¬`; + if (this.bot.platform === 'telegram') { + text += `šŸ”—`; + if (item.trailer) { + text += `šŸŽ¬`; + } + } else { + text += `šŸ”—\n`; } text += `āŒ› ${item.durationReadable}`; text += `\nšŸŽ« ${sessions}\n`; diff --git a/src/plugins/commit.ts b/src/plugins/commit.ts index d5b905c..8883ba2 100644 --- a/src/plugins/commit.ts +++ b/src/plugins/commit.ts @@ -14,12 +14,13 @@ export class CommitPlugin extends PluginBase { ]; } async run(msg: Message): Promise { - const url = 'http://whatthecommit.com/index.txt'; + const url = 'https://whatthecommit.com/index.txt'; const resp = await sendRequest(url, null, null, null, false, this.bot); - const content = await resp.text(); - if (!content) { + const commit = await resp.text(); + if (!commit) { return this.bot.replyMessage(msg, this.bot.errors.connectionError); } + const content = `
${commit}
`; return this.bot.replyMessage(msg, content); } } diff --git a/src/plugins/github.ts b/src/plugins/github.ts deleted file mode 100644 index 5cad111..0000000 --- a/src/plugins/github.ts +++ /dev/null @@ -1,46 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ - -import { Bot } from '../bot'; -import { PluginBase } from '../plugin'; -import { Conversation } from '../types'; -import { capitalize, getTaggedWith, logger } from '../utils'; - -export class GitHubPlugin extends PluginBase { - constructor(bot: Bot) { - super(bot); - } - - async webhook(url: string, data: any): Promise { - logger.debug(`github plugin: ${url}`); - - if (data) { - let text; - const subs = await getTaggedWith(this.bot, `sub:github/${data.repository.name}`); - if (data.head_commit) { - text = `${data.commits.length} new commit to ${ - data.repository.name - }:${data.ref.replace('refs/heads/', '')}\n\n${data.head_commit.id.slice( - 0, - 7, - )}: ${data.head_commit.message} by ${data.repository.owner.name}`; - } - if (data.check_run) { - let icon; - if (data.check_run.status == 'completed') { - icon = 'āœ”ļø'; - } else if (data.check_run.status == 'queued') { - icon = 'āž•'; - } else if (data.check_run.status == 'failed') { - icon = 'āŒ'; - } else { - icon = 'ā”'; - } - const status = capitalize(data.check_run.status); - text = `${icon} ${status} check run ${data.check_run.id}`; - } - subs.map((sub) => { - this.bot.sendMessage(new Conversation(sub), text); - }); - } - } -} diff --git a/src/plugins/reminder.ts b/src/plugins/reminder.ts index 3ea8a87..c1929d4 100644 --- a/src/plugins/reminder.ts +++ b/src/plugins/reminder.ts @@ -186,7 +186,7 @@ export class ReminderPlugin extends PluginBase { name = await getFullName(this.bot, reminder.userId, false); username = (await getUsername(this.bot, reminder.userId)).slice(1); } - let text = `${reminder.text}\n - ${name}`; + let text = `
${reminder.text}
\n - ${name}`; if (username && username.length > 0) { text += ` (@${username})`; } diff --git a/src/plugins/urban-dictionary.ts b/src/plugins/urban-dictionary.ts index fc95ec8..7d0d504 100644 --- a/src/plugins/urban-dictionary.ts +++ b/src/plugins/urban-dictionary.ts @@ -43,7 +43,7 @@ export class UrbanDictionaryPlugin extends PluginBase { } const entry = content.list[0]; - let text = `${term}\n\n${entry.definition}`; + let text = `${term}\n\n
${entry.definition}
`; if (entry.example) { text += `\n\n${entry.example}`; } diff --git a/src/utils.ts b/src/utils.ts index fd46ece..a8d837f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -343,7 +343,7 @@ export const getUsername = async (bot: Bot, uid: number | string, includeNick = name = `@${group.username}`; } } else { - name = '[null]'; + name = ''; } return name; }; @@ -374,7 +374,7 @@ export const getFullName = async (bot: Bot, uid: number | string, includeUsernam name += ` (@${group.username})`; } } else { - name = '[null]'; + name = ''; } return name; }; @@ -799,6 +799,7 @@ export const htmlToMarkdown = (text: string): string => { text = text.replace(new RegExp('(.*?)', 'gim'), '~$1~'); text = text.replace(new RegExp('(.*?)', 'gim'), '`$1`'); text = text.replace(new RegExp('
(.*?)
', 'gim'), '```$1```'); + text = text.replace(new RegExp('
(.*?)
', 'gim'), '> $1'); text = text.replace(new RegExp('<', 'gim'), '<'); text = text.replace(new RegExp('>', 'gim'), '>'); @@ -826,26 +827,6 @@ export const htmlToMarkdownLegacy = (text: string): string => { return text; }; -export const htmlToDiscordMarkdown = (text: string): string => { - if (text) { - const replacements = [ - { pattern: '([\\S\\s]+)', sub: '```$1\n$2```' }, - { pattern: '(.[^<]+)', sub: '$1' }, - { pattern: '<[/]?i>', sub: '_' }, - { pattern: '<[/]?b>', sub: '**' }, - { pattern: '<[/]?u>', sub: '__' }, - { pattern: '<[/]?code>', sub: '`' }, - { pattern: '<[/]?pre>', sub: '```' }, - ]; - replacements.map((rep) => { - text = text.replace(new RegExp(rep['pattern'], 'gim'), rep['sub']); - }); - text = text.replace(new RegExp('<', 'gim'), '<'); - text = text.replace(new RegExp('>', 'gim'), '>'); - } - return text; -}; - export const splitLargeMessage = (content: string, maxLength: number): string[] => { const lineBreak = '\n'; const texts = [];