Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cronjob to notify admin about missing solvers (#314) #563

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bot/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const {
attemptCommunitiesPendingPayments,
deleteCommunity,
nodeInfo,
checkSolvers,
} = require('../jobs');
const { logger } = require('../logger');
export interface MainContext extends Context {
Expand Down Expand Up @@ -197,6 +198,10 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
await nodeInfo(bot);
});

schedule.scheduleJob('0 0 * * *', async () => {
await checkSolvers(bot);
});

bot.start(async (ctx: MainContext) => {
try {
if (!('message' in ctx.update) || !('text' in ctx.update.message)){
Expand Down
40 changes: 40 additions & 0 deletions jobs/check_solvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Community, User } from '../models';
import { Telegraf } from 'telegraf';
import { MainContext } from '../bot/start';
import { logger } from '../logger';

const MAX_MESSAGES = 5; // Number of messages before disabling the community
Catrya marked this conversation as resolved.
Show resolved Hide resolved

exports.checkSolvers = async (bot: Telegraf<MainContext>): Promise<void> => {
try {
const communities = await Community.find({ isDisabled: false });

for (const community of communities) {
const solvers = await User.find({ default_community_id: community._id, role: 'solver' });

if (solvers.length === 0) {
community.messagesSent += 1;

if (community.messagesSent >= MAX_MESSAGES) {
community.isDisabled = true;
await community.save();
logger.info(`Community ${community._id} has been disabled due to lack of solvers.`);
} else {
await community.save();
const admin = await User.findOne({ tg_id: community.creator_id, admin: true });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user.admin is only used for global admin, not community admins, we don't need to add it on this findOne() request

if (admin) {
await bot.telegram.sendMessage(
admin.tg_id,
`Your community ${community.name} doesn't have any solvers. Please add at least one solver.`
Catrya marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
} else {
community.messagesSent = 0; // Reset the counter if solvers are added
await community.save();
}
}
} catch (error) {
logger.error(error);
}
};
4 changes: 4 additions & 0 deletions models/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface ICommunity extends Document {
currencies: Array<string>;
created_at: Date;
nostr_public_key: string;
messagesSent: number;
isDisabled: boolean;
}

const CommunitySchema = new Schema<ICommunity>({
Expand Down Expand Up @@ -81,6 +83,8 @@ const CommunitySchema = new Schema<ICommunity>({
},
created_at: { type: Date, default: Date.now },
nostr_public_key: { type: String },
messagesSent: { type: Number, default: 0 },
isDisabled: { type: Boolean, default: false },
});


Expand Down
Loading