From f78e83e6f10c2a979c160f290fae314d5b6b2182 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 6 May 2023 23:23:23 +0200 Subject: [PATCH] add phonetic command --- cmd/nourybot/commands.go | 11 +++++ internal/commands/phonetic.go | 89 +++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 internal/commands/phonetic.go diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index eafc962..0fbc215 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -44,6 +44,17 @@ func (app *Application) ParseCommand(evt *event.Event) { } } + case "phonetic": + msg := evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)] + if resp, err := commands.Phonetic(msg); err != nil { + app.Log.Error().Err(err).Msg("failed to handle phonetic command") + app.SendText(evt, "Something went wrong.") + return + } else { + app.SendText(evt, resp) + return + } + case "ping": if resp, err := commands.Ping(); err != nil { app.Log.Error().Err(err).Msg("failed to handle ping command") diff --git a/internal/commands/phonetic.go b/internal/commands/phonetic.go new file mode 100644 index 0000000..3a67cd9 --- /dev/null +++ b/internal/commands/phonetic.go @@ -0,0 +1,89 @@ +package commands + +import "fmt" + +var cm = map[string]string{ + "`": "ё", + "~": "Ё", + "=": "ъ", + "+": "Ъ", + "[": "ю", + "]": "щ", + `\`: "э", + "{": "Ю", + "}": "Щ", + "|": "Э", + ";": "ь", + ":": "Ь", + "'": "ж", + `"`: "Ж", + + "q": "я", + "w": "ш", + "e": "е", + "r": "р", + "t": "т", + "y": "ы", + "u": "у", + "i": "и", + "o": "о", + "p": "п", + "a": "а", + "s": "с", + "d": "д", + "f": "ф", + "g": "г", + "h": "ч", + "j": "й", + "k": "к", + "l": "л", + "z": "з", + "x": "х", + "c": "ц", + "v": "в", + "b": "б", + "n": "н", + "m": "м", + + "Q": "Я", + "W": "Ш", + "E": "Е", + "R": "Р", + "T": "Т", + "Y": "Ы", + "U": "У", + "I": "И", + "O": "О", + "P": "П", + "A": "А", + "S": "С", + "D": "Д", + "F": "Ф", + "G": "Г", + "H": "Ч", + "J": "Й", + "K": "К", + "L": "Л", + "Z": "З", + "X": "Х", + "C": "Ц", + "V": "В", + "B": "Б", + "N": "Н", + "M": "М", +} + +func Phonetic(message string) (string, error) { + var ts string + + for _, c := range message { + if _, ok := cm[string(c)]; ok { + ts = ts + cm[string(c)] + } else { + ts = ts + string(c) + + } + } + + return fmt.Sprint(ts), nil +}