Skip to content

Commit eecf42e

Browse files
committed
discordwebhook: log delivery events to module_events, not a missing table
logEvent inserted into a discord_events table that no migration ever created, so every WebhookSent/WebhookError audit write failed with "no such table: discord_events". Use the shared engine.EventLogger (module_events) like the other modules instead.
1 parent ea0910f commit eecf42e

4 files changed

Lines changed: 13 additions & 19 deletions

File tree

modules/discordwebhook/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Any module (or SQL trigger) can deliver a message simply by inserting a row into
1616

1717
## Behavioral details
1818

19-
- Backed by a SQLite table `discord_webhook_queue` created via migration on `New`; the `channel_id` column is added best-effort via `ALTER TABLE` on `New`. Also writes audit rows to a shared `discord_events` table (must exist; not created here).
19+
- Backed by a SQLite table `discord_webhook_queue` created via migration on `New`; the `channel_id` column is added best-effort via `ALTER TABLE` on `New`. Audit rows (`WebhookSent`/`WebhookError`) are written via the shared `engine.EventLogger` to the `module_events` table under module `discordwebhook`.
2020
- `AttachWorkers` registers two background workers:
2121
- Hourly cleanup deleting any queue rows older than 1 hour (`created` age > 3600s), regardless of delivery status.
2222
- 1Hz workqueue poller, rate-limited to 5 sends/sec globally (`maxRPS = 5`).

modules/discordwebhook/module.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,28 @@ type MessageQueuer interface {
5151
type Module struct {
5252
db *sql.DB
5353
sender Sender
54+
events *engine.EventLogger
5455
}
5556

56-
func New(d *sql.DB, sender Sender) *Module {
57+
func New(d *sql.DB, events *engine.EventLogger, sender Sender) *Module {
5758
engine.MustMigrate(d, migration)
5859
// channel_id generalizes the queue to bot-API channel delivery. ALTER
5960
// TABLE ADD COLUMN can't use IF NOT EXISTS, so run it best-effort and
6061
// ignore the "duplicate column" error on subsequent boots.
6162
d.Exec("ALTER TABLE discord_webhook_queue ADD COLUMN channel_id TEXT NOT NULL DEFAULT ''")
62-
m := &Module{db: d, sender: sender}
63+
m := &Module{db: d, sender: sender, events: events}
6364
if m.sender == nil {
6465
m.sender = newNoopSender()
6566
}
6667
return m
6768
}
6869

69-
// logEvent logs a Discord webhook event to the shared discord_events table.
70+
// logEvent records a Discord delivery event to the shared module_events table.
7071
func (m *Module) logEvent(ctx context.Context, eventType string, success bool, details string) {
71-
successInt := 0
72-
if success {
73-
successInt = 1
74-
}
75-
_, err := m.db.ExecContext(ctx,
76-
`INSERT INTO discord_events (event_type, success, details)
77-
VALUES (?, ?, ?)`,
78-
eventType, successInt, details)
79-
if err != nil {
80-
slog.Error("failed to log discord webhook event", "error", err, "eventType", eventType)
72+
if m.events == nil {
73+
return
8174
}
75+
m.events.LogEvent(ctx, 0, eventType, "", "", success, details)
8276
}
8377

8478
func (m *Module) AttachWorkers(mgr *engine.ProcMgr) {

modules/discordwebhook/module_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestQueueMessage(t *testing.T) {
1212
testDB := engine.OpenTestDB(t)
1313

14-
m := New(testDB, nil)
14+
m := New(testDB, nil, nil)
1515
ctx := t.Context()
1616

1717
err := m.QueueMessage(ctx, "https://discord.com/api/webhooks/test", `{"content":"test message"}`)
@@ -32,7 +32,7 @@ func TestQueueMessage(t *testing.T) {
3232
func TestGetItem(t *testing.T) {
3333
testDB := engine.OpenTestDB(t)
3434

35-
m := New(testDB, nil)
35+
m := New(testDB, nil, nil)
3636
ctx := t.Context()
3737

3838
err := m.QueueMessage(ctx, "https://discord.com/api/webhooks/test", `{"content":"hello"}`)
@@ -48,7 +48,7 @@ func TestGetItem(t *testing.T) {
4848
func TestUpdateItemSuccess(t *testing.T) {
4949
testDB := engine.OpenTestDB(t)
5050

51-
m := New(testDB, nil)
51+
m := New(testDB, nil, nil)
5252
ctx := t.Context()
5353

5454
err := m.QueueMessage(ctx, "https://discord.com/api/webhooks/test", `{"content":"test"}`)
@@ -71,7 +71,7 @@ func TestUpdateItemSuccess(t *testing.T) {
7171
func TestUpdateItemFailure(t *testing.T) {
7272
testDB := engine.OpenTestDB(t)
7373

74-
m := New(testDB, nil)
74+
m := New(testDB, nil, nil)
7575
ctx := t.Context()
7676

7777
err := m.QueueMessage(ctx, "https://discord.com/api/webhooks/test", `{"content":"test"}`)

modules/register.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func Register(a *engine.App, opts Options) *auth.Module {
112112
}
113113
return cfg.BotToken, nil
114114
})
115-
discordWebhookMod := discordwebhook.New(opts.Database, webhookSender)
115+
discordWebhookMod := discordwebhook.New(opts.Database, engine.NewEventLogger(opts.Database, "discordwebhook"), webhookSender)
116116
a.Add(discordWebhookMod)
117117

118118
discordMod := discord.New(opts.Database, opts.Self, opts.DiscordIssuer, engine.NewEventLogger(opts.Database, "discord"))

0 commit comments

Comments
 (0)