From 3c12eaa9074c9440493fad369d49ac73ef770922 Mon Sep 17 00:00:00 2001 From: RandomChars <random@chars.jp> Date: Tue, 9 Nov 2021 10:31:17 +0900 Subject: [PATCH] copy value before taking address, lock message create handler to make sure messages arrive in the order they're received, relay discord message updates, return after calling telegram command handler --- config.go | 7 ++++--- discord.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- telegram.go | 1 + 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 3ec2e91..ed94e8a 100644 --- a/config.go +++ b/config.go @@ -95,15 +95,16 @@ func setupConfig() { } for _, c := range config.Bridges.Item { + p := c if c.Discord.Enable { if c.Discord.ID < 0 { - discordPMQueue[strconv.Itoa(c.Discord.ID)] = &c + discordPMQueue[strconv.Itoa(c.Discord.ID)] = &p } else { - discordBridge[strconv.Itoa(c.Discord.ID)] = &c + discordBridge[strconv.Itoa(c.Discord.ID)] = &p } } if c.Telegram.Enable { - telegramBridge[int64(c.Telegram.ID)] = &c + telegramBridge[int64(c.Telegram.ID)] = &p } } } diff --git a/discord.go b/discord.go index 085ab35..a3e73f1 100644 --- a/discord.go +++ b/discord.go @@ -7,11 +7,13 @@ import ( "log" "strconv" "strings" + "sync" ) var ( - session *discordgo.Session - application *discordgo.Application + session *discordgo.Session + application *discordgo.Application + discordMutex = make(map[string]*sync.Mutex) ) func init() { @@ -76,6 +78,10 @@ func openDiscord() { } } + for id := range discordBridge { + discordMutex[id] = &sync.Mutex{} + } + ready <- struct{}{} } @@ -83,6 +89,7 @@ var idReference = make(map[string]int) func handleDiscord() { session.AddHandler(discordHandleCreate) + session.AddHandler(discordHandleUpdate) } func discordHandleCreate(session *discordgo.Session, create *discordgo.MessageCreate) { @@ -95,6 +102,9 @@ func discordHandleCreate(session *discordgo.Session, create *discordgo.MessageCr return } + discordMutex[create.ChannelID].Lock() + defer discordMutex[create.ChannelID].Unlock() + setTelegramPreviousCaller(int64(tc.ID), -1) if create.Message == nil || create.Message.Author == nil { @@ -148,6 +158,42 @@ func discordHandleCreate(session *discordgo.Session, create *discordgo.MessageCr create.Message.Content) } +func discordHandleUpdate(session *discordgo.Session, update *discordgo.MessageUpdate) { + if update.Author == nil || update.Author.ID == session.State.User.ID { + return + } + + if update.Message == nil || update.Message.Author == nil { + if config.System.Verbose { + log.Printf("got update with missing fields") + } + return + } + + var tid int + if id, ok := idReference[update.Message.ID]; !ok { + return + } else { + tid = id + } + + dc, tc := discordGetConf(update.ChannelID) + if dc == nil || tc == nil { + return + } + + edit := tgbotapi.NewEditMessageText(int64(tc.ID), tid, discordMakeHeader(update.Message.Author)+update.Message.Content) + edit.ParseMode = "Markdown" + if _, err := botAPI.Send(edit); err != nil { + log.Printf("error relaying edit on message %s: %s", update.Message.ID, err) + return + } + log.Printf("D%vM%s -> T%vM%v %s#%s (%s) [edit]: %s", + dc.ID, update.Message.ID, tc.ID, idReference[update.Message.ID], + update.Message.Author.Username, update.Message.Author.Discriminator, update.Message.Author.ID, + update.Message.Content) +} + func discordGetConf(id string) (dc, tc *bridgePlatformConf) { if c, ok := discordBridge[id]; !ok { if config.System.Verbose { diff --git a/telegram.go b/telegram.go index f2a9861..600ea4a 100644 --- a/telegram.go +++ b/telegram.go @@ -129,6 +129,7 @@ func respondTelegram(update tgbotapi.Update) { if update.Message.IsCommand() { go telegramCommand(update) + return } var ( -- GitLab