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