/
module.go
66 lines (56 loc) · 1.72 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ublockfx
import (
"context"
"time"
"github.com/bwmarrin/discordgo"
"go.uber.org/fx"
"github.com/ajvpot/vanderbot/pkg/discordfx"
)
type NewCommandParams struct {
fx.In
}
type NewCommandResult struct {
fx.Out
Command *discordfx.ApplicationCommandWithHandler `group:"command"`
}
func ptr[T any](t T) *T {
return &t
}
// NewCommand initializes a uBlock command.
func NewCommand(p NewCommandParams) NewCommandResult {
return NewCommandResult{Command: &discordfx.ApplicationCommandWithHandler{
Command: discordgo.ApplicationCommand{
Name: "ublock",
Description: "Generate a report of resources blocked by uBlock Origin.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "url",
Description: "URL to check for blocked resources.",
Required: true,
},
},
},
Handler: func(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
// Access options in the order provided by the user.
options := i.ApplicationCommandData().Options
// Or convert the slice into a map
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Doing the thing",
},
})
time.AfterFunc(time.Second, func() {
s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(optionMap["URL"].StringValue()),
})
})
},
}}
}
var Module = fx.Options(fx.Provide(NewCommand))