Skip to content

Commit

Permalink
added admin checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedegeofroy committed Oct 20, 2024
1 parent 5e0aeee commit ba1f433
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ POSTGRES_PORT=5432
POSTGRES_DB=root
AUTO_MIGRATE=True

BOT_KEY=botkey
BOT_KEY=botkey
TELEGRAM_CHANNEL_ID=-1002123654210
10 changes: 0 additions & 10 deletions src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------

input CreateStatInput {
count: Int!
name: String!
stat_id: String!
}

"""
A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
"""
scalar DateTime

type Mutation {
createStat(createStatInput: CreateStatInput!): Stat!
}

type Query {
stats: [Stat!]!
}
Expand Down
96 changes: 81 additions & 15 deletions src/telegram-stats/telegram-stats.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Hears, Help, On, Start, Update } from 'nestjs-telegraf';
import { On, Start, Update } from 'nestjs-telegraf';
import { StatsService } from 'src/stats/stats.service';
import { Context } from 'telegraf';
import { Context, Markup } from 'telegraf';

// todo
// check the telegram id's for god's sake
Expand All @@ -13,6 +13,8 @@ export class TelegramStatsService {

@On('chat_member')
async onNewChatMembers(ctx: Context) {
if (ctx.chat.id !== parseInt(process.env.TELEGRAM_CHANNEL_ID)) return;

const currentCount = (await this.statsService.findLatest()).find(
(x) => x.stat_id == 'telegram_channel_count'
).count;
Expand All @@ -36,25 +38,89 @@ export class TelegramStatsService {
}
}

@Start()
async startCommand(ctx: Context) {
const isAdmin = ctx.telegram
.getChatMember(parseInt(process.env.TELEGRAM_CHANNEL_ID), ctx.from.id)
.then((x) => x.status === 'administrator' || x.status === 'creator');

if (!isAdmin) return;

await ctx.reply(
'Welcome! Use the keyboard to update stats. To update a custom value send a message in the following format \n\n [stat_id] [count] \n\n coffee_cups \n sleepless_nights \n github_commit_count \n telegram_channel_count \n telegram_channel_joined \n commits',
Markup.keyboard([
Markup.button.callback('Update Coffee Cups', 'update_coffee_cups'),
Markup.button.callback(
'Update Sleepless Nights',
'update_sleepless_nights'
),
])
);
}

@On('text')
async onComment(ctx: Context) {
if (ctx.chat.type === 'private') {
console.info('updated stat');
const stat_id = ctx.text.split(' ')[0];
const count = parseInt(ctx.text.split(' ')[1]);
const name = ctx.text.split(' ').slice(2).join(' ');
async onText(ctx: Context) {
const message = ctx.text;

const isAdmin = ctx.telegram
.getChatMember(parseInt(process.env.TELEGRAM_CHANNEL_ID), ctx.from.id)
.then((x) => x.status === 'administrator' || x.status === 'creator');

if (!isAdmin) return;

if (message === 'Update Coffee Cups') {
const currentStat = (await this.statsService.findLatest()).find(
(x) => x.stat_id == 'coffee_cups'
);

const updatedCount = currentStat ? currentStat.count + 1 : 1;

await this.statsService.createStat({
stat_id,
count,
name,
stat_id: 'coffee_cups',
name: currentStat.name,
count: updatedCount,
});

await ctx.reply(`Coffee cups updated to ${updatedCount}!`);
} else if (message === 'Update Sleepless Nights') {
const currentStat = (await this.statsService.findLatest()).find(
(x) => x.stat_id == 'sleepless_nights'
);

const updatedCount = currentStat ? currentStat.count + 1 : 1;

await this.statsService.createStat({
stat_id: 'sleepless_nights',
name: currentStat.name,
count: updatedCount,
});

await ctx.reply(`Sleepless nigts updated to ${updatedCount}!`);
} else {
console.info('added comment');
const custom_data = message.split(' ');
if (custom_data.length > 2) {
await ctx.reply(`Unknown format.`);
return;
}

const currentStat = (await this.statsService.findLatest()).find(
(x) => x.stat_id == custom_data[0]
);

if (currentStat == undefined) {
await ctx.reply(`Unknown current stat.`);
return;
}

await this.statsService.createStat({
stat_id: 'telegram_channel_commented',
name: `"${ctx.text}"`,
count: 0,
stat_id: currentStat.stat_id,
name: currentStat.name,
count: parseInt(custom_data[1]),
});

await ctx.reply(
`${currentStat.name} updated to ${parseInt(custom_data[1])}!`
);
}
}
}

0 comments on commit ba1f433

Please sign in to comment.