Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/commands/autoEhre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ProcessableMessage } from "#service/command.ts";
import type { SpecialCommand } from "#commands/command.ts";

import * as ehreService from "#service/ehre.ts";
import * as botReplyService from "#service/botReply.ts";

export default class AutoEhreCommand implements SpecialCommand {
name = "AutoEhre";
Expand Down Expand Up @@ -39,6 +40,7 @@ export default class AutoEhreCommand implements SpecialCommand {
}

const reply = await ehreService.addEhre(message.author, repliedToMessage.author);
await message.reply(reply);
const replyMessage = await message.reply(reply);
await botReplyService.recordBotReply(message, replyMessage, "auto-ehre");
}
}
5 changes: 4 additions & 1 deletion src/commands/dadJoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { SpecialCommand } from "#commands/command.ts";
import type { BotContext } from "#context.ts";
import { substringAfter } from "#utils/stringUtils.ts";
import { randomEntry } from "#service/random.ts";
import * as botReplyService from "#service/botReply.ts";

type Lang = "german" | "austrian";

Expand Down Expand Up @@ -81,9 +82,11 @@ export default class DadJokeCommand implements SpecialCommand {

const answer = this.#getRandomAnswer(attributes, slots);

await message.reply({
const replyMessage = await message.reply({
content: answer,
});

await botReplyService.recordBotReply(message, replyMessage, "dad-joke");
}
}
}
11 changes: 8 additions & 3 deletions src/commands/deoida.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { BotContext } from "#context.ts";
import type { ProcessableMessage } from "#service/command.ts";
import type { MessageCommand } from "#commands/command.ts";
import * as austrianTranslation from "#storage/austrianTranslation.ts";
import * as botReplyService from "#service/botReply.ts";

