Skip to content

Commit 8917535

Browse files
committed
Add bot context
1 parent 64c048e commit 8917535

29 files changed

+87
-76
lines changed

bot/handlers.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bot
22

33
import (
4+
"context"
45
"goirc/events"
56

67
irc "github.com/thoj/go-ircevent"
@@ -19,4 +20,4 @@ func (hp *HandlerParams) Publish(eventName string, payload any) {
1920
events.Publish(eventName, payload)
2021
}
2122

22-
type HandlerFunction func(HandlerParams) error
23+
type HandlerFunction func(context.Context, HandlerParams) error

bot/irc.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (b *Bot) Repeat(timeout time.Duration, action HandlerFunction) {
6464
go func() {
6565
for {
6666
time.Sleep(timeout)
67-
err := action(HandlerParams{
67+
err := action(context.TODO(), HandlerParams{
6868
Privmsgf: b.MakePrivmsgf(),
6969
Target: b.Channel,
7070
})
@@ -77,7 +77,7 @@ func (b *Bot) Repeat(timeout time.Duration, action HandlerFunction) {
7777

7878
func (b *Bot) IdleRepeat(timeout time.Duration, action HandlerFunction) {
7979
reset := idle.Repeat(timeout, func() {
80-
err := action(HandlerParams{
80+
err := action(context.TODO(), HandlerParams{
8181
Privmsgf: b.MakePrivmsgf(),
8282
Target: b.Channel,
8383
})
@@ -91,7 +91,7 @@ func (b *Bot) IdleRepeat(timeout time.Duration, action HandlerFunction) {
9191

9292
func (b *Bot) IdleRepeatAfterReset(timeout time.Duration, action HandlerFunction) {
9393
reset := idle.RepeatAfterReset(timeout, func() {
94-
err := action(HandlerParams{
94+
err := action(context.TODO(), HandlerParams{
9595
Privmsgf: b.MakePrivmsgf(),
9696
Target: b.Channel,
9797
})
@@ -183,7 +183,7 @@ on conflict(channel, nick) do update set updated_at = current_timestamp, present
183183
return
184184
}
185185
if url != "" {
186-
bot.Conn.Privmsgf(channel, url)
186+
bot.Conn.Privmsgf(channel, "%s", url)
187187
}
188188
initialized <- true
189189
}()
@@ -275,7 +275,7 @@ func (bot *Bot) RunHandlers(e *irc.Event) {
275275
for _, handler := range bot.Handlers {
276276
matches := handler.regexp.FindStringSubmatch(msg)
277277
if len(matches) > 0 {
278-
err := handler.action(HandlerParams{
278+
err := handler.action(context.TODO(), HandlerParams{
279279
Privmsgf: bot.MakePrivmsgf(),
280280
Msg: msg,
281281
Nick: nick,

handlers.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ func addHandlers(b *bot.Bot) {
139139
// send anonymous note
140140
switch msg.Kind {
141141
case "link":
142-
err = handlers.AnonLink(bot.HandlerParams{
142+
err = handlers.AnonLink(ctx, bot.HandlerParams{
143143
Target: b.Channel,
144144
Privmsgf: b.MakePrivmsgf(),
145145
})
146146
case "quote":
147-
err = handlers.AnonQuote(bot.HandlerParams{
147+
err = handlers.AnonQuote(ctx, bot.HandlerParams{
148148
Target: b.Channel,
149149
Privmsgf: b.MakePrivmsgf(),
150150
})
@@ -176,7 +176,7 @@ func addHandlers(b *bot.Bot) {
176176

177177
events.Subscribe("anonnoteposted", func(note any) {
178178
go func() {
179-
err := handlers.AnonLink(bot.HandlerParams{
179+
err := handlers.AnonLink(context.TODO(), bot.HandlerParams{
180180
Target: b.Channel,
181181
Privmsgf: b.MakePrivmsgf(),
182182
})
@@ -194,7 +194,7 @@ func addHandlers(b *bot.Bot) {
194194

195195
events.Subscribe("anonquoteposted", func(note any) {
196196
go func() {
197-
err := handlers.AnonQuote(bot.HandlerParams{
197+
err := handlers.AnonQuote(context.TODO(), bot.HandlerParams{
198198
Target: b.Channel,
199199
Privmsgf: b.MakePrivmsgf(),
200200
})
@@ -210,7 +210,7 @@ func addHandlers(b *bot.Bot) {
210210
}()
211211
})
212212

213-
b.Handle(`^!help`, func(params bot.HandlerParams) error {
213+
b.Handle(`^!help`, func(ctx context.Context, params bot.HandlerParams) error {
214214
params.Privmsgf(params.Target, "%s: %s", params.Nick, "https://github.com/rcy/annie/blob/main/handlers.go")
215215
return nil
216216
})

handlers/annie/annie.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import (
1313
"github.com/sashabaranov/go-openai"
1414
)
1515

16-
func Handle(params bot.HandlerParams) error {
17-
ctx := context.TODO()
18-
16+
func Handle(ctx context.Context, params bot.HandlerParams) error {
1917
var msg string
2018
if len(params.Matches) < 2 {
2119
return nil

handlers/anonlink.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const (
1818
FutureMessageInterval = "+1 hour"
1919
)
2020

21-
func AnonLink(params bot.HandlerParams) error {
21+
func AnonLink(ctx context.Context, params bot.HandlerParams) error {
2222
q := model.New(db.DB)
2323
pool := linkpool.New(q, minAge)
24-
note, err := pool.PopRandomNote(context.Background(), params.Target, "link")
24+
note, err := pool.PopRandomNote(ctx, params.Target, "link")
2525
if err != nil {
2626
return err
2727
}
@@ -37,16 +37,16 @@ func AnonLink(params bot.HandlerParams) error {
3737

3838
const generateAnonQuoteImages = false
3939

40-
func AnonQuote(params bot.HandlerParams) error {
40+
func AnonQuote(ctx context.Context, params bot.HandlerParams) error {
4141
q := model.New(db.DB)
4242
pool := linkpool.New(q, minAge)
43-
note, err := pool.PopRandomNote(context.Background(), params.Target, "quote")
43+
note, err := pool.PopRandomNote(ctx, params.Target, "quote")
4444
if err != nil {
4545
return err
4646
}
4747

4848
if generateAnonQuoteImages {
49-
img, err := image.GenerateDALLE(context.TODO(), note.Text.String)
49+
img, err := image.GenerateDALLE(ctx, note.Text.String)
5050
if err != nil {
5151
params.Privmsgf(params.Target, "%s", note.Text.String)
5252
return err
@@ -60,8 +60,7 @@ func AnonQuote(params bot.HandlerParams) error {
6060
return nil
6161
}
6262

63-
func AnonStatus(params bot.HandlerParams) error {
64-
ctx := context.TODO()
63+
func AnonStatus(ctx context.Context, params bot.HandlerParams) error {
6564
q := model.New(db.DB)
6665
allPool := linkpool.New(q, 0)
6766
allLinks, err := allPool.Notes(ctx, "link")

handlers/bedtime/bedtime.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package bedtime
22

33
import (
4+
"context"
45
"goirc/bot"
56
"goirc/model"
67
)
78

8-
func Handle(params bot.HandlerParams) error {
9+
func Handle(ctx context.Context, params bot.HandlerParams) error {
910
_, err := model.DB.Exec(`insert into bedtimes(nick, message) values(?, ?)`, params.Nick, params.Msg)
1011
return err
1112
}

handlers/bible/bible.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package bible
22

33
import (
4+
"context"
45
"goirc/bot"
56
"goirc/internal/bible"
67
)
78

8-
func Handle(params bot.HandlerParams) error {
9+
func Handle(ctx context.Context, params bot.HandlerParams) error {
910
ref := params.Matches[1]
1011

1112
b := bible.New()

handlers/catchup.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99
"time"
1010
)
1111

12-
func Catchup(params bot.HandlerParams) error {
13-
ctx := context.TODO()
12+
func Catchup(ctx context.Context, params bot.HandlerParams) error {
1413
q := model.New(db.DB)
1514

1615
notes, err := q.LastDaysNotes(ctx)

handlers/create_note.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"strings"
1010
)
1111

12-
func CreateNote(params bot.HandlerParams) error {
12+
func CreateNote(ctx context.Context, params bot.HandlerParams) error {
1313
q := model.New(db.DB)
1414

1515
text := strings.TrimSpace(params.Matches[1])
1616

17-
_, err := q.InsertNote(context.TODO(), model.InsertNoteParams{
17+
_, err := q.InsertNote(ctx, model.InsertNoteParams{
1818
Target: params.Target,
1919
Nick: sql.NullString{String: params.Nick, Valid: true},
2020
Kind: "note",

handlers/day/day.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var dayCache = NewCache(dayCmd)
1919
var weekCache = NewCache(weekCmd)
2020
var monthCache = NewCache(monthCmd)
2121

22-
func NationalDay(params bot.HandlerParams) error {
22+
func NationalDay(ctx context.Context, params bot.HandlerParams) error {
2323
str, err := dayCache.Pop()
2424
if err != nil {
2525
return err
@@ -28,7 +28,7 @@ func NationalDay(params bot.HandlerParams) error {
2828
str = strings.ReplaceAll(str, "&amp;", "&")
2929

3030
if str == "EOF" {
31-
img, err := dayImage(dayCmd)
31+
img, err := dayImage(ctx, dayCmd)
3232
if err != nil {
3333
return err
3434
}
@@ -40,7 +40,7 @@ func NationalDay(params bot.HandlerParams) error {
4040
return nil
4141
}
4242

43-
func NationalWeek(params bot.HandlerParams) error {
43+
func NationalWeek(ctx context.Context, params bot.HandlerParams) error {
4444
str, err := weekCache.Pop()
4545
if err != nil {
4646
return err
@@ -49,7 +49,7 @@ func NationalWeek(params bot.HandlerParams) error {
4949
str = strings.ReplaceAll(str, "&amp;", "&")
5050

5151
if str == "EOF" {
52-
img, err := dayImage(weekCmd)
52+
img, err := dayImage(ctx, weekCmd)
5353
if err != nil {
5454
return err
5555
}
@@ -61,7 +61,7 @@ func NationalWeek(params bot.HandlerParams) error {
6161
return nil
6262
}
6363

64-
func NationalMonth(params bot.HandlerParams) error {
64+
func NationalMonth(ctx context.Context, params bot.HandlerParams) error {
6565
str, err := monthCache.Pop()
6666
if err != nil {
6767
return err
@@ -70,7 +70,7 @@ func NationalMonth(params bot.HandlerParams) error {
7070
str = strings.ReplaceAll(str, "&amp;", "&")
7171

7272
if str == "EOF" {
73-
img, err := dayImage(monthCmd)
73+
img, err := dayImage(ctx, monthCmd)
7474
if err != nil {
7575
return err
7676
}
@@ -82,7 +82,7 @@ func NationalMonth(params bot.HandlerParams) error {
8282
return nil
8383
}
8484

85-
func NationalRefs(params bot.HandlerParams) error {
85+
func NationalRefs(ctx context.Context, params bot.HandlerParams) error {
8686
params.Privmsgf(params.Target, "%s", url)
8787

8888
return nil
@@ -130,7 +130,7 @@ func stripPhrases(days []string) []string {
130130
return result
131131
}
132132

133-
func dayImage(cmd string) (*image.GeneratedImage, error) {
133+
func dayImage(ctx context.Context, cmd string) (*image.GeneratedImage, error) {
134134
r, err := shell.Command(cmd)
135135
if err != nil {
136136
return nil, err
@@ -141,44 +141,44 @@ func dayImage(cmd string) (*image.GeneratedImage, error) {
141141
days := strings.Split(strings.TrimSpace(r), "\n")
142142
days = stripPhrases(days)
143143
prompt := "create a single scene with representations of " + strings.Join(days, ", ")
144-
gi, err := image.GenerateDALLE(context.Background(), prompt)
144+
gi, err := image.GenerateDALLE(ctx, prompt)
145145
if err != nil {
146146
return nil, fmt.Errorf("prompt: %s: %w", prompt, err)
147147
}
148148

149149
return gi, nil
150150
}
151151

152-
func Dayi(params bot.HandlerParams) error {
153-
img, err := dayImage(dayCmd)
152+
func Dayi(ctx context.Context, params bot.HandlerParams) error {
153+
img, err := dayImage(ctx, dayCmd)
154154
if err != nil {
155155
return err
156156
}
157157
params.Privmsgf(params.Target, "Today's image: %s", img.URL())
158158
return nil
159159
}
160160

161-
func Weeki(params bot.HandlerParams) error {
162-
img, err := dayImage(weekCmd)
161+
func Weeki(ctx context.Context, params bot.HandlerParams) error {
162+
img, err := dayImage(ctx, weekCmd)
163163
if err != nil {
164164
return err
165165
}
166166
params.Privmsgf(params.Target, "This week's image: %s", img.URL())
167167
return nil
168168
}
169169

170-
func Monthi(params bot.HandlerParams) error {
171-
img, err := dayImage(monthCmd)
170+
func Monthi(ctx context.Context, params bot.HandlerParams) error {
171+
img, err := dayImage(ctx, monthCmd)
172172
if err != nil {
173173
return err
174174
}
175175
params.Privmsgf(params.Target, "This month's image: %s", img.URL())
176176
return nil
177177
}
178178

179-
func Image(params bot.HandlerParams) error {
179+
func Image(ctx context.Context, params bot.HandlerParams) error {
180180
prompt := params.Matches[1]
181-
gi, err := image.GenerateDALLE(context.Background(), prompt)
181+
gi, err := image.GenerateDALLE(ctx, prompt)
182182
if err != nil {
183183
return err
184184
}

handlers/deferred_delivery.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package handlers
22

33
import (
4+
"context"
45
"goirc/bot"
56
"goirc/model"
67
)
78

8-
func DeferredDelivery(params bot.HandlerParams) error {
9+
func DeferredDelivery(ctx context.Context, params bot.HandlerParams) error {
910
if params.Target == params.Nick {
1011
params.Privmsgf(params.Target, "not your personal secretary")
1112
return nil

handlers/election/election.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package election
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"goirc/bot"
@@ -211,7 +212,7 @@ func partyResults() ([]string, error) {
211212
return rows, nil
212213
}
213214

214-
func Handle(params bot.HandlerParams) error {
215+
func Handle(ctx context.Context, params bot.HandlerParams) error {
215216
rows, err := partyResults()
216217
if err != nil {
217218
return err

handlers/epigram/epigram.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package epigram
22

33
import (
4+
"context"
45
_ "embed"
56
"goirc/bot"
67
"math/rand"
@@ -13,7 +14,7 @@ var (
1314
epigrams []string = strings.Split(epigramsContent, "\n")
1415
)
1516

16-
func Handle(params bot.HandlerParams) error {
17+
func Handle(ctx context.Context, params bot.HandlerParams) error {
1718
ri := rand.Intn(len(epigrams))
1819

1920
params.Privmsgf(params.Target, epigrams[ri])

handlers/gold/gold.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gold
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"goirc/bot"
@@ -60,7 +61,7 @@ func getGoldPrice(token string) (float64, error) {
6061
return obj.Price, nil
6162
}
6263

63-
func Handle(params bot.HandlerParams) error {
64+
func Handle(ctx context.Context, params bot.HandlerParams) error {
6465
price, err := getGoldPrice(os.Getenv("GOLD_API_TOKEN"))
6566
if err != nil {
6667
return err

0 commit comments

Comments
 (0)