Skip to content

Commit

Permalink
Merge pull request #168 from harmony-one/dev
Browse files Browse the repository at this point in the history
Dev to Master
  • Loading branch information
theofandrich authored Aug 25, 2023
2 parents f487b88 + b969e76 commit 304357f
Show file tree
Hide file tree
Showing 33 changed files with 1,216 additions and 1,259 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"lokijs": "^1.5.12",
"lru-cache": "^10.0.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"node-cron": "^3.0.2",
"openai": "^4.0.1",
"otpauth": "^9.1.3",
Expand Down
143 changes: 94 additions & 49 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ import { BotPayments } from "./modules/payment";
import { BotSchedule } from "./modules/schedule";
import config from "./config";
import { commandsHelpText, TERMS, SUPPORT, FEEDBACK, LOVE } from "./constants";
import prometheusRegister from './metrics/prometheus'
import prometheusRegister, {PrometheusMetrics} from "./metrics/prometheus";

import {chatService, statsService} from "./database/services";
import {AppDataSource} from "./database/datasource";
import { text } from "stream/consumers";
import { chatService, statsService } from "./database/services";
import { AppDataSource } from "./database/datasource";
import { autoRetry } from "@grammyjs/auto-retry";
import {run} from "@grammyjs/runner";
import {runBotHeartBit} from "./monitoring/monitoring";

import {BotPaymentLog} from "./database/stats.service";

