Skip to content

Commit

Permalink
bot/modules/user: convert to TS (#631)
Browse files Browse the repository at this point in the history
Convert bot/modules/user to TypeScript.
  • Loading branch information
webwarrior-ws authored Feb 18, 2025
1 parent 3e4ac23 commit e8c973c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 36 deletions.
2 changes: 2 additions & 0 deletions bot/modules/community/communityContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface CommunityWizardState {
bot: Telegraf<CommunityContext>;
message: Message.TextMessage | undefined;
error?: any;
feedback?: any;
language: string;
updateUI: (() => Promise<void>);
handler?: ((ctx: CommunityContext) => Promise<any>);
}
Expand Down
12 changes: 9 additions & 3 deletions bot/modules/user/index.js → bot/modules/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// @ts-check
const { userMiddleware } = require('../../middleware/user');

const Scenes = (exports.Scenes = require('./scenes'));
import { Telegraf } from 'telegraf';
import Scenes from './scenes';
import { CommunityContext } from '../community/communityContext';

exports.configure = bot => {
export const configure = (bot: Telegraf<CommunityContext>) => {
bot.command('/settings', userMiddleware, async ctx => {
try {
const { user } = ctx;
await ctx.scene.enter(Scenes.Settings.id, { user });
} catch (err) {
} catch (err: any) {
ctx.reply(err.message);
}
});
};

export {
Scenes
}
1 change: 0 additions & 1 deletion bot/modules/user/scenes/index.js

This file was deleted.

3 changes: 3 additions & 0 deletions bot/modules/user/scenes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Settings from './settings';

export default { Settings }
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
const { Scenes } = require('telegraf');
const { Community } = require('../../../../models');
const { getLanguageFlag } = require('../../../../util');
const NostrLib = require('../../nostr/lib');
import { Scenes } from 'telegraf';
import { Community } from '../../../../models';
import { getLanguageFlag } from '../../../../util';
import * as NostrLib from '../../nostr/lib';
import { CommunityContext, CommunityWizardState } from '../../community/communityContext';
import { Message } from 'telegraf/typings/core/types/typegram';

function make() {
const resetMessage = async (ctx, next) => {
delete ctx.scene.state.feedback;
delete ctx.scene.state.error;
const resetMessage = async (ctx: CommunityContext, next: () => void) => {
const state = ctx.scene.state as CommunityWizardState;
delete state.feedback;
delete state.error;
next();
};
async function mainData(ctx) {
const { user } = ctx.scene.state;
async function mainData(ctx: CommunityContext) {
const state = ctx.scene.state as CommunityWizardState;
const { user } = state;
const data = {
user,
language: getLanguageFlag(ctx.scene.state.language),
language: getLanguageFlag(state.language),
npub: '',
community: '',
lightning_address: '',
};
if (user.default_community_id) {
const community = await Community.findById(user.default_community_id);
if(community == null)
throw new Error("community not found")
data.community = community.group;
}
if (user.nostr_public_key) {
Expand All @@ -32,10 +38,14 @@ function make() {

return data;
}
async function updateMessage(ctx) {
async function updateMessage(ctx: CommunityContext) {
try {
ctx.i18n.locale(ctx.scene.state.language); // i18n locale resets if user executes unknown action
const { message, error } = ctx.scene.state;
const state = ctx.scene.state as CommunityWizardState;
ctx.i18n.locale(state.language); // i18n locale resets if user executes unknown action
const { message, error } = state;

if(message === undefined)
throw new Error("message is undefined");

const errorText = (error => {
if (!error) return;
Expand All @@ -45,7 +55,7 @@ function make() {
if (!feedback) return;
if (typeof feedback === 'string') return feedback;
return ctx.i18n.t(feedback.i18n, feedback);
})(ctx.scene.state.feedback);
})(state.feedback);
const extras = [errorText, feedbackText].filter(e => e);

const main = ctx.i18n.t('user_settings', await mainData(ctx));
Expand All @@ -57,30 +67,31 @@ function make() {
const msg = await ctx.telegram.editMessageText(
message.chat.id,
message.message_id,
null,
undefined,
str,
{
parse_mode: 'HTML',
disable_web_page_preview: true,
}
link_preview_options: { is_disabled: true },
} as any
);
ctx.scene.state.message = msg;
ctx.scene.state.message.text = str;
state.message = msg as Message.TextMessage;
state.message.text = str;
} catch (err) {}
}
async function initHandler(ctx) {
async function initHandler(ctx: CommunityContext) {
try {
const { user } = ctx.scene.state;
ctx.scene.state.language = user.lang || ctx.from?.language_code;
const state = ctx.scene.state as CommunityWizardState;
const { user } = state;
state.language = user.lang || ctx.from?.language_code;
const str = ctx.i18n.t('user_settings', await mainData(ctx));
const msg = await ctx.reply(str, { parse_mode: 'HTML' });
ctx.scene.state.message = msg;
ctx.scene.state.message.text = str;
state.message = msg;
state.message.text = str;
} catch (err) {}
}
const scene = new Scenes.WizardScene('USER_SETTINGS', async ctx => {
ctx.user = ctx.scene.state.user;
const { state } = ctx.scene;
const scene = new Scenes.WizardScene('USER_SETTINGS', async (ctx: CommunityContext) => {
const state = ctx.scene.state as CommunityWizardState;
ctx.user = state.user;
if (!state.message) return initHandler(ctx);
await ctx.deleteMessage();
state.error = {
Expand All @@ -89,22 +100,25 @@ function make() {
await updateMessage(ctx);
});

scene.command('/setnpub', resetMessage, async ctx => {
scene.command('/setnpub', resetMessage, async (ctx: CommunityContext) => {
try {
await ctx.deleteMessage();
const state = ctx.scene.state as CommunityWizardState;
if(ctx.message === undefined)
throw new Error("ctx.message is undefined");
const [, npub] = ctx.message.text.trim().split(' ');
const hex = NostrLib.decodeNpub(npub);
if (!hex) throw new Error('NpubNotValid');
const user = ctx.scene.state.user;
const user = state.user;
user.nostr_public_key = hex;
await user.save();
ctx.scene.state.feedback = {
state.feedback = {
i18n: 'user_npub_updated',
npub,
};
await updateMessage(ctx);
} catch (err) {
ctx.scene.state.error = {
(ctx.scene.state as CommunityWizardState).error = {
i18n: 'npub_not_valid',
};
await updateMessage(ctx);
Expand All @@ -114,4 +128,4 @@ function make() {
return scene;
}

module.exports = make();
export default make();

0 comments on commit e8c973c

Please sign in to comment.