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

Dev to Master #168

Merged
merged 74 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
5baa3ce
SD Images: multiple models mode
potvik Aug 20, 2023
468b78d
Sd images: refactoring
potvik Aug 20, 2023
d263748
Sd images: refactoring
potvik Aug 20, 2023
9536b11
Sd images: refactoring
potvik Aug 20, 2023
03746cb
Sd images: refactoring
potvik Aug 20, 2023
d2c76fa
Sd images: refactoring
potvik Aug 20, 2023
e1456cc
SD Images: added 3 XL models
potvik Aug 21, 2023
1df98d8
SD images: added --model params
potvik Aug 21, 2023
060e1b7
SD Images: multiple models, more commands and helpers
potvik Aug 22, 2023
80a6b73
SD Images: added default prompts for each model
potvik Aug 23, 2023
25235f0
SD Images: added mul models for many img mode
potvik Aug 23, 2023
f77494a
SD Images: added /all cmd for model testing
potvik Aug 23, 2023
f7dc60d
add onError method to OpenAIBot + add OpenAI.ApiError basic handling
fegloff Aug 23, 2023
b12bee8
Merge pull request #154 from harmony-one/botstats
theofandrich Aug 23, 2023
6b167ab
refactor error handling on controller and api + add await to cpx.repl…
fegloff Aug 23, 2023
393354d
added list of banned words on constant.ts
theofandrich Aug 23, 2023
43fef2d
fix error handling for edit messages on completion streaming + add aw…
fegloff Aug 24, 2023
02ede30
await changes
fegloff Aug 24, 2023
a9514c8
minor changes
fegloff Aug 24, 2023
e95c75b
add queue logic for chat gpt
fegloff Aug 24, 2023
1f73cb9
Merge branch 'dev' into openai-queue
fegloff Aug 24, 2023
bf6dbd1
fix await calls for openAI onEvent method
fegloff Aug 24, 2023
7d7bb21
Added monitoring
ahiipsa Aug 24, 2023
fb17d6a
Merge pull request #171 from harmony-one/monitoring
ahiipsa Aug 24, 2023
ec640ce
Merge branch 'dev' into sd_images_mul_models_2
potvik Aug 24, 2023
dcf43fe
Merge pull request #161 from harmony-one/sd_images_mul_models_2
theofandrich Aug 24, 2023
8c8642e
Fixed /stats report
ahiipsa Aug 24, 2023
607968e
Merge pull request #173 from harmony-one/botstats01
ahiipsa Aug 24, 2023
68e7f91
updated format
theofandrich Aug 24, 2023
31cb9b4
Merge pull request #176 from harmony-one/botstats
theofandrich Aug 24, 2023
51a1c8c
Hotfix /botstats
ahiipsa Aug 24, 2023
29e9c6d
minor changes
fegloff Aug 24, 2023
a235863
add MAX_TRIES constant
fegloff Aug 24, 2023
a12d03d
Merge branch 'dev' into openai-queue
fegloff Aug 24, 2023
054c4b7
Add payment log
ArtemKolodko Aug 24, 2023
0fcf5f2
Merge pull request #169 from harmony-one/openai-upgrade
fegloff Aug 24, 2023
0196328
Merge branch 'dev' into openai-queue
fegloff Aug 24, 2023
e8ceb73
Fixed openai getEstimate price function
ahiipsa Aug 24, 2023
fc533c1
Add messages log
ArtemKolodko Aug 24, 2023
bd6492a
Merge branch 'dev' into add_payment_log
ArtemKolodko Aug 24, 2023
f4e254b
Merge pull request #180 from harmony-one/hotfix05
theofandrich Aug 24, 2023
934de31
Merge pull request #170 from harmony-one/openai-queue
theofandrich Aug 24, 2023
57a4722
fix onChatHandler
fegloff Aug 24, 2023
68ee573
fix getEstimatedPrice call
fegloff Aug 24, 2023
1183a31
Merge pull request #182 from harmony-one/openai-queue
fegloff Aug 24, 2023
0da60d0
cleaned up default prompts
theofandrich Aug 24, 2023
1146fb6
Rename log to logs
ArtemKolodko Aug 24, 2023
aff5f84
Uncomment stats logs
ArtemKolodko Aug 24, 2023
c0e07da
Merge branch 'dev' into add_payment_log
ArtemKolodko Aug 24, 2023
8d33c33
SD Images: added new aliases
potvik Aug 24, 2023
fc0fb21
SD Images: set xl model by default
potvik Aug 24, 2023
a1c3d6f
SD Images: added default image for xl model
potvik Aug 24, 2023
fefad5e
Add DAU metrics
ArtemKolodko Aug 24, 2023
0e7543e
SD Images: correct aliases
potvik Aug 24, 2023
95078b5
Minor update
ArtemKolodko Aug 24, 2023
490c9a6
SD Images: refactoring
potvik Aug 24, 2023
2b94880
SD Images: refactoring
potvik Aug 24, 2023
211df01
SD Images: refactoring
potvik Aug 24, 2023
e0b17ef
Reverted package-lock.lock
potvik Aug 24, 2023
a11df7e
updated prompt
theofandrich Aug 24, 2023
8026fac
Merge pull request #183 from harmony-one/sd_images_aliases
theofandrich Aug 24, 2023
fcbd459
add a. alias + remove unused code
fegloff Aug 24, 2023
f2a3bae
Add --ar <w>:<h> flag for image promtps
ONEJulia Aug 24, 2023
bbd2bcc
Merge pull request #181 from harmony-one/add_payment_log
theofandrich Aug 24, 2023
d66a55f
allow caseless prefix
fegloff Aug 24, 2023
6588cda
Merge pull request #184 from harmony-one/ask-alias
theofandrich Aug 24, 2023
0fb4093
Merge pull request #185 from Aishlia/dev
theofandrich Aug 24, 2023
8f6024f
minor changes
fegloff Aug 24, 2023
ba64d70
Merge branch 'dev' of https://github.com/harmony-one/HarmonyOneBot in…
fegloff Aug 24, 2023
0cb3a0a
Add --cfg flag to prompts
Aishlia Aug 24, 2023
6c932a8
Merge pull request #186 from Aishlia/dev
Aishlia Aug 24, 2023
6c348f8
reverted default image changes
theofandrich Aug 25, 2023
5b4e8fa
Merge pull request #187 from harmony-one/sd_images_aliases
theofandrich Aug 25, 2023
b969e76
Merge branch 'master' into dev
theofandrich Aug 25, 2023
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
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