const logger = pino({
name: "bot",
Expand Down Expand Up @@ -86,6 +85,8 @@ function createInitialSessionData(): BotSessionData {
chatConversation: [],
price: 0,
usage: 0,
isProcessingQueue: false,
requestQueue: [],
},
},
oneCountry: {
Expand Down Expand Up @@ -171,31 +172,57 @@ bot.use((ctx, next) => {

for (let i = 0; i < entities.length; i++) {
const entity = entities[i];
if (entity.type === 'bot_command' && ctx.message) {
if (entity.type === "bot_command" && ctx.message) {
const tgUserId = ctx.message.from.id;
statsService.addCommandStat({tgUserId, command: entity.text.replace('/', ''), rawMessage: ''})
statsService.addCommandStat({
tgUserId,
command: entity.text.replace("/", ""),
rawMessage: "",
});
}
}

return next();
})
});

const writeCommandLog = async (ctx: OnMessageContext, isSupportedCommand = true) => {
const { from, text = '', chat } = ctx.update.message

try {
const accountId = payments.getAccountId(ctx);
const [command] = text?.split(' ')

const log: BotPaymentLog = {
tgUserId: from.id,
accountId,
command,
groupId: chat.id,
isPrivate: chat.type === 'private',
message: text,
isSupportedCommand,
amountCredits: 0,
amountOne: 0,
}
await statsService.writeLog(log)
} catch (e) {
logger.error(`Cannot write unsupported command log: ${(e as Error).message}`)
}
}

const onMessage = async (ctx: OnMessageContext) => {
try {
await assignFreeCredits(ctx);

if (qrCodeBot.isSupportedEvent(ctx)) {
const price = qrCodeBot.getEstimatedPrice(ctx);
const isPaid = await payments.pay(ctx, price);
if (isPaid) {
await qrCodeBot
.onEvent(ctx, (reason?: string) => {
payments.refundPayment(reason, ctx, price);
})
.catch((e) => {
payments.refundPayment(e.message || "Unknown error", ctx, price);
});

.onEvent(ctx, (reason?: string) => {
payments.refundPayment(reason, ctx, price);
})
.catch((e) => {
payments.refundPayment(e.message || "Unknown error", ctx, price);
});
return;
}
}
Expand All @@ -204,12 +231,12 @@ const onMessage = async (ctx: OnMessageContext) => {
const isPaid = await payments.pay(ctx, price);
if (isPaid) {
await sdImagesBot
.onEvent(ctx, (reason?: string) => {
payments.refundPayment(reason, ctx, price);
})
.catch((e) => {
payments.refundPayment(e.message || "Unknown error", ctx, price);
});
.onEvent(ctx, (reason?: string) => {
payments.refundPayment(reason, ctx, price);
})
.catch((e) => {
payments.refundPayment(e.message || "Unknown error", ctx, price);
});
return;
}
return;
Expand All @@ -218,7 +245,7 @@ const onMessage = async (ctx: OnMessageContext) => {
const price = voiceMemo.getEstimatedPrice(ctx);
const isPaid = await payments.pay(ctx, price);
if (isPaid) {
await voiceMemo.onEvent(ctx).catch((e) => {
await voiceMemo.onEvent(ctx).catch((e) => {
payments.refundPayment(e.message || "Unknown error", ctx, price);
});
}
Expand All @@ -228,19 +255,19 @@ const onMessage = async (ctx: OnMessageContext) => {
if (ctx.session.openAi.imageGen.isEnabled) {
if (openAiBot.isValidCommand(ctx)) {
const price = openAiBot.getEstimatedPrice(ctx);
const isPaid = await payments.pay(ctx, price);
const isPaid = await payments.pay(ctx, price!);
if (isPaid) {
return openAiBot
.onEvent(ctx)
.catch((e) => payments.refundPayment(e, ctx, price));
await openAiBot
.onEvent(ctx)
.catch((e) => payments.refundPayment(e, ctx, price!));
return;
}
return;
} else {
// ctx.reply("Error: Missing prompt");
return;
}
} else {
ctx.reply("Bot disabled");
await ctx.reply("Bot disabled");
return;
}
}
Expand All @@ -253,13 +280,12 @@ const onMessage = async (ctx: OnMessageContext) => {
const isPaid = await payments.pay(ctx, price);
if (isPaid) {
await oneCountryBot
.onEvent(ctx)
.catch((e) => payments.refundPayment(e, ctx, price));
.onEvent(ctx)
.catch((e) => payments.refundPayment(e, ctx, price));
return;
}
return;
} else {
// ctx.reply("Error: Missing prompt");
return;
}
}
Expand All @@ -278,21 +304,23 @@ const onMessage = async (ctx: OnMessageContext) => {
}
// if (ctx.update.message.text && ctx.update.message.text.startsWith("/", 0)) {
// const command = ctx.update.message.text.split(' ')[0].slice(1)
// onlfy for private chats
// only for private chats
if (ctx.update.message.chat && ctx.chat.type === "private") {
await ctx.reply(
`Unsupported, type */help* for commands.`,
{
parse_mode: "Markdown",
}
);
await writeCommandLog(ctx, false)
return;
}
if (ctx.update.message.chat) {
logger.info(`Received message in chat id: ${ctx.update.message.chat.id}`);
}
}catch(ex: any){
console.error('onMessage error', ex)
await writeCommandLog(ctx, false)
} catch (ex: any) {
console.error("onMessage error", ex);
}
};

Expand All @@ -311,8 +339,8 @@ const onCallback = async (ctx: OnCallBackQueryData) => {
});
return;
}
}catch(ex: any){
console.error('onMessage error', ex)
} catch (ex: any) {
console.error("onMessage error", ex);
}
};

