97 lines
3.4 KiB
TypeScript
97 lines
3.4 KiB
TypeScript
import { Composer } from "grammy";
|
|
import type { Context } from "#root/bot/context.js";
|
|
import { logHandle } from "#root/bot/helpers/logging.js";
|
|
import { urql } from "#root/main.js";
|
|
import increment from "#root/lib/graphql/mutations/incrementMutation.js";
|
|
import getGroupStats from "#root/lib/graphql/queries/getGroupStatsQuery.js";
|
|
|
|
const composer = new Composer<Context>();
|
|
|
|
const feature = composer.chatType(["group", "supergroup"]);
|
|
|
|
/**
|
|
* What triggers this feature and adds to the log when it has been triggered.
|
|
* The trigger is the command "/groupStats"
|
|
*/
|
|
feature.hears(
|
|
/^\/groupStats/,
|
|
logHandle("groups-stats-command"),
|
|
async (ctx: Context) => {
|
|
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
|
|
|
|
await urql.mutation(increment, { trigger: true, mutationKey });
|
|
|
|
// Checks if the context includes a message property.
|
|
if (ctx.msg && ctx.chat && ctx.msg.from) {
|
|
// Doesn't respond to regular users in groups. This is to prevent users spamming the command.
|
|
const chatMember = await ctx.getChatMember(ctx.msg.from.id);
|
|
if (!["creator", "administrator"].includes(chatMember.status)) {
|
|
return;
|
|
}
|
|
|
|
// Stringify the groupID
|
|
const groupID = ctx.chat?.id.toString() || "";
|
|
|
|
// Query to get group stats.
|
|
await urql
|
|
.query(getGroupStats, {
|
|
groupID
|
|
})
|
|
.toPromise()
|
|
.then(async res => {
|
|
// Replies to the message.
|
|
if (ctx.msg) {
|
|
// Check if the group has a document in the database and respond accordingly.
|
|
if (res.data.getGroupStats !== null) {
|
|
const { name, username, linksDeleted } = res.data.getGroupStats;
|
|
|
|
await ctx.reply(
|
|
`Your group is registered in the database as "${name}" ${
|
|
username.length
|
|
? `with a username of ${name}`
|
|
: `without a public username`
|
|
}\\.\n\nThe bot has successfully deleted ${linksDeleted} links from your group\\.`,
|
|
{
|
|
parse_mode: "MarkdownV2",
|
|
reply_parameters: { message_id: ctx.msg.message_id }
|
|
}
|
|
);
|
|
|
|
return await ctx.reply(
|
|
`If you need to update this information you can use the \\/registerGroup command\\.`,
|
|
{
|
|
parse_mode: "MarkdownV2",
|
|
reply_parameters: { message_id: ctx.msg.message_id }
|
|
}
|
|
);
|
|
}
|
|
|
|
// Default response //
|
|
|
|
await ctx.reply(
|
|
`Your group was not found in the database\\. You can use the \\/registerGroup command to add your group to the database\\.`,
|
|
{
|
|
parse_mode: "MarkdownV2",
|
|
reply_parameters: { message_id: ctx.msg.message_id }
|
|
}
|
|
);
|
|
|
|
return await ctx.reply(
|
|
`This is optional\\. If the bot ever removes a link in your group then the group information will be added to the database and tracked from then on\\.`,
|
|
{
|
|
parse_mode: "MarkdownV2",
|
|
reply_parameters: { message_id: ctx.msg.message_id }
|
|
}
|
|
);
|
|
}
|
|
|
|
return await urql
|
|
.mutation(increment, { command: true, mutationKey })
|
|
.toPromise();
|
|
});
|
|
}
|
|
}
|
|
);
|
|
|
|
export { composer as getGroupStats };
|