first: make first a reply

This commit is contained in:
Chris Sexton 2024-03-21 08:33:00 -04:00
parent c54aafc491
commit 66c982745d
1 changed files with 37 additions and 25 deletions

View File

@ -35,17 +35,19 @@ type FirstEntry struct {
body string body string
nick string nick string
saved bool saved bool
messageID string
} }
// Insert or update the first entry // Insert or update the first entry
func (fe *FirstEntry) save(db *sqlx.DB) error { func (fe *FirstEntry) save(db *sqlx.DB) error {
if _, err := db.Exec(`insert into first (day, time, channel, body, nick) if _, err := db.Exec(`insert into first (day, time, channel, body, nick, message_id)
values (?, ?, ?, ?, ?)`, values (?, ?, ?, ?, ?, ?)`,
fe.day.Unix(), fe.day.Unix(),
fe.time.Unix(), fe.time.Unix(),
fe.channel, fe.channel,
fe.body, fe.body,
fe.nick, fe.nick,
fe.messageID,
); err != nil { ); err != nil {
return err return err
} }
@ -76,7 +78,8 @@ func New(b bot.Bot) *FirstPlugin {
time integer, time integer,
channel string, channel string,
body string, body string,
nick string nick string,
message_id string
);`) );`)
if err != nil { if err != nil {
log.Fatal(). log.Fatal().
@ -105,9 +108,10 @@ func getLastFirst(db *sqlx.DB, channel string) (*FirstEntry, error) {
var timeEntered sql.NullInt64 var timeEntered sql.NullInt64
var body sql.NullString var body sql.NullString
var nick sql.NullString var nick sql.NullString
var message_id sql.NullString
err := db.QueryRow(`select err := db.QueryRow(`select
id, max(day), time, body, nick from first id, max(day), time, body, nick, message_id from first
where channel = ? where channel = ?
limit 1; limit 1;
`, channel).Scan( `, channel).Scan(
@ -116,6 +120,7 @@ func getLastFirst(db *sqlx.DB, channel string) (*FirstEntry, error) {
&timeEntered, &timeEntered,
&body, &body,
&nick, &nick,
&message_id,
) )
switch { switch {
case err == sql.ErrNoRows || !id.Valid: case err == sql.ErrNoRows || !id.Valid:
@ -134,6 +139,7 @@ func getLastFirst(db *sqlx.DB, channel string) (*FirstEntry, error) {
channel: channel, channel: channel,
body: body.String, body: body.String,
nick: nick.String, nick: nick.String,
messageID: message_id.String,
saved: true, saved: true,
}, nil }, nil
} }
@ -301,6 +307,7 @@ func (p *FirstPlugin) recordFirst(c bot.Connector, message msg.Message) {
channel: message.Channel, channel: message.Channel,
body: message.Body, body: message.Body,
nick: message.User.Name, nick: message.User.Name,
messageID: message.ID,
} }
log.Info().Msgf("recordFirst: %+v", first.day) log.Info().Msgf("recordFirst: %+v", first.day)
err := first.save(p.db) err := first.save(p.db)
@ -338,8 +345,13 @@ func (p *FirstPlugin) leaderboard(c bot.Connector, ch string) error {
func (p *FirstPlugin) announceFirst(c bot.Connector, first *FirstEntry) { func (p *FirstPlugin) announceFirst(c bot.Connector, first *FirstEntry) {
ch := first.channel ch := first.channel
p.bot.Send(c, bot.Message, ch, fmt.Sprintf("%s had first at %s with the message: \"%s\"", guildID := p.config.Get("discord.guildid", "")
first.nick, first.time.Format("15:04"), first.body)) p.bot.Send(c, bot.Message, ch, fmt.Sprintf("%s had first at %s",
first.nick, first.time.Format("15:04")), bot.MessageReference{
MessageID: first.messageID,
ChannelID: first.channel,
GuildID: guildID,
})
} }
// Help responds to help requests. Every plugin must implement a help function. // Help responds to help requests. Every plugin must implement a help function.