Added cache exchange urq, fixed eslint, added prettier, upgraded yarn, added statsSite command.
All checks were successful
Main / build-and-push-docker-image (20.x) (push) Successful in 4m58s
All checks were successful
Main / build-and-push-docker-image (20.x) (push) Successful in 4m58s
This commit is contained in:
53
src/bot/features/statsSiteCommand.ts
Normal file
53
src/bot/features/statsSiteCommand.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
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";
|
||||
|
||||
const composer = new Composer<Context>();
|
||||
|
||||
const feature = composer.chatType(["private", "group", "supergroup"]);
|
||||
|
||||
/**
|
||||
* What triggers this feature and adds to the log when it has been triggered.
|
||||
* The trigger is the command "/botInfo"
|
||||
*/
|
||||
feature.hears(
|
||||
/^\/botStats/,
|
||||
logHandle("site-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.
|
||||
if (ctx.chat.type !== "private") {
|
||||
const chatMember = await ctx.getChatMember(ctx.msg.from.id);
|
||||
|
||||
if (!["creator", "administrator"].includes(chatMember.status)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await urql
|
||||
.mutation(increment, { command: true, mutationKey })
|
||||
.toPromise()
|
||||
.then(async () => {
|
||||
if (ctx.msg) {
|
||||
// Replies to the message.
|
||||
return await ctx.reply(
|
||||
`The bot statistics website can be found at https://bot\\-stats\\.werewolfkid\\.monster/\\.`,
|
||||
{
|
||||
parse_mode: "MarkdownV2",
|
||||
reply_parameters: { message_id: ctx.msg.message_id }
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export { composer as statsSiteCommand };
|
||||
@@ -5,7 +5,7 @@ import type { BotConfig } from "grammy";
|
||||
import { unhandledFeature } from "#root/bot/features/unhandled.js";
|
||||
import { welcomeFeature } from "#root/bot/features/welcome.js";
|
||||
import { errorHandler } from "#root/bot/handlers/error.js";
|
||||
import { i18n, isMultipleLocales } from "#root/bot/i18n.js";
|
||||
import { i18n } from "#root/bot/i18n.js";
|
||||
import { session } from "#root/bot/middlewares/session.js";
|
||||
import { updateLogger } from "#root/bot/middlewares/update-logger.js";
|
||||
import { autoChatAction } from "@grammyjs/auto-chat-action";
|
||||
@@ -20,6 +20,7 @@ import { helpCommand } from "./features/helpCommand.js";
|
||||
import { embedCheck } from "./features/embedCheck.js";
|
||||
import { registerGroup } from "./features/registerBotCommand.js";
|
||||
import { getGroupStats } from "./features/getGroupStatsCommand.js";
|
||||
import { statsSiteCommand } from "./features/statsSiteCommand.js";
|
||||
|
||||
interface Dependencies {
|
||||
config: Config;
|
||||
@@ -74,6 +75,7 @@ export function createBot(
|
||||
protectedBot.use(helpCommand);
|
||||
protectedBot.use(registerGroup);
|
||||
protectedBot.use(getGroupStats);
|
||||
protectedBot.use(statsSiteCommand);
|
||||
|
||||
// Blacklist Feature
|
||||
protectedBot.use(twitterBlacklist);
|
||||
|
||||
@@ -8,7 +8,7 @@ import { config } from "#root/config.js";
|
||||
import { logger } from "#root/logger.js";
|
||||
import { createServer, createServerManager } from "#root/server/index.js";
|
||||
import { run } from "@grammyjs/runner";
|
||||
import { Client, fetchExchange } from "@urql/core";
|
||||
import { cacheExchange, Client, fetchExchange } from "@urql/core";
|
||||
|
||||
async function startPolling(config: PollingConfig) {
|
||||
const bot = createBot(config.botToken, {
|
||||
@@ -116,7 +116,7 @@ function onShutdown(cleanUp: () => Promise<void>) {
|
||||
|
||||
export const urql = new Client({
|
||||
url: process.env.GRAPHQL_URL || "",
|
||||
exchanges: [fetchExchange],
|
||||
exchanges: [fetchExchange, cacheExchange],
|
||||
fetchOptions: {
|
||||
headers: {
|
||||
"x-api-key": process.env?.GRAPHQL_API_TOKEN || ""
|
||||
|
||||
Reference in New Issue
Block a user