Skip to content

Commit 18798e8

Browse files
committed
refactor: improve changes and fix CI
1 parent 7310686 commit 18798e8

16 files changed

Lines changed: 69 additions & 40 deletions

File tree

bot/messages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import { IFiat } from '../util/fiatModel';
2626
import { CommunityContext } from './modules/community/communityContext';
2727
import { imageCache } from '../util/imageCache';
2828
import { ImageProcessingError } from '../util/errors';
29-
const { I18n } = require('@grammyjs/i18n');
3029
import { Community } from '../models';
3130

31+
const { I18n } = require('@grammyjs/i18n');
32+
3233
const startMessage = async (ctx: MainContext) => {
3334
try {
3435
const holdInvoiceExpiration = holdInvoiceExpirationInSecs();

bot/modules/community/scenes.communityAdmin.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Scenes } from 'telegraf';
22
import { CommunityContext } from './communityContext';
3+
import { isValidLanguage } from '../../../util/languages';
34

45
import * as CommunityEvents from '../events/community';
56

@@ -39,21 +40,23 @@ const communityAdmin = () => {
3940

4041
scene.command('/setlanguage', async (ctx: CommunityContext) => {
4142
try {
42-
const [, language] = ctx.message!.text.trim().split(' ');
43-
const lang = language?.toLowerCase();
44-
45-
// Check if language is valid
46-
const validLanguages = ['en', 'es', 'fr', 'de', 'it', 'pt', 'ru', 'uk', 'ko', 'fa'];
47-
if (!lang || !validLanguages.includes(lang)) {
43+
const [, maybeLanguage] = ctx.message!.text.split(' ');
44+
if (!maybeLanguage || maybeLanguage.trim() === '') {
4845
return ctx.reply(ctx.i18n.t('wizard_community_invalid_language'));
4946
}
50-
47+
48+
const language = maybeLanguage.trim().toLowerCase();
49+
if (!isValidLanguage(language)) {
50+
return ctx.reply(ctx.i18n.t('wizard_community_invalid_language'));
51+
}
52+
5153
const { community } = ctx.scene.state as any;
52-
community.language = lang;
54+
community.language = language;
5355
await community.save();
54-
await ctx.reply(ctx.i18n.t('community_language_updated', { language: lang }));
56+
await ctx.reply(ctx.i18n.t('community_language_updated', { language }));
5557
CommunityEvents.communityUpdated(community);
5658
} catch (err) {
59+
console.error('setlanguage error:', err);
5760
return ctx.reply(ctx.i18n.t('generic_error'));
5861
}
5962
});

bot/modules/community/scenes.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from './messages';
1313
import { CommunityContext } from './communityContext';
1414
import * as commAdmin from './scenes.communityAdmin';
15+
import { isValidLanguage } from '../../../util/languages';
1516

1617
const CURRENCIES = parseInt(process.env.COMMUNITY_CURRENCIES || '10');
1718

@@ -165,10 +166,8 @@ const createCommunitySteps = {
165166

166167
ctx.wizard.state.error = null;
167168
const lang = text.trim().toLowerCase();
168-
169-
// Check if language is valid
170-
const validLanguages = ['en', 'es', 'fr', 'de', 'it', 'pt', 'ru', 'uk', 'ko', 'fa'];
171-
if (!validLanguages.includes(lang)) {
169+
170+
if (!isValidLanguage(lang)) {
172171
ctx.telegram.deleteMessage(ctx.chat!.id, ctx.message!.message_id);
173172
ctx.wizard.state.error = ctx.i18n.t('wizard_community_invalid_language');
174173
return await ctx.wizard.state.updateUI();
@@ -896,13 +895,11 @@ export const updateLanguageCommunityWizard = new Scenes.WizardScene(
896895
if (ctx.message === undefined) return ctx.scene.leave();
897896

898897
const lang = ctx.message.text.trim().toLowerCase();
899-
// Check if language is valid
900-
const validLanguages = ['en', 'es', 'fr', 'de', 'it', 'pt', 'ru', 'uk', 'ko', 'fa'];
901-
if (!validLanguages.includes(lang)) {
898+
if (!isValidLanguage(lang)) {
902899
ctx.deleteMessage();
903900
return await ctx.reply(ctx.i18n.t('wizard_community_invalid_language'));
904901
}
905-
902+
906903
const { community } = ctx.wizard.state;
907904
community.language = lang;
908905
await community.save();

bot/ordersActions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import { UserDocument } from '../models/user';
1515
import { HasTelegram, MainContext } from './start';
1616
import { IOrder } from '../models/order';
1717
import { IFiat } from '../util/fiatModel';
18+
1819
import * as OrderEvents from './modules/events/orders';
1920

2021
const { ObjectId } = require('mongoose').Types;
22+
const { I18n } = require('@grammyjs/i18n');
2123

2224
interface CreateOrderArguments {
2325
type: string;
@@ -84,7 +86,6 @@ const createOrder = async (
8486

8587
// Get community language for description
8688
if (community.language) {
87-
const { I18n } = require('@grammyjs/i18n');
8889
descriptionI18n = new I18n({
8990
defaultLanguageOnMissing: true,
9091
locale: community.language,

locales/de.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ community_admin_help: |
585585
/setlanguage <lang> - Configure community's language for published messages.
586586
community_npub_updated: You added the community's pubkey ${npub} successfully!
587587
community_language_updated: You updated the community's language to ${language} successfully!
588-
wizard_community_enter_language: Enter the language code for your community (en, es, fr, de, it, pt, ru, uk, ko, fa)
589-
wizard_community_invalid_language: Invalid language code. Please enter one of the following - en, es, fr, de, it, pt, ru, uk, ko, fa
588+
wizard_community_enter_language: 'Enter the language code for your community (en, es, fr, de, it, pt, ru, uk, ko, fa)'
589+
wizard_community_invalid_language: 'Invalid language code. Please enter one of the following - en, es, fr, de, it, pt, ru, uk, ko, fa'
590590
# END modules/community
591591

592592
# START modules/orders

locales/en.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,8 @@ community_admin_help: |
574574
/setlanguage <lang> - Configure community's language for published messages.
575575
community_npub_updated: You added the community's pubkey ${npub} successfully!
576576
community_language_updated: You updated the community's language to ${language} successfully!
577-
wizard_community_enter_language: Enter the language code for your community (en, es, fr, de, it, pt, ru, uk, ko, fa)
578-
wizard_community_invalid_language: Invalid language code. Please enter one of the following - en, es, fr, de, it, pt, ru, uk, ko, fa
577+
wizard_community_enter_language: 'Enter the language code for your community (en, es, fr, de, it, pt, ru, uk, ko, fa)'
578+
wizard_community_invalid_language: 'Invalid language code. Please enter one of the following - en, es, fr, de, it, pt, ru, uk, ko, fa'
579579
language: Language
580580
# END modules/community
581581

locales/es.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,8 @@ community_admin_help: |
569569
/setlanguage <lang> - Configura el idioma de la comunidad para los mensajes publicados.
570570
community_npub_updated: Has configurado la pubkey ${npub} de la comunidad exitosamente!
571571
community_language_updated: Has configurado el idioma ${language} de la comunidad exitosamente!
572-
wizard_community_enter_language: Ingresa el idioma de la comunidad, ej: es, en, pt, fr, de, it, etc.
573-
wizard_community_invalid_language: El idioma debe ser uno de los siguientes: es, en, pt, fr, de, it, etc.
572+
wizard_community_enter_language: 'Ingresa el idioma de la comunidad, ej: es, en, pt, fr, de, it, etc.'
573+
wizard_community_invalid_language: 'El idioma debe ser uno de los siguientes: es, en, pt, fr, de, it, etc.'
574574
# END modules/community
575575

576576
# START modules/nostr

locales/fa.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ community_admin_help: |
567567
/setnpub <npub> - کلید عمومی جامعه را پیکربندی کنید. رویدادهای ناستر با این کلید عمومی برچسب‌گذاری می‌شوند.
568568
/setlanguage <lang> - Configure community's language for published messages.
569569
community_language_updated: You updated the community's language to ${language} successfully!
570-
wizard_community_enter_language: Enter the language code for your community (en, es, fr, de, it, pt, ru, uk, ko, fa)
571-
wizard_community_invalid_language: Invalid language code. Please enter one of the following - en, es, fr, de, it, pt, ru, uk, ko, fa
570+
wizard_community_enter_language: 'Enter the language code for your community (en, es, fr, de, it, pt, ru, uk, ko, fa)'
571+
wizard_community_invalid_language: 'Invalid language code. Please enter one of the following - en, es, fr, de, it, pt, ru, uk, ko, fa'
572572
community_npub_updated: شما کلیدعمومی کامیونیتی ${npub} را با موفقیت اضافه کردید!
573573
# END modules/community
574574

locales/fr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ community_admin_help: |
566566
/setnpub <npub> - Configurer la clé publique de la communauté.
567567
/setlanguage <lang> - Configurer la langue de la communauté pour les messages publiés.
568568
community_language_updated: Vous avez configuré la langue de la communauté en ${language} avec succès !
569-
wizard_community_enter_language: Entrez le code de la langue de votre communauté (en, es, fr, de, it, pt, ru, uk, ko, fa)
570-
wizard_community_invalid_language: Code de langue invalide. Veuillez entrer l'un des suivants - en, es, fr, de, it, pt, ru, uk, ko, fa
569+
wizard_community_enter_language: 'Entrez le code de la langue de votre communauté (en, es, fr, de, it, pt, ru, uk, ko, fa)'
570+
wizard_community_invalid_language: "Code de langue invalide. Veuillez entrer l'un des suivants - en, es, fr, de, it, pt, ru, uk, ko, fa"
571571
community_npub_updated: Vous avez ajouté la clé publique de la communauté ${npub} avec succès !
572572
# END modules/community
573573

locales/it.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,8 @@ community_admin_help: |
581581
/setnpub <npub> - Configure community's public key.
582582
/setlanguage <lang> - Configure community's language for published messages.
583583
community_language_updated: You updated the community's language to ${language} successfully!
584-
wizard_community_enter_language: Enter the language code for your community (en, es, fr, de, it, pt, ru, uk, ko, fa)
585-
wizard_community_invalid_language: Invalid language code. Please enter one of the following - en, es, fr, de, it, pt, ru, uk, ko, fa
584+
wizard_community_enter_language: 'Enter the language code for your community (en, es, fr, de, it, pt, ru, uk, ko, fa)'
585+
wizard_community_invalid_language: 'Invalid language code. Please enter one of the following - en, es, fr, de, it, pt, ru, uk, ko, fa'
586586
community_npub_updated: You added the community's pubkey ${npub} successfully!
587587
# END modules/community
588588

0 commit comments

Comments
 (0)