From e7e40f74e61847b3ecf7eb173588220a12dad900 Mon Sep 17 00:00:00 2001
From: Benedikt Magnus <magnus@magnuscraft.de>
Date: Tue, 16 Nov 2021 14:21:47 +0100
Subject: [PATCH] Possibility to send messages to gift giver/taker

---
 locale/de-DE.commands.json                    |  8 ++++
 locale/de-DE.texts.json                       |  6 +++
 scripts/utility/localisation.ts               |  8 ++++
 .../wichtelbot/message/handlingDefinition.ts  | 47 +++++++++++++++++++
 .../message/handlingTools/stateAndText.ts     |  8 ++++
 .../message/modules/generalModule.ts          | 34 +++++++++++++-
 6 files changed, 110 insertions(+), 1 deletion(-)
 create mode 100644 scripts/wichtelbot/message/handlingTools/stateAndText.ts

diff --git a/locale/de-DE.commands.json b/locale/de-DE.commands.json
index 5ccc362..c4793b6 100644
--- a/locale/de-DE.commands.json
+++ b/locale/de-DE.commands.json
@@ -145,6 +145,14 @@
             "Großen Dank"
         ]
     },
+    "writeOwnGiftGiver": {
+        "commands": ["Meinem geheimen Wichtelpaten schreiben"],
+        "info": "Schreibe deinem Wichtelpaten eine Nachricht, den du nicht kennst, der aber weiß, wer du bist. Du darfst dabei frei über dich erzählen."
+    },
+    "writeOwnGiftTaker": {
+        "commands": ["Meinem mir bekannten Wichtelkind schreiben"],
+        "info": "Schreibe deinem Wichtekind, den du zwar kennst, der aber nicht weiß, wer du bist. Du solltest dich dabei stark zurückhalten und nur das Nötigste zusammenfassen, um eine Erkennung zu vermeiden."
+    },
     "yes": {
         "commands": ["ja", "jap", "jawohl", "okay", "ok", "meinetwegen"]
     },
diff --git a/locale/de-DE.texts.json b/locale/de-DE.texts.json
index 8cf1332..d0f7cdf 100644
--- a/locale/de-DE.texts.json
+++ b/locale/de-DE.texts.json
@@ -37,6 +37,10 @@
     "noCommandsAvailable": "Derzeit stehen keine besonderen Befehle zur Verfügung.",
     "notUnderstood": "Häää? Probiers nochmal.",
     "oldInformation": "Dies findet sich aktuell dazu in meiner Datenbank:\n>>> {var.informationValue}",
+    "messageFromGiftGiverOrTaker": "*hust*",
+    "messageFromGiftGiverHeadline": "Von deinem Wichtelpaten...",
+    "messageFromGiftTakerHeadline": "Von deinem Wichtelkind...",
+    "messageToGiftGiverOrTakerSent": "Ich reiche deine stille Post weiter... hoffentlich kann ich mir all das fehlerfrei merken...",
     "moderationNeedHelp": "<@&{var.moderationRoleId}> Der Wichtel {contact.nickname} ({contact.tag}) bittet um Hilfe.",
     "moderationRegistrationEnded": "Registrierungsphase beendet. \nAnzahl Wartender: {var.waitingMemberCount}",
     "moderationProfilesDistributed": "Alle {var.profileCount} Steckbriefe wurden verteilt.",
@@ -60,5 +64,7 @@
     "sternenrose": "Vielen Dank, Sterni! :)",
     "thankYouResponse": "Bitte!",
     "wichtelProfileDistribution": "Hallo {contact.nickname},\ndie Auslosung ist vorüber!\nHier ist der Steckbrief deines Wichtelkindes:",
+    "writeOwnGiftGiver": "Was möchtest du deinem Wichtelpaten schreiben? \nDenke daran, dies ist nur für wirklich wichtige Anmerkungen und Korrekturen gedacht, nicht für Pläuschchen! \n\n...\nIm Ernst! Ich bekomme das mit!",
+    "writeOwnGiftTaker": "Was möchtest du deinem Wichtelkind schreiben? \nBitte denke daran, dass dein Wichtelkind nicht weiß, wer du bist und das auch so bleiben sollte. Es wäre nicht schön, wenn die Überraschung vor dem Wichtelstream ruiniert würde! \nSchreibe ihm also bitte **nur**, wenn es **wirklich** nicht anders geht. Und wenn, dann halte dich kurz, denn schon dein Schreibstil könnte dich schnell verraten!",
     "yourAreWelcomeRespone": "🙂"
 }
diff --git a/scripts/utility/localisation.ts b/scripts/utility/localisation.ts
index 97d4d5f..8732849 100644
--- a/scripts/utility/localisation.ts
+++ b/scripts/utility/localisation.ts
@@ -35,6 +35,8 @@ interface Commands
     registration: CommandInfo;
     sternenrose: CommandInfo;
     thankYou: CommandInfo;
+    writeOwnGiftGiver: CommandInfo;
+    writeOwnGiftTaker: CommandInfo;
     yes: CommandInfo;
     yourAreWelcome: CommandInfo;
 }
@@ -80,6 +82,10 @@ interface Texts
     noCommandsAvailable: TokenString;
     notUnderstood: TokenString;
     oldInformation: TokenString;
+    messageFromGiftGiverOrTaker: TokenString;
+    messageFromGiftGiverHeadline: TokenString;
+    messageFromGiftTakerHeadline: TokenString;
+    messageToGiftGiverOrTakerSent: TokenString;
     moderationNeedHelp: TokenString; // TODO: This includes a Discord specific mention which is not portable to other implementations.
     moderationRegistrationEnded: TokenString;
     moderationProfilesDistributed: TokenString;
