Added new commands. getGroupStats and registerGroup.
All checks were successful
Main / build-and-push-docker-image (20.x) (push) Successful in 3m3s
All checks were successful
Main / build-and-push-docker-image (20.x) (push) Successful in 3m3s
This commit is contained in:
96
src/bot/features/getGroupStatsCommand.ts
Normal file
96
src/bot/features/getGroupStatsCommand.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
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"]);
|
||||
|
||||
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
|
||||
|
||||
/**
|
||||
* 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) => {
|
||||
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 };
|
||||
65
src/bot/features/registerBotCommand.ts
Normal file
65
src/bot/features/registerBotCommand.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
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<Context>();
|
||||
|
||||
const feature = composer.chatType(["group", "supergroup"]);
|
||||
|
||||
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
|
||||
|
||||
/**
|
||||
* 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) => {
|
||||
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 };
|
||||
@@ -18,6 +18,8 @@ import { metaBlacklist } from "./features/metaBlacklist.js";
|
||||
import { botInfoCommand } from "./features/botInfoCommand.js";
|
||||
import { helpCommand } from "./features/helpCommand.js";
|
||||
import { embedCheck } from "./features/embedCheck.js";
|
||||
import { registerGroup } from "./features/registerBotCommand.js";
|
||||
import { getGroupStats } from "./features/getGroupStatsCommand.js";
|
||||
|
||||
interface Dependencies {
|
||||
config: Config;
|
||||
@@ -70,6 +72,8 @@ export function createBot(
|
||||
// Commands
|
||||
protectedBot.use(botInfoCommand);
|
||||
protectedBot.use(helpCommand);
|
||||
protectedBot.use(registerGroup);
|
||||
protectedBot.use(getGroupStats);
|
||||
|
||||
// Blacklist Feature
|
||||
protectedBot.use(twitterBlacklist);
|
||||
|
||||
Reference in New Issue
Block a user