diff --git a/config.go b/config.go index 3ec2e919783a5735398a1014dbe3859e0e62b176..ed94e8a44d163d6ab557162e1d5f6322342f7010 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 085ab3519ce450fdb93ddbc0a21990c6787fa23e..a3e73f1294bea8e88b15d863caa5fcb0f60c5e17 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 f2a98615ac60ad1b961be23c385aadb59c03deec..600ea4ac9c2e04f29d1ed609f4af3a0b9df18e9a 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 (