list: implement last as a reply to the last message

This commit is contained in:
Chris Sexton 2024-03-19 11:26:47 -04:00
parent 35499ee213
commit def86a3c7e
3 changed files with 37 additions and 15 deletions

View File

@ -63,6 +63,12 @@ type File struct {
mime *mimetype.MIME 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 { func (f File) Mime() *mimetype.MIME {
if f.mime == nil { if f.mime == nil {
f.mime = mimetype.Detect(f.Data) f.mime = mimetype.Detect(f.Data)

View File

@ -116,6 +116,7 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a
embeds := []*discordgo.MessageEmbed{} embeds := []*discordgo.MessageEmbed{}
files := []*discordgo.File{} files := []*discordgo.File{}
var ref *discordgo.MessageReference
for _, arg := range args { for _, arg := range args {
switch a := arg.(type) { switch a := arg.(type) {
@ -141,13 +142,20 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a
ContentType: a.ContentType(), ContentType: a.ContentType(),
Reader: bytes.NewBuffer(a.Data), Reader: bytes.NewBuffer(a.Data),
}) })
case bot.MessageReference:
ref = &discordgo.MessageReference{
MessageID: a.MessageID,
ChannelID: a.ChannelID,
GuildID: a.GuildID,
}
} }
} }
data := &discordgo.MessageSend{ data := &discordgo.MessageSend{
Content: message, Content: message,
Embeds: embeds, Embeds: embeds,
Files: files, Files: files,
Reference: ref,
} }
log.Debug(). log.Debug().

View File

@ -45,9 +45,10 @@ func (p *LastPlugin) migrate() error {
_, err = tx.Exec(`create table if not exists last ( _, err = tx.Exec(`create table if not exists last (
day integer, day integer,
channel string not null, channel string not null,
ts int not null, time int not null,
who string not null, nick string not null,
message string not null, body string not null,
message_id string not null,
constraint last_key primary key (day, channel) on conflict replace constraint last_key primary key (day, channel) on conflict replace
)`) )`)
if err != nil { if err != nil {
@ -134,8 +135,8 @@ func (p *LastPlugin) recordLast(r bot.Request) bool {
} }
_, err := p.db.Exec( _, err := p.db.Exec(
`insert into last values (?, ?, ?, ?, ?)`, `insert into last (day, channel, time, body, nick, message_id) values (?, ?, ?, ?, ?, ?)`,
day.Unix(), ch, time.Now().Unix(), who, r.Msg.Body) day.Unix(), ch, time.Now().Unix(), r.Msg.Body, who, r.Msg.ID)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Could not record last.") log.Error().Err(err).Msgf("Could not record last.")
} }
@ -143,11 +144,13 @@ func (p *LastPlugin) recordLast(r bot.Request) bool {
} }
type last struct { type last struct {
Day int64 ID int64 `db:"id"`
TS int64 Day int64 `db:"day"`
Channel string Time int64 `db:"time"`
Who string Channel string `db:"channel"`
Message string Nick string `db:"nick"`
Body string `db:"body"`
MessageID string `db:"message_id"`
} }
func (p *LastPlugin) yesterdaysLast(ch string) (last, error) { 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 return
} }
msg := fmt.Sprintf(`%s killed the channel last night by saying "%s"`, l.Who, l.Message) msg := fmt.Sprintf(`%s killed the channel last night by saying "%s"`, l.Nick, l.Body)
p.b.Send(c, bot.Message, chTo, msg) guildID := p.c.Get("discord.guildid", "")
p.b.Send(c, bot.Message, chTo, msg, bot.MessageReference{
MessageID: l.MessageID,
ChannelID: l.Channel,
GuildID: guildID,
})
} }