Skip to content

Commit

Permalink
add group whitelist logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fegloff committed Oct 18, 2023
1 parent 9ba5791 commit 601f5f3
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/modules/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,21 @@ export class BotPayments {
)
}

public isGroupInWhitelist (chatId: number | string): boolean {
const { groupWhitelist } = config.payment
return (
groupWhitelist.includes(chatId.toString())
)
public async isGroupInWhitelist (ctx: OnMessageContext): Promise<boolean> {
if (ctx.chat.id && ctx.chat.type !== 'private') {
const { whitelist } = config.payment
const admins = await ctx.getChatAdministrators()
for (let i = 0; i < admins.length; i++) {
const username = admins[i].user.username ?? ''
if (whitelist.includes(admins[i].user.id.toString()) ||
(username && whitelist.includes(username.toString().toLowerCase()))) {
return true
}
}
return false
} else {
return true
}
}

public isPaymentsEnabled (): boolean {
Expand All @@ -295,22 +305,31 @@ export class BotPayments {
)
}

private skipPayment (ctx: OnMessageContext, amountUSD: number): boolean {
private async skipPayment (ctx: OnMessageContext, amountUSD: number): Promise<boolean> {
const { id: userId, username = '' } = ctx.update.message.from

if (!this.isPaymentsEnabled()) {
return true
}

if (amountUSD === 0) {
return true
}

if (this.isUserInWhitelist(userId, username)) {
this.logger.info(
`@${username} (${userId}) is in the whitelist, skip payment`
)
return true
}

if (await this.isGroupInWhitelist(ctx)) {
this.logger.info(
`The chat ${ctx.chat.id} is in the whitelist, skip payment`
)
return true
}

if (this.ONERate === 0) {
this.logger.error('ONE token rate is 0, skip payment')
return true
Expand Down Expand Up @@ -384,7 +403,7 @@ export class BotPayments {
`Payment requested @${from.username}(${from.id}) in chat ${chat.id} (${chat.type}), accountId: ${accountId}, account address: ${userAccount.address}`
)

if (this.skipPayment(ctx, amountUSD)) {
if (await this.skipPayment(ctx, amountUSD)) {
await this.writePaymentLog(ctx, BigNumber(0))
return true
}
Expand Down

0 comments on commit 601f5f3

Please sign in to comment.