From 3c9e38f01fccf8a710d276912f5cb0998eae404a Mon Sep 17 00:00:00 2001 From: Kuro Date: Wed, 19 Aug 2020 23:19:03 +0200 Subject: [PATCH] feat: Insult people that send non text messages at leet time. (#258) - Update contributor. - Add/Update watchLeet related tests. - Only enable debug command in production environment. --- package.json | 2 +- src/commands/__tests__/watchLeet.test.js | 46 ++++++++++++++++++++++++ src/commands/watchLeet.js | 6 +++- src/leetbot.js | 6 ++-- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index adfe62b6..42bde9cb 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ }, { "name": "Alexander Kampf", - "email": "akampftcg@gmail.com" + "email": "mail@akampf.dev" }, { "name": "Noah Hummel", diff --git a/src/commands/__tests__/watchLeet.test.js b/src/commands/__tests__/watchLeet.test.js index 2a97ed1a..7c1ba336 100644 --- a/src/commands/__tests__/watchLeet.test.js +++ b/src/commands/__tests__/watchLeet.test.js @@ -29,6 +29,7 @@ describe("watchLeetCommand", () => { const store = createStore(rootReducer); const chatId = "someChatId"; const mockCtx = { + updateType: "message", chat: { id: chatId }, update: { message: { text: "1337" } }, reply: jest.fn(), @@ -50,6 +51,7 @@ describe("watchLeetCommand", () => { const chatId = "someChatId"; const fromId = "someUserId"; const mockCtx = { + updateType: "message", chat: { id: chatId }, from: { id: fromId }, update: { message: { text: "1337" } }, @@ -72,6 +74,7 @@ describe("watchLeetCommand", () => { const chatId = "someChatId"; const fromId = "someUserId"; const mockCtx = { + updateType: "message", chat: { id: chatId }, from: { id: fromId }, update: { message: { text: "some stupid shit" } }, @@ -89,11 +92,35 @@ describe("watchLeetCommand", () => { timekeeper.reset(); }); + it("replies to non-text messages during leet", () => { + const store = createStore(rootReducer); + const chatId = "someChatId"; + const fromId = "someUserId"; + const mockCtx = { + updateType: "message", + chat: { id: chatId }, + from: { id: fromId }, + update: { message: {} }, + reply: jest.fn(), + }; + + timekeeper.freeze(duringLeet.toDate()); + + translationMiddleware({ i18n, store })(mockCtx, () => {}); + store.dispatch(actions.enableChat(chatId)); + watchLeetCommand({ store, config })(mockCtx); + + expect(mockCtx.reply).toHaveBeenCalled(); + + timekeeper.reset(); + }); + it("replies to multiple 1337 messages from the same person during leet", () => { const store = createStore(rootReducer); const chatId = "someChatId"; const fromId = "someUserId"; const mockCtx = { + updateType: "message", chat: { id: chatId }, from: { id: fromId }, update: { message: { text: "some stupid shit" } }, @@ -112,4 +139,23 @@ describe("watchLeetCommand", () => { timekeeper.reset(); }); + + it("ignores non message update types", () => { + const store = createStore(rootReducer); + const chatId = "someChatId"; + const mockCtx = { + updateType: "something else", + reply: jest.fn(), + }; + + timekeeper.freeze(duringLeet.toDate()); + + translationMiddleware({ i18n, store })(mockCtx, () => {}); + store.dispatch(actions.enableChat(chatId)); + watchLeetCommand({ store, config })(mockCtx); + + expect(mockCtx.reply).not.toHaveBeenCalled(); + + timekeeper.reset(); + }); }); diff --git a/src/commands/watchLeet.js b/src/commands/watchLeet.js index 70aee3b1..fc91bad9 100644 --- a/src/commands/watchLeet.js +++ b/src/commands/watchLeet.js @@ -13,13 +13,17 @@ const telegramUtility = require("../util/telegram"); const watchLeet = ({ store, config: { leetHour, leetMinute, timezone } }) => ( ctx ) => { + if (ctx.updateType !== "message") { + return; + } + const chatId = telegramUtility.chatIdInContext(ctx); if (!getters.isChatEnabled(chatId)(store.getState())) { return; } - const message = telegramUtility.messageInContext(ctx); + const message = telegramUtility.messageInContext(ctx) || ""; if (isCurrentlyLeet(leetHour, leetMinute, timezone)) { if (getters.isLeetInChatAborted(chatId)(store.getState())) { diff --git a/src/leetbot.js b/src/leetbot.js index d22033c1..67bf1830 100644 --- a/src/leetbot.js +++ b/src/leetbot.js @@ -76,9 +76,11 @@ module.exports = (token, config, telegramOptions) => { bot.command("disable", commands.disable({ store })); bot.command("info", commands.info({ store, config })); bot.command("setLanguage", commands.setLanguage({ store })); - bot.command("debug", commands.debug({ store })); + if (process.env.NODE_ENV !== "production") { + bot.command("debug", commands.debug({ store })); + } bot.command("score", commands.score({ store })); - bot.hears(/.*/, commands.watchLeet({ store, config })); + bot.use(commands.watchLeet({ store, config })); // Start the bot. bot.startPolling();