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 addGroup from "#root/lib/graphql/mutations/addGroupMutation.js"; const composer = new Composer(); 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 "/registerGroup" */ feature.hears( /^\/registerGroup/, logHandle("register-group-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; } const groupName = ctx.chat?.title || ""; const groupID = ctx.chat?.id.toString() || ""; // Stringify the groupID const groupUsername = ctx.chat?.username || ""; // Add or update the group await urql .mutation(addGroup, { groupID, groupName, groupUsername, mutationKey }) .toPromise() .then(async () => { await urql .mutation(increment, { command: true, mutationKey }) .toPromise(); if (ctx.msg) { // Replies to the message. return await ctx.reply( `Your group has been successfully added to the database\\.`, { parse_mode: "MarkdownV2", reply_parameters: { message_id: ctx.msg.message_id } } ); } }); } } ); export { composer as registerGroup };