From f6f00d0c2fb7b7afb5f395273f09fe358be5c44d Mon Sep 17 00:00:00 2001 From: webwarrior Date: Mon, 12 Aug 2024 11:57:17 +0200 Subject: [PATCH 1/3] bot, jobs: convert requires to imports Converted requires to imports where applicable and fixed type errors resulting from it. --- app.ts | 7 +- bot/commands.ts | 31 +++-- bot/index.ts | 20 +-- bot/messages.ts | 75 ++++++----- bot/middleware/stage.ts | 8 +- bot/middleware/user.ts | 8 +- bot/modules/community/index.ts | 2 +- .../community/scenes.communityAdmin.ts | 2 +- bot/modules/community/scenes.ts | 2 +- bot/modules/dispute/actions.ts | 2 +- bot/modules/dispute/commands.ts | 3 +- bot/modules/dispute/index.ts | 6 +- bot/modules/language/index.ts | 5 +- bot/modules/nostr/index.ts | 6 +- bot/modules/orders/index.ts | 1 + bot/modules/orders/takeOrder.ts | 2 +- bot/modules/user/index.ts | 2 +- bot/ordersActions.ts | 2 +- bot/scenes.ts | 6 +- bot/start.ts | 125 +++++++++--------- bot/validations.ts | 4 +- jobs/cancel_orders.ts | 13 +- jobs/communities.ts | 4 +- jobs/delete_published_orders.ts | 9 +- jobs/node_info.ts | 6 +- jobs/pending_payments.ts | 9 +- 26 files changed, 183 insertions(+), 177 deletions(-) diff --git a/app.ts b/app.ts index 4855e257..37d2527d 100644 --- a/app.ts +++ b/app.ts @@ -1,11 +1,12 @@ import "dotenv/config"; import { SocksProxyAgent } from "socks-proxy-agent"; -import { MainContext, start } from "./bot/start"; +import { start } from "./bot/start"; import { connect as mongoConnect } from './db_connect' const { resubscribeInvoices } = require('./ln'); import { logger } from "./logger"; import { Telegraf } from "telegraf"; -const { delay } = require('./util'); +import { delay } from './util'; +import { CommunityContext } from "./bot/modules/community/communityContext"; (async () => { process.on('unhandledRejection', e => { @@ -24,7 +25,7 @@ const { delay } = require('./util'); mongoose.connection .once('open', async () => { logger.info('Connected to Mongo instance.'); - let options: Partial> = { handlerTimeout: 60000 }; + let options: Partial> = { handlerTimeout: 60000 }; if (process.env.SOCKS_PROXY_HOST) { const agent = new SocksProxyAgent(process.env.SOCKS_PROXY_HOST); options = { diff --git a/bot/commands.ts b/bot/commands.ts index 71d9ded8..cd16cef9 100644 --- a/bot/commands.ts +++ b/bot/commands.ts @@ -1,27 +1,26 @@ import { validateFiatSentOrder, validateReleaseOrder } from './validations'; -const { +import { createHoldInvoice, subscribeInvoice, cancelHoldInvoice, settleHoldInvoice, -} = require('../ln'); +} from '../ln'; import { Order, User, Dispute } from '../models'; import * as messages from './messages'; -import { getBtcFiatPrice, deleteOrderFromChannel, getUserI18nContext, getFee } from '../util'; +import { getBtcFiatPrice, deleteOrderFromChannel, getUserI18nContext, getFee, removeLightningPrefix } from '../util'; import * as ordersActions from './ordersActions'; -const OrderEvents = require('./modules/events/orders'); -const { removeLightningPrefix } = require('../util'); +import * as OrderEvents from './modules/events/orders'; import { resolvLightningAddress } from '../lnurl/lnurl-pay'; import { logger } from '../logger'; import { Telegraf } from 'telegraf'; import { IOrder } from '../models/order'; import { UserDocument } from '../models/user'; -import { MainContext } from './start'; +import { HasTelegram, MainContext } from './start'; import { CommunityContext } from './modules/community/communityContext'; import { Types } from 'mongoose'; -const waitPayment = async (ctx: MainContext, bot: MainContext, buyer: UserDocument, seller: UserDocument, order: IOrder, buyerInvoice: any) => { +const waitPayment = async (ctx: MainContext, bot: HasTelegram, buyer: UserDocument, seller: UserDocument, order: IOrder, buyerInvoice: any) => { try { // If there is not fiat amount the function don't do anything if (order.fiat_amount === undefined) { @@ -88,7 +87,7 @@ const waitPayment = async (ctx: MainContext, bot: MainContext, buyer: UserDocume } }; -const addInvoice = async (ctx: CommunityContext, bot: MainContext, order: IOrder | null) => { +const addInvoice = async (ctx: CommunityContext, bot: HasTelegram, order: IOrder | null = null) => { try { ctx.deleteMessage(); ctx.scene.leave(); @@ -177,7 +176,7 @@ const addInvoice = async (ctx: CommunityContext, bot: MainContext, order: IOrder } }; -const rateUser = async (ctx: CommunityContext, bot: MainContext, rating: number, orderId: string) => { +const rateUser = async (ctx: CommunityContext, bot: HasTelegram, rating: number, orderId: string) => { try { ctx.deleteMessage(); ctx.scene.leave(); @@ -240,7 +239,7 @@ const saveUserReview = async (targetUser: UserDocument, rating: number) => { } }; -const cancelAddInvoice = async (ctx: CommunityContext, order: IOrder | null, job?: any) => { +const cancelAddInvoice = async (ctx: CommunityContext, order: IOrder | null = null, job?: any) => { try { let userAction = false; let userTgId = null; @@ -357,7 +356,7 @@ const cancelAddInvoice = async (ctx: CommunityContext, order: IOrder | null, job } }; -const showHoldInvoice = async (ctx: CommunityContext, bot: MainContext, order: IOrder | null) => { +const showHoldInvoice = async (ctx: CommunityContext, bot: HasTelegram, order: IOrder | null = null) => { try { ctx.deleteMessage(); if (!order) { @@ -427,7 +426,7 @@ const showHoldInvoice = async (ctx: CommunityContext, bot: MainContext, order: I } }; -const cancelShowHoldInvoice = async (ctx: CommunityContext, order: IOrder | null, job?: any) => { +const cancelShowHoldInvoice = async (ctx: CommunityContext, order: IOrder | null = null, job?: any) => { try { let userAction = false; let userTgId = null; @@ -550,7 +549,7 @@ const cancelShowHoldInvoice = async (ctx: CommunityContext, order: IOrder | null * @param {*} order * @returns */ -const addInvoicePHI = async (ctx: CommunityContext, bot: MainContext, orderId: string) => { +const addInvoicePHI = async (ctx: CommunityContext, bot: HasTelegram, orderId: string) => { try { ctx.deleteMessage(); const order = await Order.findOne({ _id: orderId }); @@ -574,7 +573,7 @@ const addInvoicePHI = async (ctx: CommunityContext, bot: MainContext, orderId: s } }; -const cancelOrder = async (ctx: CommunityContext, orderId: string, user: UserDocument | null) => { +const cancelOrder = async (ctx: CommunityContext, orderId: string, user: UserDocument | null = null) => { try { if (user === null) { const tgUser = (ctx.update as any).callback_query.from; @@ -700,7 +699,7 @@ const cancelOrder = async (ctx: CommunityContext, orderId: string, user: UserDoc } }; -const fiatSent = async (ctx: MainContext, orderId: string, user: UserDocument | null) => { +const fiatSent = async (ctx: MainContext, orderId: string, user: UserDocument | null = null) => { try { if (!user) { const tgUser = (ctx.update as any).callback_query.from; @@ -736,7 +735,7 @@ const fiatSent = async (ctx: MainContext, orderId: string, user: UserDocument | } }; -const release = async (ctx: MainContext, orderId: string, user: UserDocument | null) => { +const release = async (ctx: MainContext, orderId: string, user: UserDocument | null = null) => { try { if (!user) { const tgUser = (ctx.update as any).callback_query.from; diff --git a/bot/index.ts b/bot/index.ts index b4d84c05..ac8a9a7d 100644 --- a/bot/index.ts +++ b/bot/index.ts @@ -1,10 +1,6 @@ -const { initialize, start } = require('./start'); -const { - createOrder, - getOrder, - getNewRangeOrderPayload, -} = require('./ordersActions'); -const { +import { initialize, start } from './start'; +import { createOrder, getOrder, getNewRangeOrderPayload } from './ordersActions'; +import { validateSellOrder, validateBuyOrder, validateUser, @@ -13,8 +9,8 @@ const { validateTakeBuyOrder, validateReleaseOrder, validateDisputeOrder, -} = require('./validations'); -const { +} from './validations'; +import { startMessage, initBotErrorMessage, invoicePaymentRequestMessage, @@ -36,12 +32,11 @@ const { onGoingTakeBuyMessage, pendingSellMessage, pendingBuyMessage, - beginDisputeMessage, notOrderMessage, customMessage, -} = require('./messages'); +} from './messages'; -module.exports = { +export { initialize, start, createOrder, @@ -75,7 +70,6 @@ module.exports = { onGoingTakeBuyMessage, pendingSellMessage, pendingBuyMessage, - beginDisputeMessage, notOrderMessage, customMessage, getNewRangeOrderPayload, diff --git a/bot/messages.ts b/bot/messages.ts index 0515f91f..cf7c5c73 100644 --- a/bot/messages.ts +++ b/bot/messages.ts @@ -1,6 +1,6 @@ import { TelegramError } from 'telegraf' import QR from 'qrcode'; -const { +import { getCurrency, numberFormat, getDetailedOrder, @@ -12,8 +12,8 @@ const { decimalRound, getUserAge, getStars, -} = require('../util'); -const OrderEvents = require('./modules/events/orders'); +} from '../util'; +import * as OrderEvents from './modules/events/orders'; import { logger } from "../logger"; import { HasTelegram, MainContext } from './start'; import { UserDocument } from '../models/user' @@ -25,6 +25,7 @@ import { IPendingPayment } from '../models/pending_payment'; import { PayViaPaymentRequestResult } from 'lightning'; import { IFiat } from '../util/fiatModel'; import { generateQRWithImage } from '../util'; +import { CommunityContext } from './modules/community/communityContext'; const startMessage = async (ctx: MainContext) => { try { @@ -66,10 +67,10 @@ const invoicePaymentRequestMessage = async ( buyer: UserDocument ) => { try { - let currency = getCurrency(order.fiat_code); - currency = - !!currency && !!currency.symbol_native - ? currency.symbol_native + const currencyObject = getCurrency(order.fiat_code); + const currency = + !!currencyObject && !!currencyObject.symbol_native + ? currencyObject.symbol_native : order.fiat_code; const expirationTime = Number(process.env.HOLD_INVOICE_EXPIRATION_WINDOW) / 60; @@ -202,7 +203,7 @@ const expiredInvoiceMessage = async (ctx: MainContext) => { } }; -const expiredInvoiceOnPendingMessage = async (bot: Telegraf, user: UserDocument, order: IOrder, i18n: I18nContext) => { +const expiredInvoiceOnPendingMessage = async (bot: Telegraf, user: UserDocument, order: IOrder, i18n: I18nContext) => { try { await bot.telegram.sendMessage(user.tg_id, i18n.t('invoice_expired_long')); await bot.telegram.sendMessage( @@ -305,7 +306,7 @@ const alreadyTakenOrderMessage = async (ctx: MainContext, bot: HasTelegram, user } }; -const invalidDataMessage = async (ctx: MainContext, bot: MainContext, user: UserDocument) => { +const invalidDataMessage = async (ctx: MainContext, bot: HasTelegram, user: UserDocument) => { try { await bot.telegram.sendMessage(user.tg_id, ctx.i18n.t('invalid_data')); } catch (error) { @@ -313,7 +314,7 @@ const invalidDataMessage = async (ctx: MainContext, bot: MainContext, user: User } }; -const genericErrorMessage = async (bot: MainContext, user: UserDocument, i18n: I18nContext) => { +const genericErrorMessage = async (bot: HasTelegram, user: UserDocument, i18n: I18nContext) => { try { await bot.telegram.sendMessage(user.tg_id, i18n.t('generic_error')); } catch (error) { @@ -363,16 +364,16 @@ const showHoldInvoiceMessage = async ( randomImage: IOrder["random_image"] ) => { try { - let currency = getCurrency(fiatCode); - currency = - !!currency && !!currency.symbol_native - ? currency.symbol_native + const currencyObject = getCurrency(fiatCode); + const currency = + !!currencyObject && !!currencyObject.symbol_native + ? currencyObject.symbol_native : fiatCode; await ctx.reply( ctx.i18n.t('pay_invoice', { amount: numberFormat(fiatCode, amount), - fiatAmount: numberFormat(fiatCode, fiatAmount), + fiatAmount: numberFormat(fiatCode, fiatAmount!), currency, }) ); @@ -486,7 +487,7 @@ const onGoingTakeSellMessage = async ( orderId: order.id, currency: order.fiat_code, sellerUsername: sellerUser.username, - fiatAmount: numberFormat(order.fiat_code, order.fiat_amount), + fiatAmount: numberFormat(order.fiat_code, order.fiat_amount!), paymentMethod: order.payment_method, }) ); @@ -512,7 +513,7 @@ const onGoingTakeSellMessage = async ( const takeSellWaitingSellerToPayMessage = async ( ctx: MainContext, - bot: MainContext, + bot: HasTelegram, buyerUser: UserDocument, order: IOrder ) => { @@ -547,7 +548,7 @@ const releasedSatsMessage = async ( } }; -const rateUserMessage = async (bot: Telegraf, caller: UserDocument, order: IOrder, i18n: I18nContext) => { +const rateUserMessage = async (bot: Telegraf, caller: UserDocument, order: IOrder, i18n: I18nContext) => { try { const starButtons = []; for (let num = 5; num > 0; num--) { @@ -604,6 +605,8 @@ const publishBuyOrderMessage = async ( publishMessage += `:${order._id}:`; const channel = await getOrderChannel(order); + if (channel === undefined) + throw new Error("channel is undefined"); // We send the message to the channel const message1 = await bot.telegram.sendMessage(channel, publishMessage, { reply_markup: { @@ -639,6 +642,8 @@ const publishSellOrderMessage = async ( let publishMessage = `⚡️🍊⚡️\n${order.description}\n`; publishMessage += `:${order._id}:`; const channel = await getOrderChannel(order); + if (channel === undefined) + throw new Error("channel is undefined"); // We send the message to the channel const message1 = await ctx.telegram.sendMessage(channel, publishMessage, { reply_markup: { @@ -673,6 +678,8 @@ const customMessage = async (ctx: MainContext, message: string) => { const checkOrderMessage = async (ctx: MainContext, order: IOrder, buyer: UserDocument | null, seller: UserDocument | null) => { try { let message = getDetailedOrder(ctx.i18n, order, buyer, seller); + if (message === undefined) + throw new Error("message is undefined"); message += `\n\n`; await ctx.reply(message, { parse_mode: 'MarkdownV2' }); } catch (error) { @@ -845,7 +852,7 @@ const notValidIdMessage = async (ctx: MainContext) => { } }; -const addInvoiceMessage = async (ctx: MainContext, bot: MainContext, buyer: UserDocument, seller: UserDocument, order: IOrder) => { +const addInvoiceMessage = async (ctx: MainContext, bot: HasTelegram, buyer: UserDocument, seller: UserDocument, order: IOrder) => { try { await bot.telegram.sendMessage( buyer.tg_id, @@ -853,7 +860,7 @@ const addInvoiceMessage = async (ctx: MainContext, bot: MainContext, buyer: User orderId: order.id, currency: order.fiat_code, sellerUsername: seller.username, - fiatAmount: numberFormat(order.fiat_code, order.fiat_amount), + fiatAmount: numberFormat(order.fiat_code, order.fiat_amount!), paymentMethod: order.payment_method, }) ); @@ -867,7 +874,7 @@ const addInvoiceMessage = async (ctx: MainContext, bot: MainContext, buyer: User } }; -const sendBuyerInfo2SellerMessage = async (bot: MainContext, buyer: UserDocument, seller: UserDocument, order: IOrder, i18n: I18nContext) => { +const sendBuyerInfo2SellerMessage = async (bot: HasTelegram, buyer: UserDocument, seller: UserDocument, order: IOrder, i18n: I18nContext) => { try { await bot.telegram.sendMessage( seller.tg_id, @@ -875,7 +882,7 @@ const sendBuyerInfo2SellerMessage = async (bot: MainContext, buyer: UserDocument currency: order.fiat_code, orderId: order.id, buyerUsername: buyer.username, - fiatAmount: numberFormat(order.fiat_code, order.fiat_amount), + fiatAmount: numberFormat(order.fiat_code, order.fiat_amount!), paymentMethod: order.payment_method, }) ); @@ -1013,7 +1020,7 @@ const successCancelAllOrdersMessage = async (ctx: MainContext) => { } }; -const successCancelOrderByAdminMessage = async (ctx: MainContext, bot: Telegraf, user: UserDocument, order: IOrder) => { +const successCancelOrderByAdminMessage = async (ctx: MainContext, bot: Telegraf, user: UserDocument, order: IOrder) => { try { await bot.telegram.sendMessage( user.tg_id, @@ -1032,7 +1039,7 @@ const successCompleteOrderMessage = async (ctx: MainContext, order: IOrder) => { } }; -const successCompleteOrderByAdminMessage = async (ctx: MainContext, bot: Telegraf, user: UserDocument, order: IOrder) => { +const successCompleteOrderByAdminMessage = async (ctx: MainContext, bot: HasTelegram, user: UserDocument, order: IOrder) => { try { await bot.telegram.sendMessage( user.tg_id, @@ -1197,7 +1204,7 @@ const listCurrenciesResponse = async (ctx: MainContext, currencies: Array } }; -const priceApiFailedMessage = async (ctx: MainContext, bot: MainContext, user: UserDocument) => { +const priceApiFailedMessage = async (ctx: MainContext, bot: HasTelegram, user: UserDocument) => { try { await bot.telegram.sendMessage( user.tg_id, @@ -1257,7 +1264,7 @@ const wizardAddInvoiceInitMessage = async ( expirationTime, satsAmount: numberFormat(order.fiat_code, order.amount), currency, - fiatAmount: numberFormat(order.fiat_code, order.fiat_amount), + fiatAmount: numberFormat(order.fiat_code, order.fiat_amount!), }) ); } catch (error) { @@ -1317,7 +1324,7 @@ const wizardAddFiatAmountMessage = async (ctx: MainContext, currency: string, ac ctx.i18n.t('wizard_add_fiat_amount', { action, currency, - fiatAmount: numberFormat(order.fiat_code, order.fiat_amount), + fiatAmount: numberFormat(order.fiat_code, order.fiat_amount!), minAmount: numberFormat(order.fiat_code, order.min_amount), maxAmount: numberFormat(order.fiat_code, order.max_amount), }) @@ -1354,7 +1361,7 @@ const wizardAddFiatAmountCorrectMessage = async (ctx: MainContext, currency: IFi } }; -const expiredOrderMessage = async (bot: Telegraf, order: IOrder, buyerUser: UserDocument, sellerUser: UserDocument, i18n: I18nContext) => { +const expiredOrderMessage = async (bot: HasTelegram, order: IOrder, buyerUser: UserDocument, sellerUser: UserDocument, i18n: I18nContext) => { try { const detailedOrder = getDetailedOrder(i18n, order, buyerUser, sellerUser); await bot.telegram.sendMessage( @@ -1371,7 +1378,7 @@ const expiredOrderMessage = async (bot: Telegraf, order: IOrder, bu } }; -const toBuyerExpiredOrderMessage = async (bot: Telegraf, user: UserDocument, i18n: I18nContext) => { +const toBuyerExpiredOrderMessage = async (bot: HasTelegram, user: UserDocument, i18n: I18nContext) => { try { await bot.telegram.sendMessage( user.tg_id, @@ -1382,7 +1389,7 @@ const toBuyerExpiredOrderMessage = async (bot: Telegraf, user: User } }; -const toSellerExpiredOrderMessage = async (bot: Telegraf, user: UserDocument, i18n: I18nContext) => { +const toSellerExpiredOrderMessage = async (bot: HasTelegram, user: UserDocument, i18n: I18nContext) => { try { await bot.telegram.sendMessage( user.tg_id, @@ -1476,7 +1483,7 @@ const toAdminChannelSellerDidntPayInvoiceMessage = async ( }; const toAdminChannelPendingPaymentSuccessMessage = async ( - bot: Telegraf, + bot: Telegraf, user: UserDocument, order: IOrder, pending: IPendingPayment, @@ -1500,7 +1507,7 @@ const toAdminChannelPendingPaymentSuccessMessage = async ( }; const toBuyerPendingPaymentSuccessMessage = async ( - bot: Telegraf, + bot: Telegraf, user: UserDocument, order: IOrder, payment: PayViaPaymentRequestResult, @@ -1520,7 +1527,7 @@ const toBuyerPendingPaymentSuccessMessage = async ( } }; -const toBuyerPendingPaymentFailedMessage = async (bot: Telegraf, user: UserDocument, order: IOrder, i18n: I18nContext) => { +const toBuyerPendingPaymentFailedMessage = async (bot: Telegraf, user: UserDocument, order: IOrder, i18n: I18nContext) => { try { const attempts = process.env.PAYMENT_ATTEMPTS; await bot.telegram.sendMessage( @@ -1547,7 +1554,7 @@ const toBuyerPendingPaymentFailedMessage = async (bot: Telegraf, us }; const toAdminChannelPendingPaymentFailedMessage = async ( - bot: Telegraf, + bot: Telegraf, user: UserDocument, order: IOrder, pending: IPendingPayment, diff --git a/bot/middleware/stage.ts b/bot/middleware/stage.ts index a1f90ce0..3a3baf16 100644 --- a/bot/middleware/stage.ts +++ b/bot/middleware/stage.ts @@ -1,14 +1,10 @@ // @ts-check import { Scenes } from 'telegraf'; import * as CommunityModule from '../modules/community'; -const OrdersModule = require('../modules/orders'); +import * as OrdersModule from '../modules/orders'; import * as UserModule from '../modules/user'; import { CommunityContext } from '../modules/community/communityContext'; -const { - addInvoiceWizard, - addFiatAmountWizard, - addInvoicePHIWizard, -} = require('../scenes'); +import { addInvoiceWizard, addFiatAmountWizard, addInvoicePHIWizard } from '../scenes'; export const stageMiddleware = () => { const scenes = [ diff --git a/bot/middleware/user.ts b/bot/middleware/user.ts index 03ce55ac..15ff3265 100644 --- a/bot/middleware/user.ts +++ b/bot/middleware/user.ts @@ -1,8 +1,8 @@ -import { MainContext } from "../start"; +import { CommunityContext } from "../modules/community/communityContext"; import { validateUser, validateAdmin, validateSuperAdmin } from '../validations'; -export const userMiddleware = async (ctx: MainContext, next: () => void) => { +export const userMiddleware = async (ctx: CommunityContext, next: () => void) => { const user = await validateUser(ctx, false); if (!user) return false; ctx.i18n.locale(user.lang); @@ -11,7 +11,7 @@ export const userMiddleware = async (ctx: MainContext, next: () => void) => { next(); }; -export const adminMiddleware = async (ctx: MainContext, next: () => void) => { +export const adminMiddleware = async (ctx: CommunityContext, next: () => void) => { const admin = await validateAdmin(ctx); if (!admin) return false; ctx.i18n.locale(admin.lang); @@ -20,7 +20,7 @@ export const adminMiddleware = async (ctx: MainContext, next: () => void) => { next(); }; -export const superAdminMiddleware = async (ctx: MainContext, next: () => void) => { +export const superAdminMiddleware = async (ctx: CommunityContext, next: () => void) => { const admin = await validateSuperAdmin(ctx); if (!admin) return false; ctx.i18n.locale(admin.lang); diff --git a/bot/modules/community/index.ts b/bot/modules/community/index.ts index 048faf82..94ddd861 100644 --- a/bot/modules/community/index.ts +++ b/bot/modules/community/index.ts @@ -1,6 +1,6 @@ // @ts-check import { Telegraf } from 'telegraf'; -const { userMiddleware } = require('../../middleware/user'); +import { userMiddleware } from '../../middleware/user'; import * as actions from './actions'; import * as commands from './commands'; import { earningsMessage, updateCommunityMessage, sureMessage } from './messages'; diff --git a/bot/modules/community/scenes.communityAdmin.ts b/bot/modules/community/scenes.communityAdmin.ts index 02c65ec2..c13e22ce 100644 --- a/bot/modules/community/scenes.communityAdmin.ts +++ b/bot/modules/community/scenes.communityAdmin.ts @@ -1,7 +1,7 @@ import { Scenes } from 'telegraf'; import { CommunityContext } from './communityContext'; -const CommunityEvents = require('../events/community'); +import * as CommunityEvents from '../events/community'; const communityAdmin = () => { const scene = new Scenes.WizardScene('COMMUNITY_ADMIN', async (ctx: CommunityContext) => { diff --git a/bot/modules/community/scenes.ts b/bot/modules/community/scenes.ts index 6cbb8bb6..2b3269e8 100644 --- a/bot/modules/community/scenes.ts +++ b/bot/modules/community/scenes.ts @@ -2,7 +2,7 @@ import { Scenes } from 'telegraf'; import { logger } from '../../../logger'; import { Community, User, PendingPayment } from '../../../models'; import { IOrderChannel, IUsernameId } from '../../../models/community'; -const { isPendingPayment } = require('../../../ln'); +import { isPendingPayment } from '../../../ln'; import { isGroupAdmin, itemsFromMessage, removeAtSymbol } from '../../../util'; import * as messages from '../../messages'; import { isValidInvoice } from '../../validations'; diff --git a/bot/modules/dispute/actions.ts b/bot/modules/dispute/actions.ts index 48e8d57b..59f68c4a 100644 --- a/bot/modules/dispute/actions.ts +++ b/bot/modules/dispute/actions.ts @@ -2,7 +2,7 @@ import { User, Order, Dispute } from '../../../models'; import { MainContext } from '../../start'; import * as messages from './messages'; import { validateAdmin } from '../../validations'; -const globalMessages = require('../../messages'); +import * as globalMessages from '../../messages'; export const takeDispute = async (ctx: MainContext) : Promise => { const tgId: string = (ctx.update as any).callback_query.from.id; diff --git a/bot/modules/dispute/commands.ts b/bot/modules/dispute/commands.ts index c0165412..6aebbb0c 100644 --- a/bot/modules/dispute/commands.ts +++ b/bot/modules/dispute/commands.ts @@ -3,8 +3,9 @@ import { MainContext } from "../../start"; import { User, Dispute, Order } from '../../../models'; import { validateParams, validateObjectId, validateDisputeOrder } from '../../validations'; import * as messages from './messages'; -const globalMessages = require('../../messages'); +import * as globalMessages from '../../messages'; import { logger } from '../../../logger'; +import * as OrderEvents from '../../modules/events/orders'; import { removeAtSymbol } from '../../../util'; const dispute = async (ctx: MainContext) => { diff --git a/bot/modules/dispute/index.ts b/bot/modules/dispute/index.ts index f1a1ea1b..e181110b 100644 --- a/bot/modules/dispute/index.ts +++ b/bot/modules/dispute/index.ts @@ -1,10 +1,10 @@ import * as commands from './commands'; import * as actions from './actions'; import { Telegraf } from 'telegraf'; -import { MainContext } from '../../start'; -const { userMiddleware, adminMiddleware } = require('../../middleware/user'); +import { userMiddleware, adminMiddleware } from '../../middleware/user'; +import { CommunityContext } from '../community/communityContext'; -export const configure = (bot: Telegraf) => { +export const configure = (bot: Telegraf) => { bot.command('dispute', userMiddleware, commands.dispute); bot.command('deldispute', adminMiddleware, commands.deleteDispute); bot.action( diff --git a/bot/modules/language/index.ts b/bot/modules/language/index.ts index 4c4292fe..b9de4924 100644 --- a/bot/modules/language/index.ts +++ b/bot/modules/language/index.ts @@ -1,10 +1,11 @@ -const { userMiddleware } = require('../../middleware/user'); +import { userMiddleware } from '../../middleware/user'; import * as commands from './commands'; import * as actions from './actions'; import { Telegraf } from 'telegraf'; import { MainContext } from '../../start'; +import { CommunityContext } from '../community/communityContext'; -exports.configure = (bot: Telegraf) => { +export const configure = (bot: Telegraf) => { bot.command('setlang', userMiddleware, ctx => commands.setlang(ctx as unknown as MainContext)); bot.action(/^setLanguage_([a-z]{2})$/, userMiddleware, ctx => actions.setLanguage(ctx as unknown as MainContext)); }; diff --git a/bot/modules/nostr/index.ts b/bot/modules/nostr/index.ts index 5a874f59..ac77106c 100644 --- a/bot/modules/nostr/index.ts +++ b/bot/modules/nostr/index.ts @@ -4,11 +4,11 @@ import * as Config from './config'; import { createOrderEvent } from './events'; import * as Commands from './commands'; import { Telegraf } from 'telegraf'; -import { MainContext } from '../../start'; import { IOrder } from '../../../models/order'; -const CommunityEvents = require('../events/community'); +import * as CommunityEvents from '../events/community'; +import { CommunityContext } from '../community/communityContext'; -export const configure = (bot: Telegraf) => { +export const configure = (bot: Telegraf) => { bot.command('/nostr', Commands.info); if (!Config.getRelays().length) { diff --git a/bot/modules/orders/index.ts b/bot/modules/orders/index.ts index 65fd4c99..b825bd4c 100644 --- a/bot/modules/orders/index.ts +++ b/bot/modules/orders/index.ts @@ -10,6 +10,7 @@ import { takeOrderActionValidation, takeOrderValidation, takesell, takebuyValida import { extractId } from '../../../util'; import { Telegraf } from 'telegraf'; import { CommunityContext } from '../community/communityContext'; +import { MainContext } from '../../start'; export * as Scenes from './scenes'; export const configure = (bot: Telegraf) => { diff --git a/bot/modules/orders/takeOrder.ts b/bot/modules/orders/takeOrder.ts index 0939e23f..ba8b8cce 100644 --- a/bot/modules/orders/takeOrder.ts +++ b/bot/modules/orders/takeOrder.ts @@ -6,7 +6,7 @@ import { deleteOrderFromChannel } from '../../../util'; import * as messages from '../../messages'; import { HasTelegram, MainContext } from '../../start'; import { validateUserWaitingOrder, isBannedFromCommunity, validateTakeSellOrder, validateSeller, validateObjectId, validateTakeBuyOrder } from '../../validations'; -const OrderEvents = require('../../modules/events/orders'); +import * as OrderEvents from '../../modules/events/orders'; export const takeOrderActionValidation = async (ctx: MainContext, next: () => void) => { try { diff --git a/bot/modules/user/index.ts b/bot/modules/user/index.ts index e6b412fc..0842196b 100644 --- a/bot/modules/user/index.ts +++ b/bot/modules/user/index.ts @@ -1,5 +1,5 @@ // @ts-check -const { userMiddleware } = require('../../middleware/user'); +import { userMiddleware } from '../../middleware/user'; import { Telegraf } from 'telegraf'; import Scenes from './scenes'; diff --git a/bot/ordersActions.ts b/bot/ordersActions.ts index c6c5014a..9256bfb1 100644 --- a/bot/ordersActions.ts +++ b/bot/ordersActions.ts @@ -9,7 +9,7 @@ import { MainContext } from './start'; import { IOrder } from '../models/order'; import { IFiat } from '../util/fiatModel'; -const OrderEvents = require('./modules/events/orders'); +import * as OrderEvents from './modules/events/orders'; interface CreateOrderArguments { type: string; diff --git a/bot/scenes.ts b/bot/scenes.ts index c88a0f2e..52f69747 100644 --- a/bot/scenes.ts +++ b/bot/scenes.ts @@ -1,15 +1,15 @@ import { Scenes } from 'telegraf'; -const { parsePaymentRequest } = require('invoices'); +import { parsePaymentRequest } from 'invoices'; import { isValidInvoice, validateLightningAddress } from './validations'; import { Order, PendingPayment } from '../models'; import { waitPayment, addInvoice, showHoldInvoice } from './commands'; import { getCurrency, getUserI18nContext } from '../util'; import * as messages from './messages'; -const { isPendingPayment } = require('../ln'); +import { isPendingPayment } from '../ln'; import { logger } from '../logger'; import { resolvLightningAddress } from '../lnurl/lnurl-pay'; import { CommunityContext } from './modules/community/communityContext'; -const OrderEvents = require('./modules/events/orders'); +import * as OrderEvents from './modules/events/orders'; interface InvoiceParseResult { invoice?: any; diff --git a/bot/start.ts b/bot/start.ts index 0532908d..1f918eaa 100644 --- a/bot/start.ts +++ b/bot/start.ts @@ -3,9 +3,9 @@ import { I18n, I18nContext } from '@grammyjs/i18n'; import { Message } from 'typegram' import { UserDocument } from '../models/user' import { FilterQuery } from 'mongoose'; -const OrderEvents = require('./modules/events/orders'); +import * as OrderEvents from './modules/events/orders'; import { limit } from "@grammyjs/ratelimiter" -const schedule = require('node-schedule'); +import schedule from 'node-schedule'; import { Order, User, @@ -15,21 +15,21 @@ import { Config, } from '../models'; import { getCurrenciesWithPrice, deleteOrderFromChannel, removeAtSymbol } from '../util'; -const { +import { commandArgsMiddleware, stageMiddleware, userMiddleware, adminMiddleware, superAdminMiddleware, -} = require('./middleware'); -const ordersActions = require('./ordersActions'); -const CommunityModule = require('./modules/community'); -const LanguageModule = require('./modules/language'); -const NostrModule = require('./modules/nostr'); -const OrdersModule = require('./modules/orders'); -const UserModule = require('./modules/user'); -const DisputeModule = require('./modules/dispute'); -const { +} from './middleware'; +import * as ordersActions from './ordersActions'; +import * as CommunityModule from './modules/community'; +import * as LanguageModule from './modules/language'; +import * as NostrModule from './modules/nostr'; +import * as OrdersModule from './modules/orders'; +import * as UserModule from './modules/user'; +import * as DisputeModule from './modules/dispute'; +import { rateUser, cancelAddInvoice, addInvoice, @@ -39,20 +39,20 @@ const { cancelOrder, fiatSent, release, -} = require('./commands'); -const { +} from './commands'; +import { settleHoldInvoice, cancelHoldInvoice, payToBuyer, subscribeInvoice, getInvoice, -} = require('../ln'); -const { +} from '../ln'; +import { validateUser, validateParams, validateObjectId, validateLightningAddress, -} = require('./validations'); +} from './validations'; import * as messages from './messages'; import { attemptPendingPayments, @@ -65,6 +65,7 @@ import { } from '../jobs'; import { logger } from "../logger"; import { ICommunity, IUsernameId } from '../models/community'; +import { CommunityContext } from './modules/community/communityContext'; export interface MainContext extends Context { match: Array | null; @@ -153,14 +154,14 @@ https://github.com/telegraf/telegraf/issues/1319#issuecomment-766360594 */ export const ctxUpdateAssertMsg = "ctx.update.message.text is not available."; -const initialize = (botToken: string, options: Partial>): Telegraf => { +const initialize = (botToken: string, options: Partial>): Telegraf => { const i18n = new I18n({ defaultLanguageOnMissing: true, // implies allowMissing = true directory: 'locales', useSession: true, }); - const bot = new Telegraf(botToken, options); + const bot = new Telegraf(botToken, options); bot.catch(err => { logger.error(err); }); @@ -218,7 +219,7 @@ const initialize = (botToken: string, options: Partial => { try { - const [val] = await validateParams(ctx, 2, '\\<_on/off_\\>'); + const [val] = (await validateParams(ctx, 2, '\\<_on/off_\\>'))!; if (!val) return; let config = await Config.findOne(); if (config === null) { @@ -289,7 +290,7 @@ const initialize = (botToken: string, options: Partial { try { - const [orderId] = await validateParams(ctx, 2, '\\<_order id_\\>'); + const [orderId] = (await validateParams(ctx, 2, '\\<_order id_\\>'))!; if (!orderId) return; if (!(await validateObjectId(ctx, orderId))) return; @@ -324,7 +325,9 @@ const initialize = (botToken: string, options: Partial { try { - const [orderId] = await validateParams(ctx, 2, '\\<_order id_\\>'); + const validatedParams = (await validateParams(ctx, 2, '\\<_order id_\\>'))!; + if (validatedParams == null) return; + const [orderId] = validatedParams; if (!orderId) return; if (!(await validateObjectId(ctx, orderId))) return; @@ -391,7 +394,7 @@ const initialize = (botToken: string, options: Partial { + bot.command('cancel', userMiddleware, async (ctx: CommunityContext) => { try { if (!('message' in ctx.update) || !('text' in ctx.update.message)){ throw new Error(ctxUpdateAssertMsg); @@ -416,13 +419,13 @@ const initialize = (botToken: string, options: Partial { + bot.command('cancelall', userMiddleware, async (ctx: CommunityContext) => { try { - const pending_orders = await ordersActions.getOrders(ctx.user, 'PENDING'); - const seller_orders = await ordersActions.getOrders(ctx.user, 'WAITING_BUYER_INVOICE'); - const buyer_orders = await ordersActions.getOrders(ctx.user, 'WAITING_PAYMENT'); + const pending_orders = await ordersActions.getOrders(ctx.user, 'PENDING') || []; + const seller_orders = await ordersActions.getOrders(ctx.user, 'WAITING_BUYER_INVOICE') || []; + const buyer_orders = await ordersActions.getOrders(ctx.user, 'WAITING_PAYMENT') || []; - const orders = [...pending_orders, ...seller_orders, ...buyer_orders] + const orders = [...pending_orders, ...seller_orders, ...buyer_orders]; if (orders.length === 0) { return await messages.notOrdersMessage(ctx); @@ -452,7 +455,7 @@ const initialize = (botToken: string, options: Partial { + bot.command('settleorder', adminMiddleware, async (ctx: CommunityContext) => { try { - const [orderId] = await validateParams(ctx, 2, '\\<_order id_\\>'); + const [orderId] = (await validateParams(ctx, 2, '\\<_order id_\\>'))!; if (!orderId) return; if (!(await validateObjectId(ctx, orderId))) return; @@ -527,7 +530,7 @@ const initialize = (botToken: string, options: Partial { try { - const [orderId] = await validateParams(ctx, 2, '\\<_order id_\\>'); + const [orderId] = (await validateParams(ctx, 2, '\\<_order id_\\>'))!; if (!orderId) return; if (!(await validateObjectId(ctx, orderId))) return; const order = await Order.findOne({ _id: orderId }); @@ -545,7 +548,7 @@ const initialize = (botToken: string, options: Partial { try { - const [orderId] = await validateParams(ctx, 2, '\\<_order id_\\>'); + const [orderId] = (await validateParams(ctx, 2, '\\<_order id_\\>'))!; if (!orderId) return; if (!(await validateObjectId(ctx, orderId))) return; const order = await Order.findOne({ _id: orderId }); @@ -568,7 +571,7 @@ const initialize = (botToken: string, options: Partial { try { - const [hash] = await validateParams(ctx, 2, '\\<_hash_\\>'); + const [hash] = (await validateParams(ctx, 2, '\\<_hash_\\>'))!; if (!hash) return; @@ -632,11 +635,11 @@ const initialize = (botToken: string, options: Partial { try { - let [username] = await validateParams( + let [username] = (await validateParams( ctx, 2, '\\<_username or telegram ID_\\>' - ); + ))!; if (!username) return; @@ -676,11 +679,11 @@ const initialize = (botToken: string, options: Partial { try { - let [username] = await validateParams( + let [username] = (await validateParams( ctx, 2, '\\<_username or telegram ID_\\>' - ); + ))!; if (!username) return; @@ -719,11 +722,11 @@ const initialize = (botToken: string, options: Partial { try { - const [lightningAddress] = await validateParams( + const [lightningAddress] = (await validateParams( ctx, 2, '\\<_lightningAddress / off_\\>' - ); + ))!; if (!lightningAddress) return; if (lightningAddress === 'off') { @@ -758,37 +761,37 @@ const initialize = (botToken: string, options: Partial { + bot.action('addInvoiceBtn', userMiddleware, async (ctx: CommunityContext) => { await addInvoice(ctx, bot); }); - bot.action('cancelAddInvoiceBtn', userMiddleware, async (ctx: MainContext) => { + bot.action('cancelAddInvoiceBtn', userMiddleware, async (ctx: CommunityContext) => { await cancelAddInvoice(ctx); }); - bot.action('showHoldInvoiceBtn', userMiddleware, async (ctx: MainContext) => { + bot.action('showHoldInvoiceBtn', userMiddleware, async (ctx: CommunityContext) => { await showHoldInvoice(ctx, bot); }); - bot.action('cancelShowHoldInvoiceBtn', userMiddleware, async (ctx: MainContext) => { + bot.action('cancelShowHoldInvoiceBtn', userMiddleware, async (ctx: CommunityContext) => { await cancelShowHoldInvoice(ctx); }); - bot.action(/^showStarBtn\(([1-5]),(\w{24})\)$/, userMiddleware, async (ctx: MainContext) => { + bot.action(/^showStarBtn\(([1-5]),(\w{24})\)$/, userMiddleware, async (ctx: CommunityContext) => { if (ctx.match === null) { throw new Error("ctx.match should not be null"); } - await rateUser(ctx, bot, ctx.match[1], ctx.match[2]); + await rateUser(ctx, bot, Number(ctx.match[1]), ctx.match[2]); }); - bot.action(/^addInvoicePHIBtn_([0-9a-f]{24})$/, userMiddleware, async (ctx: MainContext) => { + bot.action(/^addInvoicePHIBtn_([0-9a-f]{24})$/, userMiddleware, async (ctx: CommunityContext) => { if (ctx.match === null) { throw new Error("ctx.match should not be null"); } await addInvoicePHI(ctx, bot, ctx.match[1]); }); - bot.action(/^setinvoice_([0-9a-f]{24})$/, userMiddleware, async (ctx: MainContext) => { + bot.action(/^setinvoice_([0-9a-f]{24})$/, userMiddleware, async (ctx: CommunityContext) => { if (ctx.match === null) { throw new Error("ctx.match should not be null"); } @@ -796,7 +799,7 @@ const initialize = (botToken: string, options: Partial { + bot.action(/^cancel_([0-9a-f]{24})$/, userMiddleware, async (ctx: CommunityContext) => { if (ctx.match === null) { throw new Error("ctx.match should not be null"); } @@ -804,7 +807,7 @@ const initialize = (botToken: string, options: Partial { + bot.action(/^fiatsent_([0-9a-f]{24})$/, userMiddleware, async (ctx: CommunityContext) => { if (ctx.match === null) { throw new Error("ctx.match should not be null"); } @@ -812,7 +815,7 @@ const initialize = (botToken: string, options: Partial { + bot.action(/^release_([0-9a-f]{24})$/, userMiddleware, async (ctx: CommunityContext) => { if (ctx.match === null) { throw new Error("ctx.match should not be null"); } @@ -822,7 +825,7 @@ const initialize = (botToken: string, options: Partial { try { - const [orderId] = await validateParams(ctx, 2, '\\<_order id_\\>'); + const [orderId] = (await validateParams(ctx, 2, '\\<_order id_\\>'))!; if (!orderId) return; if (!(await validateObjectId(ctx, orderId))) return; const order = await Order.findOne({ @@ -877,12 +880,12 @@ const initialize = (botToken: string, options: Partial { try { - let [show] = await validateParams(ctx, 2, '_yes/no_'); - if (!show) return; - show = show === 'yes'; + const [showString] = (await validateParams(ctx, 2, '_yes/no_'))!; + if (!showString) return; + const show = showString === 'yes'; ctx.user.show_username = show; await ctx.user.save(); - messages.updateUserSettingsMessage(ctx, 'showusername', show); + messages.updateUserSettingsMessage(ctx, 'showusername', String(show)); } catch (error) { logger.error(error); } @@ -890,12 +893,12 @@ const initialize = (botToken: string, options: Partial { try { - let [show] = await validateParams(ctx, 2, '_yes/no_'); - if (!show) return; - show = show === 'yes'; + const [showString] = (await validateParams(ctx, 2, '_yes/no_'))!; + if (!showString) return; + const show = showString === 'yes'; ctx.user.show_volume_traded = show; await ctx.user.save(); - messages.updateUserSettingsMessage(ctx, 'showvolume', show); + messages.updateUserSettingsMessage(ctx, 'showvolume', String(show)); } catch (error) { logger.error(error); } @@ -932,7 +935,7 @@ const initialize = (botToken: string, options: Partial>): Telegraf => { +const start = (botToken: string, options: Partial>): Telegraf => { const bot = initialize(botToken, options); bot.launch(); diff --git a/bot/validations.ts b/bot/validations.ts index ea6ee77c..c73a4731 100644 --- a/bot/validations.ts +++ b/bot/validations.ts @@ -5,12 +5,12 @@ import { UserDocument } from "../models/user"; import { IOrder } from "../models/order"; import { Telegraf } from "telegraf"; -const { parsePaymentRequest } = require('invoices'); +import { parsePaymentRequest } from 'invoices'; const { ObjectId } = require('mongoose').Types; import * as messages from './messages'; import { Order, User, Community } from '../models'; import { isIso4217, isDisputeSolver, removeLightningPrefix, isOrderCreator } from '../util'; -const { existLightningAddress } = require('../lnurl/lnurl-pay'); +import { existLightningAddress } from '../lnurl/lnurl-pay'; import { logger } from '../logger'; const ctxUpdateMessageFromAssertMsg = "ctx.update.message.from is not available"; diff --git a/jobs/cancel_orders.ts b/jobs/cancel_orders.ts index 686b3dec..05e1887f 100644 --- a/jobs/cancel_orders.ts +++ b/jobs/cancel_orders.ts @@ -1,13 +1,14 @@ import { Telegraf } from "telegraf"; -import { MainContext } from "../bot/start"; +import { HasTelegram, MainContext } from "../bot/start"; import { User, Order } from "../models"; -const { cancelShowHoldInvoice, cancelAddInvoice } = require('../bot/commands'); +import { cancelShowHoldInvoice, cancelAddInvoice } from '../bot/commands'; import * as messages from "../bot/messages"; import { getUserI18nContext, holdInvoiceExpirationInSecs } from '../util'; import { logger } from "../logger"; -const OrderEvents = require('../bot/modules/events/orders'); +import { CommunityContext } from "../bot/modules/community/communityContext"; +import * as OrderEvents from '../bot/modules/events/orders'; -const cancelOrders = async (bot: Telegraf) => { +const cancelOrders = async (bot: HasTelegram) => { try { const holdInvoiceTime = new Date(); holdInvoiceTime.setSeconds( @@ -30,9 +31,9 @@ const cancelOrders = async (bot: Telegraf) => { }); for (const order of waitingPaymentOrders) { if (order.status === 'WAITING_PAYMENT') { - await cancelShowHoldInvoice(bot, order, true); + await cancelShowHoldInvoice(bot as CommunityContext, order, true); } else { - await cancelAddInvoice(bot, order, true); + await cancelAddInvoice(bot as CommunityContext, order, true); } } // We get the expired order where the seller sent the sats but never released the order diff --git a/jobs/communities.ts b/jobs/communities.ts index a3599506..1956617a 100644 --- a/jobs/communities.ts +++ b/jobs/communities.ts @@ -1,10 +1,10 @@ import { Telegraf } from "telegraf"; -import { MainContext } from "../bot/start"; import { Order, Community } from '../models'; import { logger } from "../logger"; +import { CommunityContext } from "../bot/modules/community/communityContext"; -const deleteCommunity = async (bot: Telegraf) => { +const deleteCommunity = async (bot: Telegraf) => { try { const communities = await Community.find(); for (const community of communities) { diff --git a/jobs/delete_published_orders.ts b/jobs/delete_published_orders.ts index 4182840d..935449d5 100644 --- a/jobs/delete_published_orders.ts +++ b/jobs/delete_published_orders.ts @@ -1,11 +1,12 @@ import { Telegraf } from "telegraf"; -import { MainContext } from "../bot/start"; import { Order } from '../models'; -const { deleteOrderFromChannel } = require('../util'); +import { deleteOrderFromChannel } from '../util'; import { logger } from '../logger'; +import { CommunityContext } from "../bot/modules/community/communityContext"; +import { IOrder } from "../models/order"; -const deleteOrders = async (bot: Telegraf) => { +const deleteOrders = async (bot: Telegraf) => { try { const windowTime = new Date(); windowTime.setSeconds( @@ -21,7 +22,7 @@ const deleteOrders = async (bot: Telegraf) => { logger.info( `Pending order Id: ${order._id} expired after ${process.env.ORDER_PUBLISHED_EXPIRATION_WINDOW} seconds, deleting it from database and channel` ); - const orderCloned = order.toObject(); + const orderCloned = order.toObject() as IOrder; // We remove the order from the database first, then we remove the message from the channel await order.remove(); // We delete the messages related to that order from the channel diff --git a/jobs/node_info.ts b/jobs/node_info.ts index 22e82eb3..2fc20b05 100644 --- a/jobs/node_info.ts +++ b/jobs/node_info.ts @@ -1,11 +1,11 @@ import { Telegraf } from "telegraf"; -import { MainContext } from "../bot/start"; import { Config } from '../models'; +import { CommunityContext } from "../bot/modules/community/communityContext"; const { getInfo } = require('../ln'); -const { logger } = require('../logger'); +import { logger } from '../logger'; -const info = async (bot: Telegraf) => { +const info = async (bot: Telegraf) => { try { let config = await Config.findOne({}); if (config === null) { diff --git a/jobs/pending_payments.ts b/jobs/pending_payments.ts index b3b9a091..539b9e93 100644 --- a/jobs/pending_payments.ts +++ b/jobs/pending_payments.ts @@ -4,11 +4,12 @@ import { logger } from "../logger"; import { Telegraf } from 'telegraf'; import { I18nContext } from '@grammyjs/i18n'; import { MainContext } from '../bot/start'; -const { payRequest, isPendingPayment } = require('../ln'); +import { payRequest, isPendingPayment } from '../ln'; import { getUserI18nContext } from '../util'; -const { orderUpdated } = require('../bot/modules/events/orders'); +import { CommunityContext } from '../bot/modules/community/communityContext'; +import { orderUpdated } from '../bot/modules/events/orders'; -export const attemptPendingPayments = async (bot: Telegraf): Promise => { +export const attemptPendingPayments = async (bot: Telegraf): Promise => { const pendingPayments = await PendingPayment.find({ paid: false, attempts: { $lt: process.env.PAYMENT_ATTEMPTS }, @@ -118,7 +119,7 @@ export const attemptPendingPayments = async (bot: Telegraf): Promis } }; -export const attemptCommunitiesPendingPayments = async (bot: Telegraf): Promise => { +export const attemptCommunitiesPendingPayments = async (bot: Telegraf): Promise => { const pendingPayments = await PendingPayment.find({ paid: false, attempts: { $lt: process.env.PAYMENT_ATTEMPTS }, From 08b1333d975b900699c20b75c1574932ed1eda6c Mon Sep 17 00:00:00 2001 From: webwarrior Date: Tue, 4 Mar 2025 14:51:12 +0100 Subject: [PATCH 2/3] bot: removed @ts-check annotations They are used to typecheck .js files, but make no sense in .ts files as they are already type-checked. --- bot/middleware/stage.ts | 1 - bot/modules/community/commands.ts | 1 - bot/modules/community/index.ts | 1 - bot/modules/events/community.ts | 1 - bot/modules/events/orders.ts | 1 - bot/modules/orders/commands.ts | 1 - bot/modules/orders/index.ts | 1 - bot/modules/orders/takeOrder.ts | 1 - bot/modules/user/index.ts | 1 - 9 files changed, 9 deletions(-) diff --git a/bot/middleware/stage.ts b/bot/middleware/stage.ts index 3a3baf16..1bedcd4c 100644 --- a/bot/middleware/stage.ts +++ b/bot/middleware/stage.ts @@ -1,4 +1,3 @@ -// @ts-check import { Scenes } from 'telegraf'; import * as CommunityModule from '../modules/community'; import * as OrdersModule from '../modules/orders'; diff --git a/bot/modules/community/commands.ts b/bot/modules/community/commands.ts index f378c1db..79291e81 100644 --- a/bot/modules/community/commands.ts +++ b/bot/modules/community/commands.ts @@ -1,5 +1,4 @@ /* eslint-disable no-underscore-dangle */ -// @ts-check import { logger } from '../../../logger'; import { showUserCommunitiesMessage } from './messages'; import { Community, Order } from '../../../models'; diff --git a/bot/modules/community/index.ts b/bot/modules/community/index.ts index 94ddd861..60049079 100644 --- a/bot/modules/community/index.ts +++ b/bot/modules/community/index.ts @@ -1,4 +1,3 @@ -// @ts-check import { Telegraf } from 'telegraf'; import { userMiddleware } from '../../middleware/user'; import * as actions from './actions'; diff --git a/bot/modules/events/community.ts b/bot/modules/events/community.ts index 3fccc45d..6895251d 100644 --- a/bot/modules/events/community.ts +++ b/bot/modules/events/community.ts @@ -1,4 +1,3 @@ -// @ts-check import { ICommunity } from '../../../models/community'; import * as Events from './index'; import { TYPES as ORDER_TYPES } from './orders'; diff --git a/bot/modules/events/orders.ts b/bot/modules/events/orders.ts index a38784cf..e3e84a8d 100644 --- a/bot/modules/events/orders.ts +++ b/bot/modules/events/orders.ts @@ -1,4 +1,3 @@ -// @ts-check import { IOrder } from '../../../models/order'; import * as Events from './index'; diff --git a/bot/modules/orders/commands.ts b/bot/modules/orders/commands.ts index 105f59b9..47ff1942 100644 --- a/bot/modules/orders/commands.ts +++ b/bot/modules/orders/commands.ts @@ -1,4 +1,3 @@ -// @ts-check import { logger } from '../../../logger'; import { Community, Order } from '../../../models'; import { isFloat } from '../../../util'; diff --git a/bot/modules/orders/index.ts b/bot/modules/orders/index.ts index b825bd4c..6225dc4b 100644 --- a/bot/modules/orders/index.ts +++ b/bot/modules/orders/index.ts @@ -1,4 +1,3 @@ -// @ts-check import { userMiddleware } from '../../middleware/user'; import { logger } from '../../../logger'; import * as ordersActions from '../../ordersActions'; diff --git a/bot/modules/orders/takeOrder.ts b/bot/modules/orders/takeOrder.ts index ba8b8cce..60b1fcf5 100644 --- a/bot/modules/orders/takeOrder.ts +++ b/bot/modules/orders/takeOrder.ts @@ -1,4 +1,3 @@ -// @ts-check import { Telegraf } from 'telegraf'; import { logger } from '../../../logger'; import { Order } from '../../../models'; diff --git a/bot/modules/user/index.ts b/bot/modules/user/index.ts index 0842196b..cffb2692 100644 --- a/bot/modules/user/index.ts +++ b/bot/modules/user/index.ts @@ -1,4 +1,3 @@ -// @ts-check import { userMiddleware } from '../../middleware/user'; import { Telegraf } from 'telegraf'; From 0c2ace16176fcf0137cb00be937348c349d4871c Mon Sep 17 00:00:00 2001 From: webwarrior Date: Thu, 5 Dec 2024 13:20:38 +0100 Subject: [PATCH 3/3] tests/bot: convert requires to imports --- tests/bot/bot.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bot/bot.spec.ts b/tests/bot/bot.spec.ts index 146edb66..bebac146 100644 --- a/tests/bot/bot.spec.ts +++ b/tests/bot/bot.spec.ts @@ -4,7 +4,7 @@ const sinon = require('sinon'); const { expect } = require('chai'); const proxyquire = require('proxyquire'); -const { initialize } = require('../../bot'); +import { initialize } from '../../bot'; import { User, Order } from '../../models'; import { getCurrenciesWithPrice } from '../../util'; import { mockUpdatesResponseForCurrencies } from './mocks/currenciesResponse';