forked from pinks/eris
1
0
Fork 0
eris/bot/broadcastCommand.ts

74 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-09-24 19:58:09 +00:00
import { CommandContext } from "grammy";
import { bold, fmt, FormattedString } from "grammy_parse_mode";
2023-09-26 10:43:36 +00:00
import { distinctBy } from "std/collections/distinct_by.ts";
2023-10-15 19:13:38 +00:00
import { error, info } from "std/log/mod.ts";
2023-10-23 00:39:01 +00:00
import { adminStore } from "../app/adminStore.ts";
2023-09-24 19:58:09 +00:00
import { generationStore } from "../app/generationStore.ts";
import { formatUserChat } from "../utils/formatUserChat.ts";
2023-10-15 19:13:38 +00:00
import { ErisContext } from "./mod.ts";
2023-09-24 19:58:09 +00:00
export async function broadcastCommand(ctx: CommandContext<ErisContext>) {
if (!ctx.from?.username) {
return ctx.reply("I don't know who you are.");
}
const adminEntry = await adminStore.get(["admins", ctx.from.id]);
2023-09-24 19:58:09 +00:00
if (!adminEntry.versionstamp) {
2023-09-24 19:58:09 +00:00
return ctx.reply("Only a bot admin can use this command.");
}
const text = ctx.match.trim();
if (!text) {
return ctx.reply("Please specify a message to broadcast.");
}
// find users who interacted with bot in the last 24 hours
const gens = await generationStore.getAll(
{ after: new Date(Date.now() - 24 * 60 * 60 * 1000) },
{ reverse: true },
).then((gens) => distinctBy(gens, (gen) => gen.value.from.id));
let sentCount = 0;
const errors: FormattedString[] = [];
const getMessage = () =>
fmt([
fmt`Broadcasted to ${sentCount}/${gens.length} users.\n\n`,
errors.length > 0 ? fmt([bold("Errors:"), "\n", ...errors]) : "",
]);
const replyMessage = await ctx.replyFmt(getMessage(), {
reply_to_message_id: ctx.message?.message_id,
2023-10-05 09:00:51 +00:00
allow_sending_without_reply: true,
2023-09-24 19:58:09 +00:00
});
// send message to each user
for (const gen of gens) {
try {
await ctx.api.sendMessage(gen.value.from.id, text);
2023-10-15 19:13:38 +00:00
info(`Broadcasted to ${formatUserChat({ from: gen.value.from })}`);
2023-09-24 19:58:09 +00:00
sentCount++;
} catch (err) {
2023-10-15 19:13:38 +00:00
error(`Broadcasting to ${formatUserChat({ from: gen.value.from })} failed: ${err}`);
2023-09-24 19:58:09 +00:00
errors.push(fmt`${bold(formatUserChat({ from: gen.value.from }))} - ${err.message}\n`);
}
const fmtMessage = getMessage();
if (sentCount % 20 === 0) {
await ctx.api.editMessageText(
replyMessage.chat.id,
replyMessage.message_id,
fmtMessage.text,
{ entities: fmtMessage.entities },
).catch(() => undefined);
}
}
const fmtMessage = getMessage();
await ctx.api.editMessageText(
replyMessage.chat.id,
replyMessage.message_id,
fmtMessage.text,
{ entities: fmtMessage.entities },
).catch(() => undefined);
}