From def86a3c7e14cb31784d340ea218806ef0dcd92c Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Tue, 19 Mar 2024 11:26:47 -0400 Subject: [PATCH] list: implement last as a reply to the last message --- bot/interfaces.go | 6 ++++++ connectors/discord/discord.go | 14 +++++++++++--- plugins/last/last.go | 32 ++++++++++++++++++++------------ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/bot/interfaces.go b/bot/interfaces.go index 3fb0224..5409ef5 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -63,6 +63,12 @@ type File struct { mime *mimetype.MIME } +type MessageReference struct { + MessageID string `json:"message_id"` + ChannelID string `json:"channel_id,omitempty"` + GuildID string `json:"guild_id,omitempty"` +} + func (f File) Mime() *mimetype.MIME { if f.mime == nil { f.mime = mimetype.Detect(f.Data) diff --git a/connectors/discord/discord.go b/connectors/discord/discord.go index b81ea9c..e37494e 100644 --- a/connectors/discord/discord.go +++ b/connectors/discord/discord.go @@ -116,6 +116,7 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a embeds := []*discordgo.MessageEmbed{} files := []*discordgo.File{} + var ref *discordgo.MessageReference for _, arg := range args { switch a := arg.(type) { @@ -141,13 +142,20 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a ContentType: a.ContentType(), Reader: bytes.NewBuffer(a.Data), }) + case bot.MessageReference: + ref = &discordgo.MessageReference{ + MessageID: a.MessageID, + ChannelID: a.ChannelID, + GuildID: a.GuildID, + } } } data := &discordgo.MessageSend{ - Content: message, - Embeds: embeds, - Files: files, + Content: message, + Embeds: embeds, + Files: files, + Reference: ref, } log.Debug(). diff --git a/plugins/last/last.go b/plugins/last/last.go index 7c0946a..4aad13b 100644 --- a/plugins/last/last.go +++ b/plugins/last/last.go @@ -45,9 +45,10 @@ func (p *LastPlugin) migrate() error { _, err = tx.Exec(`create table if not exists last ( day integer, channel string not null, - ts int not null, - who string not null, - message string not null, + time int not null, + nick string not null, + body string not null, + message_id string not null, constraint last_key primary key (day, channel) on conflict replace )`) if err != nil { @@ -134,8 +135,8 @@ func (p *LastPlugin) recordLast(r bot.Request) bool { } _, err := p.db.Exec( - `insert into last values (?, ?, ?, ?, ?)`, - day.Unix(), ch, time.Now().Unix(), who, r.Msg.Body) + `insert into last (day, channel, time, body, nick, message_id) values (?, ?, ?, ?, ?, ?)`, + day.Unix(), ch, time.Now().Unix(), r.Msg.Body, who, r.Msg.ID) if err != nil { log.Error().Err(err).Msgf("Could not record last.") } @@ -143,11 +144,13 @@ func (p *LastPlugin) recordLast(r bot.Request) bool { } type last struct { - Day int64 - TS int64 - Channel string - Who string - Message string + ID int64 `db:"id"` + Day int64 `db:"day"` + Time int64 `db:"time"` + Channel string `db:"channel"` + Nick string `db:"nick"` + Body string `db:"body"` + MessageID string `db:"message_id"` } func (p *LastPlugin) yesterdaysLast(ch string) (last, error) { @@ -189,6 +192,11 @@ func (p *LastPlugin) sayLast(c bot.Connector, chFrom, chTo string, force bool) { } return } - msg := fmt.Sprintf(`%s killed the channel last night by saying "%s"`, l.Who, l.Message) - p.b.Send(c, bot.Message, chTo, msg) + msg := fmt.Sprintf(`%s killed the channel last night by saying "%s"`, l.Nick, l.Body) + guildID := p.c.Get("discord.guildid", "") + p.b.Send(c, bot.Message, chTo, msg, bot.MessageReference{ + MessageID: l.MessageID, + ChannelID: l.Channel, + GuildID: guildID, + }) }