@@ -103,6 +109,8 @@ interface Texts
     sternenrose: TokenString;
     thankYouResponse: TokenString;
     wichtelProfileDistribution: TokenString;
+    writeOwnGiftGiver: TokenString;
+    writeOwnGiftTaker: TokenString;
     yourAreWelcomeRespone: TokenString;
 }
 
diff --git a/scripts/wichtelbot/message/handlingDefinition.ts b/scripts/wichtelbot/message/handlingDefinition.ts
index f8009ee..0e2fbe9 100644
--- a/scripts/wichtelbot/message/handlingDefinition.ts
+++ b/scripts/wichtelbot/message/handlingDefinition.ts
@@ -10,6 +10,7 @@ import Message from '../endpoint/definitions/message';
 import { ModerationModule } from './modules/moderationModule';
 import State from "../endpoint/definitions/state";
 import TokenString from '../../utility/tokenString';
+import { StateAndText } from './handlingTools/stateAndText';
 
 interface CommandDefinition
 {
@@ -486,6 +487,52 @@ export default class HandlingDefinition
                     await this.generalModule.continue(message, State.Waiting, Localisation.texts.deregistrationCancelled);
                 }
             }
+        },
+        // Wichtel things:
+        {
+            state: State.Wichteling,
+            expectsComponentResult: false,
+            paths: [
+                {
+                    command: Localisation.commands.writeOwnGiftGiver,
+                    result: {
+                        state: State.MessageToGiftGiver,
+                        text: Localisation.texts.writeOwnGiftGiver
+                    },
+                },
+                {
+                    command: Localisation.commands.writeOwnGiftTaker,
+                    result: {
+                        state: State.MessageToGiftTaker,
+                        text: Localisation.texts.writeOwnGiftTaker
+                    },
+                }
+            ],
+            handlerFunction: async (message: Message, result: StateAndText): Promise<void> =>
+            {
+                await this.generalModule.continue(message, result.state, result.text);
+            }
+        },
+        // Message to gift giver/taker:
+        {
+            state: State.MessageToGiftGiver,
+            expectsComponentResult: false,
+            paths: null,
+            handlerFunction: async (message: Message): Promise<void> =>
+            {
+                await this.generalModule.sendMessageToOwnGiftGiver(message);
+                await this.generalModule.continue(message, State.Wichteling, Localisation.texts.messageToGiftGiverOrTakerSent);
+            }
+        },
+        {
+            state: State.MessageToGiftTaker,
+            expectsComponentResult: false,
+            paths: null,
+            handlerFunction: async (message: Message): Promise<void> =>
+            {
+                await this.generalModule.sendMessageToOwnGiftTaker(message);
+                await this.generalModule.continue(message, State.Wichteling, Localisation.texts.messageToGiftGiverOrTakerSent);
+            }
         }
     ];
 
diff --git a/scripts/wichtelbot/message/handlingTools/stateAndText.ts b/scripts/wichtelbot/message/handlingTools/stateAndText.ts
new file mode 100644
index 0000000..82c70f3
--- /dev/null
+++ b/scripts/wichtelbot/message/handlingTools/stateAndText.ts
@@ -0,0 +1,8 @@
+import { State } from "../../endpoint/definitions";
+import TokenString from "../../../utility/tokenString";
+
+export interface StateAndText
+{
+    state: State,
+    text: TokenString,
+}
diff --git a/scripts/wichtelbot/message/modules/generalModule.ts b/scripts/wichtelbot/message/modules/generalModule.ts
index f851f05..0189ed8 100644
--- a/scripts/wichtelbot/message/modules/generalModule.ts
+++ b/scripts/wichtelbot/message/modules/generalModule.ts
@@ -1,5 +1,5 @@
 import Localisation, { CommandInfo } from '../../../utility/localisation';
-import { Additions } from '../../endpoint/definitions';
+import { Additions, Visualisation, VisualisationType } from '../../endpoint/definitions';
 import Config from '../../../utility/config';
 import Contact from '../../classes/contact';
 import ContactType from '../../types/contactType';
@@ -232,4 +232,36 @@ export default class GeneralModule
             'https://cdn.discordapp.com/attachments/391928490456514561/394095185275125760/Weihn8.jpg'
         );
     }
+
+    public async sendMessageToOwnGiftGiver (message: Message): Promise<void>
+    {
+        const wichtel = this.database.getWichtel(message.author.id);
+
+        const giverUser = await message.client.fetchUser(wichtel.giverId);
+
+        const messageText = Localisation.texts.messageFromGiftGiverOrTaker.process(message.author);
+        const messageVisualisation: Visualisation = {
+            headline: Localisation.texts.messageFromGiftTakerHeadline.process(message.author),
+            text: message.content,
+            type: VisualisationType.Normal
+        };
+
+        await giverUser.send(messageText, [messageVisualisation]);
+    }
+
+    public async sendMessageToOwnGiftTaker (message: Message): Promise<void>
+    {
+        const wichtel = this.database.getWichtel(message.author.id);
+
+        const takerUser = await message.client.fetchUser(wichtel.takerId);
+
+        const messageText = Localisation.texts.messageFromGiftGiverOrTaker.process(message.author);
+        const messageVisualisation: Visualisation = {
+            headline: Localisation.texts.messageFromGiftGiverHeadline.process(message.author),
+            text: message.content,
+            type: VisualisationType.Normal
+        };
+
+        await takerUser.send(messageText, [messageVisualisation]);
+    }
 }
-- 
GitLab