From 22bba922f95724492ccfe004bae609c846c9538a Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 18 May 2021 18:41:42 +0100 Subject: [PATCH] Discord: Add !lang command --- discord.go | 45 ++++++++++++++++++++++++++++++++++------ lang/telegram/en-us.json | 2 +- storage.go | 1 + telegram.go | 9 ++++---- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/discord.go b/discord.go index 7d6843a..e91eca5 100644 --- a/discord.go +++ b/discord.go @@ -21,7 +21,7 @@ type DiscordDaemon struct { username string tokens map[string]DiscordToken // map of user IDs to tokens. verifiedTokens []DiscordToken - languages map[string]string // Store of languages for user channelIDs. Added to on first interaction, and loaded from app.storage.discord on start. + languages map[string]string // Store of languages for user IDs. Added to on first interaction, and loaded from app.storage.discord on start. app *appContext } @@ -45,7 +45,7 @@ func newDiscordDaemon(app *appContext) (*DiscordDaemon, error) { } for _, user := range app.storage.discord { if user.Lang != "" { - dd.languages[user.ChannelID] = user.Lang + dd.languages[user.ID] = user.Lang } } return dd, nil @@ -98,8 +98,10 @@ func (d *DiscordDaemon) messageHandler(s *dg.Session, m *dg.MessageCreate) { switch msg := sects[0]; msg { case d.app.config.Section("telegram").Key("start_command").MustString("!start"): d.commandStart(s, m, lang) + case "!lang": + d.commandLang(s, m, sects, lang) default: - d.commandPIN(s, m, lang) + d.commandPIN(s, m, sects, lang) } } @@ -112,7 +114,7 @@ func (d *DiscordDaemon) commandStart(s *dg.Session, m *dg.MessageCreate, lang st token := d.NewAuthToken(channel.ID, m.Author.ID, m.Author.Username) d.tokens[m.Author.ID] = token content := d.app.storage.lang.Telegram[lang].Strings.get("startMessage") + "\n" - content += d.app.storage.lang.Telegram[lang].Strings.get("languageMessage") + content += d.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "!lang"}) _, err = s.ChannelMessageSend(channel.ID, content) if err != nil { d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", m.Author.Username, err) @@ -120,9 +122,40 @@ func (d *DiscordDaemon) commandStart(s *dg.Session, m *dg.MessageCreate, lang st } } -func (d *DiscordDaemon) commandPIN(s *dg.Session, m *dg.MessageCreate, lang string) { +func (d *DiscordDaemon) commandLang(s *dg.Session, m *dg.MessageCreate, sects []string, lang string) { + if len(sects) == 1 { + list := "!lang \n" + for code := range d.app.storage.lang.Telegram { + list += fmt.Sprintf("%s: %s\n", code, d.app.storage.lang.Telegram[code].Meta.Name) + } + _, err := s.ChannelMessageSendReply( + m.ChannelID, + list, + m.Reference(), + ) + if err != nil { + d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", m.Author.Username, err) + } + return + } + if _, ok := d.app.storage.lang.Telegram[sects[1]]; ok { + d.languages[m.Author.ID] = sects[1] + for jfID, user := range d.app.storage.discord { + if user.ID == m.Author.ID { + user.Lang = sects[1] + d.app.storage.discord[jfID] = user + if err := d.app.storage.storeDiscordUsers(); err != nil { + d.app.err.Printf("Failed to store Discord users: %v", err) + } + break + } + } + } +} + +func (d *DiscordDaemon) commandPIN(s *dg.Session, m *dg.MessageCreate, sects []string, lang string) { token, ok := d.tokens[m.Author.ID] - if !ok || token.Token != m.Content { + if !ok || token.Token != sects[0] { _, err := s.ChannelMessageSendReply( m.ChannelID, d.app.storage.lang.Telegram[lang].Strings.get("invalidPIN"), diff --git a/lang/telegram/en-us.json b/lang/telegram/en-us.json index 749587d..642399f 100644 --- a/lang/telegram/en-us.json +++ b/lang/telegram/en-us.json @@ -6,6 +6,6 @@ "startMessage": "Hi!\nEnter your Jellyfin PIN code here to verify your account.", "invalidPIN": "That PIN was invalid, try again.", "pinSuccess": "Success! You can now return to the sign-up page.", - "languageMessage": "Note: See available languages with /lang, and set language with /lang ." + "languageMessage": "Note: See available languages with {command}, and set language with {command} ." } } diff --git a/storage.go b/storage.go index add1a9a..61a2e2d 100644 --- a/storage.go +++ b/storage.go @@ -40,6 +40,7 @@ type TelegramUser struct { type DiscordUser struct { ChannelID string + ID string Username string Lang string Contact bool diff --git a/telegram.go b/telegram.go index 1d8acc9..00d5ee3 100644 --- a/telegram.go +++ b/telegram.go @@ -172,7 +172,7 @@ func (t *TelegramDaemon) Shutdown() { func (t *TelegramDaemon) commandStart(upd *tg.Update, sects []string, lang string) { content := t.app.storage.lang.Telegram[lang].Strings.get("startMessage") + "\n" - content += t.app.storage.lang.Telegram[lang].Strings.get("languageMessage") + content += t.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "/lang"}) err := t.Reply(upd, content) if err != nil { t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err) @@ -181,9 +181,9 @@ func (t *TelegramDaemon) commandStart(upd *tg.Update, sects []string, lang strin func (t *TelegramDaemon) commandLang(upd *tg.Update, sects []string, lang string) { if len(sects) == 1 { - list := "/lang \n" + list := "/lang ``\n" for code := range t.app.storage.lang.Telegram { - list += fmt.Sprintf("%s: %s\n", code, t.app.storage.lang.Telegram[code].Meta.Name) + list += fmt.Sprintf("`%s`: %s\n", code, t.app.storage.lang.Telegram[code].Meta.Name) } err := t.Reply(upd, list) if err != nil { @@ -197,8 +197,7 @@ func (t *TelegramDaemon) commandLang(upd *tg.Update, sects []string, lang string if user.ChatID == upd.Message.Chat.ID { user.Lang = sects[1] t.app.storage.telegram[jfID] = user - err := t.app.storage.storeTelegramUsers() - if err != nil { + if err := t.app.storage.storeTelegramUsers(); err != nil { t.app.err.Printf("Failed to store Telegram users: %v", err) } break