Skip to content

Commit

Permalink
bot/middleware: convert to TS (#632)
Browse files Browse the repository at this point in the history
Convert bot/middleware to TypeScript.
  • Loading branch information
webwarrior-ws authored Feb 21, 2025
1 parent e8c973c commit 29a6f37
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
8 changes: 5 additions & 3 deletions bot/middleware/commands.js → bot/middleware/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const commandArgs = () => (ctx, next) => {
import { CommunityContext } from "../modules/community/communityContext";

const commandArgs = () => (ctx: CommunityContext, next: () => void) => {
if (ctx.message && ctx.message.text) {
const text = ctx.message.text;
if (text.startsWith('/')) {
Expand All @@ -12,7 +14,7 @@ const commandArgs = () => (ctx, next) => {
command = match[1];
}
let next_arg = ['', '', match[2]];
while ((next_arg = re_next_arg.exec(next_arg[2]))) {
while ((next_arg = re_next_arg.exec(next_arg[2])!)) {
let quoted_arg = next_arg[1];
let unquoted_arg = '';
while (quoted_arg.length > 0) {
Expand Down Expand Up @@ -49,4 +51,4 @@ const commandArgs = () => (ctx, next) => {
return next();
};

module.exports = commandArgs;
export default commandArgs;
15 changes: 0 additions & 15 deletions bot/middleware/index.js

This file was deleted.

11 changes: 11 additions & 0 deletions bot/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import commandArgsMiddleware from './commands';
import { stageMiddleware } from './stage';
import { userMiddleware, adminMiddleware, superAdminMiddleware } from './user';

export {
commandArgsMiddleware,
stageMiddleware,
userMiddleware,
adminMiddleware,
superAdminMiddleware,
};
13 changes: 7 additions & 6 deletions bot/middleware/stage.js → bot/middleware/stage.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// @ts-check
const { Scenes } = require('telegraf');
const CommunityModule = require('../modules/community');
import { Scenes } from 'telegraf';
import * as CommunityModule from '../modules/community';
const OrdersModule = require('../modules/orders');
const UserModule = require('../modules/user');
import * as UserModule from '../modules/user';
import { CommunityContext } from '../modules/community/communityContext';
const {
addInvoiceWizard,
addFiatAmountWizard,
addInvoicePHIWizard,
} = require('../scenes');

exports.stageMiddleware = () => {
export const stageMiddleware = () => {
const scenes = [
addInvoiceWizard,
addFiatAmountWizard,
Expand All @@ -34,8 +35,8 @@ exports.stageMiddleware = () => {
return stage.middleware();
};

function addGenericCommands(scene) {
scene.command('exit', async ctx => {
function addGenericCommands(scene: Scenes.WizardScene<CommunityContext>) {
scene.command('exit', async (ctx) => {
await ctx.scene.leave();
const text = ctx.i18n.t('wizard_exit');
await ctx.reply(text);
Expand Down
14 changes: 6 additions & 8 deletions bot/middleware/user.js → bot/middleware/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const {
validateUser,
validateAdmin,
validateSuperAdmin,
} = require('../validations');
import { MainContext } from "../start";

exports.userMiddleware = async (ctx, next) => {
import { validateUser, validateAdmin, validateSuperAdmin } from '../validations';

export const userMiddleware = async (ctx: MainContext, next: () => void) => {
const user = await validateUser(ctx, false);
if (!user) return false;
ctx.i18n.locale(user.lang);
Expand All @@ -13,7 +11,7 @@ exports.userMiddleware = async (ctx, next) => {
next();
};

exports.adminMiddleware = async (ctx, next) => {
export const adminMiddleware = async (ctx: MainContext, next: () => void) => {
const admin = await validateAdmin(ctx);
if (!admin) return false;
ctx.i18n.locale(admin.lang);
Expand All @@ -22,7 +20,7 @@ exports.adminMiddleware = async (ctx, next) => {
next();
};

exports.superAdminMiddleware = async (ctx, next) => {
export const superAdminMiddleware = async (ctx: MainContext, next: () => void) => {
const admin = await validateSuperAdmin(ctx);
if (!admin) return false;
ctx.i18n.locale(admin.lang);
Expand Down

0 comments on commit 29a6f37

Please sign in to comment.