Expand All @@ -326,6 +354,8 @@ bot.command(["start", "help", "menu"], async (ctx) => {
return false;
}

await writeCommandLog(ctx as OnMessageContext)

const addressBalance = await payments.getAddressBalance(account.address);
const credits = await chatService.getBalance(accountId);
const balance = addressBalance.plus(credits);
Expand All @@ -342,34 +372,39 @@ bot.command(["start", "help", "menu"], async (ctx) => {
});

bot.command("more", async (ctx) => {
writeCommandLog(ctx as OnMessageContext)
return ctx.reply(commandsHelpText.more, {
parse_mode: "Markdown",
disable_web_page_preview: true,
});
});

bot.command("terms", (ctx) => {
writeCommandLog(ctx as OnMessageContext)
return ctx.reply(TERMS.text, {
parse_mode: "Markdown",
disable_web_page_preview: true,
});
});

bot.command("support", (ctx) => {
writeCommandLog(ctx as OnMessageContext)
return ctx.reply(SUPPORT.text, {
parse_mode: "Markdown",
disable_web_page_preview: true,
});
});

bot.command("feedback", (ctx) => {
writeCommandLog(ctx as OnMessageContext)
return ctx.reply(FEEDBACK.text, {
parse_mode: "Markdown",
disable_web_page_preview: true,
});
});

bot.command("love", (ctx) => {
writeCommandLog(ctx as OnMessageContext)
return ctx.reply(LOVE.text, {
parse_mode: "Markdown",
disable_web_page_preview: true,
Expand Down Expand Up @@ -398,16 +433,15 @@ bot.catch((err) => {
logger.error(`Error while handling update ${ctx.update.update_id}:`);
const e = err.error;
if (e instanceof GrammyError) {
console.log('Grammy error:', {e});
logger.error("Error in request:", e.description);
logger.error(`Error in message: ${JSON.stringify(ctx.message)}`)
logger.error(`Error in message: ${JSON.stringify(ctx.message)}`);
} else if (e instanceof HttpError) {
logger.error("Could not contact Telegram:", e);
} else {
logger.error("Unknown error:", e);
console.error('global error others', err)
console.error("global error others", err);
}
console.error('global error', err)
console.error("global error", err);
});

bot.errorBoundary((error) => {
Expand All @@ -426,14 +460,14 @@ const httpServer = app.listen(config.port, () => {
// });
});

app.get('/health', (req, res) =>{
res.send('OK').end()
})
app.get("/health", (req, res) => {
res.send("OK").end();
});

app.get('/metrics', async (req, res) =>{
res.setHeader('Content-Type', prometheusRegister.contentType);
app.get("/metrics", async (req, res) => {
res.setHeader("Content-Type", prometheusRegister.contentType);
res.send(await prometheusRegister.metrics());
})
});

const runner = run(bot);

Expand All @@ -442,11 +476,22 @@ const runner = run(bot);
const stopRunner = () => {
httpServer.close();
return runner.isRunning() && runner.stop();
}
};
process.once("SIGINT", stopRunner);
process.once("SIGTERM", stopRunner);

AppDataSource.initialize();
AppDataSource.initialize().then(() => {
const prometheusMetrics = new PrometheusMetrics()
prometheusMetrics.bootstrap()
}).catch((e) => {
logger.error(`Error during DB initialization: ${(e as Error).message}`)
})

if (config.betteruptime.botHeartBitId) {
const task = runBotHeartBit(runner, config.betteruptime.botHeartBitId);
process.once("SIGINT", () => task.stop());
process.once("SIGTERM", () => task.stop());
}

if (config.betteruptime.botHeartBitId) {
const task = runBotHeartBit(runner, config.betteruptime.botHeartBitId);
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ export const FEEDBACK = {
export const LOVE = {
text: `What is Love?`
}

export const BANNED = {
text: 'underage,minor,child,loli,children,kid,young,girl,boy,preteen,prepubescent,Adolescent,schoolgirl,baby,infant,Newborn,Nursery,Kindergarten,Playgroup,Elementary school,Middle school,High school,Family,Sibling,Playground,Toy,Stroller,Diaper,ahegao,pinup,ballgag,Playboy,Bimbo,pleasure,bodily fluids,pleasures,boudoir,rule34,brothel,seducing,dominatrix,seductive,erotic seductive,fuck,sensual,Hardcore,sexy,Hentai,Shag,horny,shibari,incest,Smut,jav,succubus,Jerk off king at pic,thot,kinbaku,transparent,legs spread,twerk,making love,voluptuous,naughty,wincest,orgy,Sultry,XXX,Bondage,Bdsm,Dog collar,Slavegirl,Transparent and Translucent,Arse,Labia,Ass,Mammaries,Human centipede,Badonkers,Minge,Massive chests,Big Ass,Mommy Milker,Booba,Nipple,Booty,Oppai ,Bosom,Organs,Breasts,Ovaries,Busty,Penis,Clunge,Phallus,Crotch,sexy female,Dick,Skimpy,Girth,Thick,Honkers,Vagina,Hooters,Veiny,Knob,no clothes,Speedo,au naturale,no shirt,bare chest,nude,barely dressed,bra,risqué,clear,scantily,clad,cleavage,stripped,full frontal unclothed,invisible clothes,wearing nothing,lingerie with no shirt,naked,without clothes on,negligee,zero clothes,Pornography,Explicit,Adult,NSFW (Not Safe For Work),XXX,Erotic,Sexual,Sensual,Intimate,Nudity,Obscene,Vulgar,Graphic,Hardcore,Fetish,Kink,Erotic art,Erotica,Pornographic,Nude,Provocative,Lewd,Passionate,Seductive,X-rated,Adult content,Sexually explicit,Mature content,Adult entertainment,Erotic imagery,Desirable,Pleasure,Lust,Orgasm,Indecent,Raunchy,Steamy,Uncensored,Naughty,Bedroom,Censored,Bodily,Erogenous,Adult industry,Graphic content,Sensuous,Taboo,Forbidden,Private parts,Erotic literature,Sexual encounter,Intimate moment,Adult film,Provocative pose,Erotic scene,Naked,Swimsuit,Lingerie,Bikini,Underwear,Intercourse,Erogenous zones,Pleasure zones,Kinky,Dominatrix,Swingers,Threesome,Gangbang,BDSM,Escort services,Camgirl/Camboy,Virtual adult entertainment,Phone sex,Porn star,Pornographic materials,Erotic chat,Online dating,Hookup,Cybersex,Explicit language,Sex talk,Sexual innuendo,Condom,Lubricant,Vibrator,Dildo,Prostitution,Adult toys,Sex act names,blowjob,anal,doggy style,Sexual positions,Erotic massage,Nudist/naturist,Exhibitionist,Voyeurism,Adult chat,Online adult communities,Live streaming adult content,Erotic roleplay,Adult dating sites,Erotic,Sexually explicit stories,Nipple,Genitals,Lolicon,Shotacon,porn,Torture,Disturbing,Farts,Fart,Poop,Errect,Big Black,Voluptuous,Seductive,Sperm,Hot,Sexy,Sensored,Censored,Silenced,Deepfake,Inappropriate,Waifu,mp5,Succubus,terrorist,terrorism,suicide,massacre,self-harm,self harm,school shooting,selfharm,overdose,kidnap,gore,mutilate,decapitate,bloodbath,slaughter,SSN,creditcard,passport,address,genocides,holocaust,lynchings,simp,incel,prophet muhammad,prophetmuhammad,nigger,coon,beaner,chink,chinky,gook,kike,'
}
// 0x742c4788CC47A94cf260abc474E2Fa45695a79Cd

// /images vintage hot rod with custom flame paint job
Expand Down
5 changes: 3 additions & 2 deletions src/database/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { Chat } from "./entities/Chat"
import { User } from "./entities/User";
import { StatBotCommand } from "./entities/StatBotCommand";
import config from "../config"
import {BotLog} from "./entities/Log";

export const AppDataSource = new DataSource({
type: "postgres",
url: config.db.url,
entities: [Chat, User, StatBotCommand],
synchronize: false,
entities: [Chat, User, StatBotCommand, BotLog],
synchronize: true,
migrations: ['./src/database/migrations/**/*.{.ts,.js}'],
logging: false,
})
Loading

0 comments on commit 304357f

Please sign in to comment.