1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

Discord: Add !lang command

This commit is contained in:
Harvey Tindall 2021-05-18 18:41:42 +01:00
parent d928df7ab2
commit 22bba922f9
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
4 changed files with 45 additions and 12 deletions

View File

@ -21,7 +21,7 @@ type DiscordDaemon struct {
username string username string
tokens map[string]DiscordToken // map of user IDs to tokens. tokens map[string]DiscordToken // map of user IDs to tokens.
verifiedTokens []DiscordToken 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 app *appContext
} }
@ -45,7 +45,7 @@ func newDiscordDaemon(app *appContext) (*DiscordDaemon, error) {
} }
for _, user := range app.storage.discord { for _, user := range app.storage.discord {
if user.Lang != "" { if user.Lang != "" {
dd.languages[user.ChannelID] = user.Lang dd.languages[user.ID] = user.Lang
} }
} }
return dd, nil return dd, nil
@ -98,8 +98,10 @@ func (d *DiscordDaemon) messageHandler(s *dg.Session, m *dg.MessageCreate) {
switch msg := sects[0]; msg { switch msg := sects[0]; msg {
case d.app.config.Section("telegram").Key("start_command").MustString("!start"): case d.app.config.Section("telegram").Key("start_command").MustString("!start"):
d.commandStart(s, m, lang) d.commandStart(s, m, lang)
case "!lang":
d.commandLang(s, m, sects, lang)
default: 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) token := d.NewAuthToken(channel.ID, m.Author.ID, m.Author.Username)
d.tokens[m.Author.ID] = token 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("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) _, err = s.ChannelMessageSend(channel.ID, content)
if err != nil { if err != nil {
d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", m.Author.Username, err) 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 <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] token, ok := d.tokens[m.Author.ID]
if !ok || token.Token != m.Content { if !ok || token.Token != sects[0] {
_, err := s.ChannelMessageSendReply( _, err := s.ChannelMessageSendReply(
m.ChannelID, m.ChannelID,
d.app.storage.lang.Telegram[lang].Strings.get("invalidPIN"), d.app.storage.lang.Telegram[lang].Strings.get("invalidPIN"),

View File

@ -6,6 +6,6 @@
"startMessage": "Hi!\nEnter your Jellyfin PIN code here to verify your account.", "startMessage": "Hi!\nEnter your Jellyfin PIN code here to verify your account.",
"invalidPIN": "That PIN was invalid, try again.", "invalidPIN": "That PIN was invalid, try again.",
"pinSuccess": "Success! You can now return to the sign-up page.", "pinSuccess": "Success! You can now return to the sign-up page.",
"languageMessage": "Note: See available languages with /lang, and set language with /lang <language code>." "languageMessage": "Note: See available languages with {command}, and set language with {command} <language code>."
} }
} }

View File

@ -40,6 +40,7 @@ type TelegramUser struct {
type DiscordUser struct { type DiscordUser struct {
ChannelID string ChannelID string
ID string
Username string Username string
Lang string Lang string
Contact bool Contact bool

View File

@ -172,7 +172,7 @@ func (t *TelegramDaemon) Shutdown() {
func (t *TelegramDaemon) commandStart(upd *tg.Update, sects []string, lang string) { 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("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) err := t.Reply(upd, content)
if err != nil { if err != nil {
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err) 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) { func (t *TelegramDaemon) commandLang(upd *tg.Update, sects []string, lang string) {
if len(sects) == 1 { if len(sects) == 1 {
list := "/lang <lang>\n" list := "/lang `<lang>`\n"
for code := range t.app.storage.lang.Telegram { 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) err := t.Reply(upd, list)
if err != nil { 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 { if user.ChatID == upd.Message.Chat.ID {
user.Lang = sects[1] user.Lang = sects[1]
t.app.storage.telegram[jfID] = user t.app.storage.telegram[jfID] = user
err := t.app.storage.storeTelegramUsers() if err := t.app.storage.storeTelegramUsers(); err != nil {
if err != nil {
t.app.err.Printf("Failed to store Telegram users: %v", err) t.app.err.Printf("Failed to store Telegram users: %v", err)
} }
break break