Skip to content

Commit

Permalink
refactor and add quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
ender-null committed May 7, 2024
1 parent 0ae93a4 commit 7433632
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 90 deletions.
16 changes: 6 additions & 10 deletions src/plugins/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -27,14 +27,11 @@ export class AboutPlugin extends PluginBase {
];
this.strings = {
greeting: "Hi! I'm <b>{0}</b>!\nNice to meet you.",
version: 'Running <a href="https://git.io/polaris">polaris</a> <code>{0}</code> by @endernull.',
version: 'Running <a href="https://git.io/polaris">polaris</a> <code>{0}</code> by {1}.',
license:
'<b>Polaris</b> (including all plugins and documentation) is <b>free software</b>; you are free to redistribute it and/or modify it under the terms of the <b>AGPL-3.0 License</b>.',
help: 'Write {0}help to know what I can do!',
about: 'Use {0}about to know more about me',
channel: 'Support & suggestions at <a href="https://t.me/PolarisSupport">@PolarisSupport</a>',
notice:
'You can try my other bots: <a href="https://t.me/Sakubo">@Sakubo</a> and <a href="https://t.me/PetoBot">@PetoBot</a>',
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',
Expand All @@ -44,24 +41,23 @@ export class AboutPlugin extends PluginBase {
async run(msg: Message): Promise<void> {
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;
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/asco-de-vida.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export class AscoDeVidaPlugin extends PluginBase {
const published = story.find('.pre').text();
let content = story.find('.advlink').text();
content = content.replace('<br/>', '\n');
content = content.replace('ADV', '<b>ADV</b>');
const text = `${content}\n\n<i>${published.trim()}</i>`;
const text = `<blockquote>${content}</blockquote>\n\n<i>${published.trim()}</i>`;
this.bot.replyMessage(msg, text);
}
}
2 changes: 1 addition & 1 deletion src/plugins/chatgpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];
}
Expand Down
16 changes: 12 additions & 4 deletions src/plugins/cinema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@ export class CinemaPlugin extends PluginBase {
.map((session) => {
let label = session.time;
if (session.type) label = `${label} ${session.type}`;
return `<a href="${session.url}">${label}</a>`;
if (this.bot.platform === 'telegram') {
return `<a href="${session.url}">${label}</a>`;
} else {
return label;
}
})
.join(', ');
text += `\n<b>${item.name}</b>\n`;
text += `<a href="${item.source}">🔗</a>`;
if (item.trailer) {
text += `<a href="${item.trailer}">🎬</a>`;
if (this.bot.platform === 'telegram') {
text += `<a href="${item.source}">🔗</a>`;
if (item.trailer) {
text += `<a href="${item.trailer}">🎬</a>`;
}
} else {
text += `<a href="${item.source}">🔗</a>\n`;
}
text += `⌛ ${item.durationReadable}`;
text += `\n🎫 ${sessions}\n`;
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ export class CommitPlugin extends PluginBase {
];
}
async run(msg: Message): Promise<void> {
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 = `<blockquote>${commit}</blockquote>`;
return this.bot.replyMessage(msg, content);
}
}
46 changes: 0 additions & 46 deletions src/plugins/github.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugins/reminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<i>${reminder.text}</i>\n - ${name}`;
let text = `<blockquote>${reminder.text}</blockquote>\n - ${name}`;
if (username && username.length > 0) {
text += ` (@${username})`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/urban-dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class UrbanDictionaryPlugin extends PluginBase {
}

const entry = content.list[0];
let text = `<b>${term}</b>\n\n${entry.definition}`;
let text = `<b>${term}</b>\n\n<blockquote>${entry.definition}</blockquote>`;
if (entry.example) {
text += `\n\n<i>${entry.example}</i>`;
}
Expand Down
25 changes: 3 additions & 22 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export const getUsername = async (bot: Bot, uid: number | string, includeNick =
name = `@${group.username}`;
}
} else {
name = '[null]';
name = '<null>';
}
return name;
};
Expand Down Expand Up @@ -374,7 +374,7 @@ export const getFullName = async (bot: Bot, uid: number | string, includeUsernam
name += ` (@${group.username})`;
}
} else {
name = '[null]';
name = '<null>';
}
return name;
};
Expand Down Expand Up @@ -799,6 +799,7 @@ export const htmlToMarkdown = (text: string): string => {
text = text.replace(new RegExp('<u>(.*?)</u>', 'gim'), '~$1~');
text = text.replace(new RegExp('<code>(.*?)</code>', 'gim'), '`$1`');
text = text.replace(new RegExp('<pre>(.*?)</pre>', 'gim'), '```$1```');
text = text.replace(new RegExp('<blockquote>(.*?)</blockquote>', 'gim'), '> $1');

text = text.replace(new RegExp('&lt;', 'gim'), '<');
text = text.replace(new RegExp('&gt;', 'gim'), '>');
Expand Down Expand Up @@ -826,26 +827,6 @@ export const htmlToMarkdownLegacy = (text: string): string => {
return text;
};

export const htmlToDiscordMarkdown = (text: string): string => {
if (text) {
const replacements = [
{ pattern: '<code class="language-([\\w]+)">([\\S\\s]+)</code>', sub: '```$1\n$2```' },
{ pattern: '<a href="(.[^<]+)">(.[^<]+)</a>', 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('&lt;', 'gim'), '<');
text = text.replace(new RegExp('&gt;', 'gim'), '>');
}
return text;
};

export const splitLargeMessage = (content: string, maxLength: number): string[] => {
const lineBreak = '\n';
const texts = [];
Expand Down

0 comments on commit 7433632

Please sign in to comment.