async function deOidaLine(line: string): Promise<string> {
// We cannot just split all words using \s*. That could tear apart words or translations like "fescher bub"
Expand Down Expand Up @@ -118,7 +119,8 @@ export default class DeOidaCommand implements MessageCommand {
: message;

if (!messageToTranslate) {
await message.reply("Nichts zum Übersetzen da :question:");
const replyMessage = await message.reply("Nichts zum Übersetzen da :question:");
await botReplyService.recordBotReply(message, replyMessage, "deoida");
return;
}

Expand All @@ -131,17 +133,20 @@ export default class DeOidaCommand implements MessageCommand {
: messageToTranslate.content;

if (!textToTranslate) {
await message.reply("Nichts zum Übersetzen da :question:");
const replyMessage = await message.reply("Nichts zum Übersetzen da :question:");
await botReplyService.recordBotReply(message, replyMessage, "deoida");
return;
}

const translation = await deOida(textToTranslate);

await messageToTranslate.reply({
const replyMessage = await messageToTranslate.reply({
content: `🇦🇹 -> 🇩🇪: ${translation}`,
allowedMentions: {
parse: [],
},
});

await botReplyService.recordBotReply(messageToTranslate, replyMessage, "deoida");
}
}
16 changes: 13 additions & 3 deletions src/commands/download-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import type { BotContext } from "#context.ts";
import type { ApplicationCommand } from "#commands/command.ts";
import * as ytDlService from "#service/ytDl.ts";
import * as botReplyService from "#service/botReply.ts";
import assertNever from "#utils/assertNever.ts";
import TempDir from "#utils/TempDir.ts";

Expand Down Expand Up @@ -65,18 +66,20 @@ export default class DownloadVideoCommand implements ApplicationCommand {
await using tempDir = await TempDir.create("yt-dl");
const result = await ytDlService.downloadVideo(context, tempDir.path, link);
switch (result.result) {
case "aborted":
case "aborted": {
await command.editReply({
content:
"Der Download wurde abgebrochen, da er zu lange gedauert hat. Such dir einfach ein kleineres Video aus.\n\nhurensohn",
});
return;
case "error":
}
case "error": {
await command.editReply({
content: "Es ist irgendein Fehler aufgetreten.\n\nhurensohn",
});
return;
case "success":
}
case "success": {
await command.editReply({
content: result.title ? `**${result.title}**` : null,
files: [
Expand All @@ -85,7 +88,14 @@ export default class DownloadVideoCommand implements ApplicationCommand {
},
],
});

const targetMessage = command.targetMessage;
const reply = await command.fetchReply();
if (targetMessage.inGuild() && reply.inGuild()) {
await botReplyService.recordBotReply(targetMessage, reply, "download-video");
}
return;
}
default:
assertNever(result);
}
Expand Down
5 changes: 4 additions & 1 deletion src/commands/instagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Message } from "discord.js";
import type { SpecialCommand } from "#commands/command.ts";
import type { BotContext } from "#context.ts";
import * as instagramService from "#service/instagram.ts";
import * as botReplyService from "#service/botReply.ts";

const instagramOptions = {
uriPattern: /https?:\/\/(?:www\.)?instagram\.com\/(?:reel|tv|p|share)\/([0-9a-zA-Z_-]+)\/?/gi,
Expand Down Expand Up @@ -75,10 +76,12 @@ export default class InstagramLink implements SpecialCommand {
const files = [...imageFiles, ...videoFiles];

// We need to reply, since we cannot edit a message created by a different user (only remove embeds)
await message.reply({
const reply = await message.reply({
content: "Dein Dreckspost du Hund:",
files,
});

await botReplyService.recordBotReply(message, reply, "instagram");
}
await message.suppressEmbeds(true);
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/nischdaaa.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ProcessableMessage } from "#service/command.ts";
import type { SpecialCommand } from "#commands/command.ts";
import * as botReplyService from "#service/botReply.ts";

export default class NischdaaaCommand implements SpecialCommand {
name = "Nischdaaa";
Expand All @@ -11,8 +12,9 @@ export default class NischdaaaCommand implements SpecialCommand {
}

async handleSpecialMessage(message: ProcessableMessage) {
await message.reply({
const replyMessage = await message.reply({
content: "Nischdaaaa",
});
await botReplyService.recordBotReply(message, replyMessage, "nischdaaa");
}
}
10 changes: 7 additions & 3 deletions src/commands/sdm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import type { ApplicationCommand, MessageCommand } from "#commands/command.ts";
import type { ProcessableMessage } from "#service/command.ts";
import { substringAfter } from "#utils/stringUtils.ts";
import * as botReplyService from "#service/botReply.ts";

const createSecureDecisionMessage = (
question: string,
Expand Down Expand Up @@ -127,7 +128,8 @@ export default class SdmCommand implements MessageCommand, ApplicationCommand {
.filter(s => !!s);

if (!args.length && !isReply) {
await message.reply("Bruder da ist keine Frage :c");
const replyMessage = await message.reply("Bruder da ist keine Frage :c");
await botReplyService.recordBotReply(message, replyMessage, "sdm");
return;
}

Expand All @@ -148,14 +150,16 @@ export default class SdmCommand implements MessageCommand, ApplicationCommand {
});
question = listFormatter.format(options);
const msg = createSecureDecisionMessage(question, message.member, options);
await message.reply(msg);
const reply = await message.reply(msg);
await botReplyService.recordBotReply(message, reply, "sdm");
// Don't delete as it would trigger the messageDeleteHandler
// await message.delete();
return;
}

const msg = createSecureDecisionMessage(question, message.member);
await message.reply(msg);
const reply = await message.reply(msg);
await botReplyService.recordBotReply(message, reply, "sdm");
// Don't delete as it would trigger the messageDeleteHandler
// await message.delete();
return;
Expand Down
4 changes: 3 additions & 1 deletion src/commands/springerWarning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Message } from "discord.js";

import type { SpecialCommand } from "#commands/command.ts";
import type { BotContext } from "#context.ts";
import * as botReplyService from "#service/botReply.ts";

const hosts = {
// Taken from:
Expand Down Expand Up @@ -339,7 +340,8 @@ export default class SpringerWarningCommand implements SpecialCommand {

const alarm = context.emoji.alarm;
await message.suppressEmbeds(true);
await message.reply(`${alarm} Achtung, Link geht zu ${warning} ${alarm}`);
const reply = await message.reply(`${alarm} Achtung, Link geht zu ${warning} ${alarm}`);
await botReplyService.recordBotReply(message, reply, "springer-warning");
}

#getWarning(value: string): string | undefined {
Expand Down
21 changes: 14 additions & 7 deletions src/commands/tiktok.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Message } from "discord.js";

import type { SpecialCommand } from "#commands/command.ts";
import * as botReplyService from "#service/botReply.ts";

const proxitokInstance = "https://proxitok.pussthecat.org";
// const downloadUrlRegex = /href=["'](\/download[^"']*)["']/;
Expand Down Expand Up @@ -31,12 +32,16 @@ export default class TikTokLink implements SpecialCommand {
}

const defaultResponse = () =>
message.reply({
content: `Hab's nicht geschafft aber guck mal hier: ${response.url}`,
allowedMentions: {
repliedUser: false,
},
});
message
.reply({
content: `Hab's nicht geschafft aber guck mal hier: ${response.url}`,
allowedMentions: {
repliedUser: false,
},
})
.then(async reply => {
await botReplyService.recordBotReply(message, reply, "tiktok");
});

const responseString = await response.text();
const linkCandidates = responseString.match(downloadUrlRegex);
Expand All @@ -56,7 +61,7 @@ export default class TikTokLink implements SpecialCommand {

const downloadLink = `${proxitokInstance}${link}`;

await message.reply({
const reply = await message.reply({
content: `Dein TikTok du Hund: <${response.url}>`,
allowedMentions: {
repliedUser: false,
Expand All @@ -68,6 +73,8 @@ export default class TikTokLink implements SpecialCommand {
},
],
});

await botReplyService.recordBotReply(message, reply, "tiktok");
await message.suppressEmbeds(true);
}
}
36 changes: 22 additions & 14 deletions src/handler/messageDeleteHandler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import type { ClientUser, Message } from "discord.js";
import type { Message } from "discord.js";

import type { BotContext } from "#context.ts";

import * as pollService from "#service/poll.ts";
import * as botReplyService from "#service/botReply.ts";

import log from "#log";

const deleteInlineRepliesFromBot = (messageRef: Message<true>, botUser: ClientUser) =>
Promise.allSettled(
messageRef.channel.messages.cache
.filter(m => m.author.id === botUser.id && m.reference?.messageId === messageRef.id)
.map(m => m.delete()),
);
async function deleteInlineRepliesFromBot(message: Message<true>) {
const botReplies = await botReplyService.getBotRepliesForMessage(message);
if (botReplies.length === 0) {
return;
}

const deletePromises = botReplies.map(async reply => {
try {
const botReplyMessage = await message.channel.messages.fetch(reply.botReplyMessageId);
await botReplyMessage.delete();
} catch (error) {
log.warn({ error, replyId: reply.id }, "Failed to delete bot reply message");
}
});

await Promise.allSettled(deletePromises);

await botReplyService.deleteBotRepliesForMessage(message);
}

export default async function (message: Message<true>, context: BotContext) {
if (message.author.id === context.client.user.id) {
Expand All @@ -23,13 +37,7 @@ export default async function (message: Message<true>, context: BotContext) {

await handlePollDeletion(message);

const isNormalCommand =
message.content.startsWith(context.prefix.command) ||
message.content.startsWith(context.prefix.modCommand);

if (isNormalCommand) {
await deleteInlineRepliesFromBot(message, context.client.user);
}
await deleteInlineRepliesFromBot(message);
}

async function handlePollDeletion(message: Message<true>) {
Expand Down
44 changes: 44 additions & 0 deletions src/service/botReply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Message } from "discord.js";

import * as botReplyStorage from "#storage/botReply.ts";
import type { BotReply, BotReplyOrigin } from "#storage/db/model.ts";

export async function recordBotReply(
originalMessage: Message<true>,
botReplyMessage: Message<true>,
origin: BotReplyOrigin,
): Promise<BotReply> {
return await botReplyStorage.recordBotReply({
guildId: originalMessage.guildId,
channelId: originalMessage.channelId,
originalMessageId: originalMessage.id,
botReplyMessageId: botReplyMessage.id,
origin,
});
}

export async function hasRepliedToMessage(message: Message): Promise<boolean> {
return await botReplyStorage.hasRepliedToMessage(message.id);
}

export async function getBotRepliesForMessage(originalMessage: Message): Promise<BotReply[]> {
return await botReplyStorage.getBotRepliesForOriginalMessage(originalMessage.id);
}

export async function getBotReplyByBotReplyMessage(
botReplyMessage: Message,
): Promise<BotReply | undefined> {
return await botReplyStorage.getBotReplyByBotReplyMessageId(botReplyMessage.id);
}

export async function getBotRepliesByOrigin(origin: BotReplyOrigin): Promise<BotReply[]> {
return await botReplyStorage.getBotRepliesByOrigin(origin);
}

export async function deleteBotRepliesForMessage(originalMessage: Message): Promise<void> {
await botReplyStorage.deleteBotRepliesForOriginalMessage(originalMessage.id);
}

export async function deleteBotReply(id: number): Promise<BotReply> {
return await botReplyStorage.deleteBotReply(id);
}
Loading