diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b9f7ff6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +.vscode/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..855f3c7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 zekro + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b7bcca8 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# ken   [![](https://img.shields.io/badge/docs-pkg.do.dev-blue?logo=go&logoColor=white)](https://pkg.go.dev/github.com/zekroTJA/ken?tab=doc) [![Go Report Card](https://goreportcard.com/badge/github.com/zekroTJA/ken)](https://goreportcard.com/report/github.com/zekroTJA/ken) + +剣 *(`ken` - japanese for `Sword`)* - A prototype, object-oriented slash command handler for discordgo. + +This work-in-progress slash command handler is designed to be used in future versions of [shinpuru](https://github.com/zekroTJA/shinpuru). + +If you are interested how to use this package, see the [basic example](examples/basic). + +--- + +© 2021 Ringo Hoffmann (zekro Development). +Covered by the MIT Licence. diff --git a/command.go b/command.go new file mode 100644 index 0000000..1966a82 --- /dev/null +++ b/command.go @@ -0,0 +1,23 @@ +package ken + +import "github.com/bwmarrin/discordgo" + +type Command interface { + Name() string + Description() string + Version() string + Type() discordgo.ApplicationCommandType + Options() []*discordgo.ApplicationCommandOption + + Run(ctx *Ctx) (err error) +} + +func toApplicationCommand(c Command) *discordgo.ApplicationCommand { + return &discordgo.ApplicationCommand{ + Name: c.Name(), + Type: c.Type(), + Description: c.Description(), + Version: c.Version(), + Options: c.Options(), + } +} diff --git a/context.go b/context.go new file mode 100644 index 0000000..a3a606b --- /dev/null +++ b/context.go @@ -0,0 +1,40 @@ +package ken + +import ( + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/ken/state" +) + +type Ctx struct { + st state.State + + Session *discordgo.Session + Event *discordgo.InteractionCreate + Command Command +} + +func newCtx() *Ctx { + return &Ctx{} +} + +func (c *Ctx) Respond(r *discordgo.InteractionResponse) error { + return c.Session.InteractionRespond(c.Event.Interaction, r) +} + +func (c *Ctx) FollowUp(wait bool, data *discordgo.WebhookParams) (err error, fum *FollowUpMessage) { + self, err := c.st.SelfUser(c.Session) + if err != nil { + return + } + msg, err := c.Session.FollowupMessageCreate(self.ID, c.Event.Interaction, wait, data) + if err != nil { + return + } + fum = &FollowUpMessage{ + Message: msg, + self: self.ID, + s: c.Session, + i: c.Event.Interaction, + } + return +} diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..dde7742 --- /dev/null +++ b/errors.go @@ -0,0 +1,8 @@ +package ken + +import "errors" + +var ( + ErrEmptyCommandName = errors.New("command name can not be empty") + ErrCommandAlreadyRegistered = errors.New("command with the same name has already been rgistered") +) diff --git a/example/basic/commands/test.go b/example/basic/commands/test.go new file mode 100644 index 0000000..3a1a71b --- /dev/null +++ b/example/basic/commands/test.go @@ -0,0 +1,54 @@ +package commands + +import ( + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/ken" +) + +type TestCommand struct{} + +var _ ken.Command = (*TestCommand)(nil) + +func (c *TestCommand) Name() string { + return "test" +} + +func (c *TestCommand) Description() string { + return "Basic Test Command" +} + +func (c *TestCommand) Version() string { + return "1" +} + +func (c *TestCommand) Type() discordgo.ApplicationCommandType { + return discordgo.ChatApplicationCommand +} + +func (c *TestCommand) Options() []*discordgo.ApplicationCommandOption { + return []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionBoolean, + Name: "pog", + Required: true, + Description: "pog", + }, + } +} + +func (c *TestCommand) Run(ctx *ken.Ctx) (err error) { + val := ctx.Event.ApplicationCommandData().Options[0].Value.(bool) + + msg := "not poggers" + if val { + msg = "poggers" + } + + err = ctx.Respond(&discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: msg, + }, + }) + return +} diff --git a/example/basic/main.go b/example/basic/main.go new file mode 100644 index 0000000..bcc8d87 --- /dev/null +++ b/example/basic/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "os" + "os/signal" + "syscall" + + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/ken" + "github.com/zekrotja/ken/example/basic/commands" +) + +func main() { + token := os.Getenv("TOKEN") + + session, err := discordgo.New("Bot " + token) + if err != nil { + panic(err) + } + defer session.Close() + + k := ken.New(session) + k.RegisterCommands(new(commands.TestCommand)) + + err = session.Open() + if err != nil { + panic(err) + } + + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc +} diff --git a/followupmessage.go b/followupmessage.go new file mode 100644 index 0000000..eeddbac --- /dev/null +++ b/followupmessage.go @@ -0,0 +1,27 @@ +package ken + +import "github.com/bwmarrin/discordgo" + +type FollowUpMessage struct { + *discordgo.Message + + self string + s *discordgo.Session + i *discordgo.Interaction +} + +func (m *FollowUpMessage) Edit(data *discordgo.WebhookEdit) (err error) { + inter, err := m.s.FollowupMessageEdit(m.self, m.i, m.ID, data) + if err != nil { + return + } + // This is done to avoid setting m.Message to nil when + // an error is returned above. + m.Message = inter + return +} + +func (m *FollowUpMessage) Delete() (err error) { + err = m.s.FollowupMessageDelete(m.self, m.i, m.ID) + return +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d24f583 --- /dev/null +++ b/go.mod @@ -0,0 +1,21 @@ +module github.com/zekrotja/ken + +go 1.17 + +require github.com/bwmarrin/discordgo v0.23.3-0.20210821175000-0fad116c6c2a + +require ( + github.com/gorilla/websocket v1.4.2 // indirect + github.com/zekrotja/dgrs v0.3.2 + golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect +) + +require ( + github.com/cespare/xxhash/v2 v2.1.1 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/go-redis/redis/v8 v8.10.0 // indirect + go.opentelemetry.io/otel v0.20.0 // indirect + go.opentelemetry.io/otel/metric v0.20.0 // indirect + go.opentelemetry.io/otel/trace v0.20.0 // indirect + golang.org/x/sys v0.0.0-20210112080510-489259a85091 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ccf17c2 --- /dev/null +++ b/go.sum @@ -0,0 +1,108 @@ +github.com/bwmarrin/discordgo v0.23.2 h1:BzrtTktixGHIu9Tt7dEE6diysEF9HWnXeHuoJEt2fH4= +github.com/bwmarrin/discordgo v0.23.2/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M= +github.com/bwmarrin/discordgo v0.23.3-0.20210821175000-0fad116c6c2a h1:L7EuIzka83l5Z7LQqpSBfvmTNvUdr9tGhBa0mDBgSsc= +github.com/bwmarrin/discordgo v0.23.3-0.20210821175000-0fad116c6c2a/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-redis/redis/v8 v8.10.0 h1:OZwrQKuZqdJ4QIM8wn8rnuz868Li91xA3J2DEq+TPGA= +github.com/go-redis/redis/v8 v8.10.0/go.mod h1:vXLTvigok0VtUX0znvbcEW1SOt4OA9CU1ZfnOtKOaiM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/zekrotja/dgrs v0.3.2 h1:tLAcUZgxVvHEbololudACOSgoq2+dEZWVNIR5RYmkws= +github.com/zekrotja/dgrs v0.3.2/go.mod h1:I73ryIMJJXCQxBHyBdRALG+0+mIEXF008EF7A/fYV4I= +go.opentelemetry.io/otel v0.20.0 h1:eaP0Fqu7SXHwvjiqDq83zImeehOHX8doTvU9AwXON8g= +go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo= +go.opentelemetry.io/otel/metric v0.20.0 h1:4kzhXFP+btKm4jwxpjIqjs41A7MakRFUS86bqLHTIw8= +go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU= +go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw= +go.opentelemetry.io/otel/trace v0.20.0 h1:1DL6EXUdcg95gukhuRRvLDO/4X5THh/5dIV52lqtnbw= +go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091 h1:DMyOG0U+gKfu8JZzg2UQe9MeaC1X+xQWlAKcRnjxjCw= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/ken.go b/ken.go new file mode 100644 index 0000000..1d6668b --- /dev/null +++ b/ken.go @@ -0,0 +1,130 @@ +package ken + +import ( + "log" + "sync" + + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/ken/state" +) + +type Ken struct { + s *discordgo.Session + opt *Options + + cmdsLock sync.RWMutex + cmds map[string]Command + ctxPool sync.Pool +} + +type Options struct { + State state.State + OnError func(area string, err error, args ...interface{}) +} + +var defaultOptions = Options{ + State: state.NewInternal(), + OnError: func(ctx string, err error, args ...interface{}) { + log.Printf("[KEN] {%s} - %s\n", ctx, err.Error()) + }, +} + +func New(s *discordgo.Session, options ...Options) (k *Ken) { + k = &Ken{ + s: s, + cmds: make(map[string]Command), + ctxPool: sync.Pool{ + New: func() interface{} { + return newCtx() + }, + }, + } + + k.opt = &defaultOptions + if len(options) > 0 { + o := options[0] + if o.State != nil { + k.opt.State = o.State + } + if o.OnError != nil { + k.opt.OnError = o.OnError + } + } + + k.s.AddHandler(k.onReady) + k.s.AddHandler(k.onInteractionCreate) + + return +} + +func (k *Ken) RegisterCommands(cmds ...Command) (err error) { + k.cmdsLock.Lock() + defer k.cmdsLock.Unlock() + + for _, c := range cmds { + err = k.registerCommand(c) + + if err != nil { + return + } + } + + return +} + +func (k *Ken) RegisterMiddlewares(mws ...Middleware) { + for _, mw := range mws { + k.registerMiddleware(mw) + } +} + +func (k *Ken) registerCommand(cmd Command) (err error) { + if cmd.Name() == "" { + err = ErrEmptyCommandName + return + } + if _, ok := k.cmds[cmd.Name()]; ok { + err = ErrCommandAlreadyRegistered + return + } + + k.cmds[cmd.Name()] = cmd + + return +} + +func (k *Ken) registerMiddleware(mw Middleware) { + +} + +func (k *Ken) onReady(s *discordgo.Session, e *discordgo.Ready) { + k.cmdsLock.RLock() + defer k.cmdsLock.RUnlock() + + for _, cmd := range k.cmds { + if _, err := s.ApplicationCommandCreate(e.User.ID, "", toApplicationCommand(cmd)); err != nil { + k.opt.OnError("command registration", err) + } + } +} + +func (k *Ken) onInteractionCreate(s *discordgo.Session, e *discordgo.InteractionCreate) { + k.cmdsLock.RLock() + cmd := k.cmds[e.ApplicationCommandData().Name] + k.cmdsLock.RUnlock() + + if cmd == nil { + return + } + + ctx := k.ctxPool.Get().(*Ctx) + defer k.ctxPool.Put(ctx) + ctx.st = k.opt.State + ctx.Session = s + ctx.Event = e + ctx.Command = cmd + + if err := cmd.Run(ctx); err != nil { + k.opt.OnError("command error", err, ctx) + } +} diff --git a/middleware.go b/middleware.go new file mode 100644 index 0000000..f1505f1 --- /dev/null +++ b/middleware.go @@ -0,0 +1,4 @@ +package ken + +type Middleware interface { +} diff --git a/state/dgrs.go b/state/dgrs.go new file mode 100644 index 0000000..3858555 --- /dev/null +++ b/state/dgrs.go @@ -0,0 +1,34 @@ +package state + +import ( + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/dgrs" +) + +var _ State = (*Dgrs)(nil) + +// Dgrs is the State implementation for zekrotja/dgrs. +type Dgrs struct { + st *dgrs.State +} + +// NewDgrs returns a new instance of Dgrs using the +// passed dgrs.State. +func NewDgrs(st *dgrs.State) *Dgrs { + return &Dgrs{st} +} + +func (s *Dgrs) SelfUser(_ *discordgo.Session) (u *discordgo.User, err error) { + u, err = s.st.SelfUser() + return +} + +func (s *Dgrs) Channel(_ *discordgo.Session, id string) (c *discordgo.Channel, err error) { + c, err = s.st.Channel(id) + return +} + +func (s *Dgrs) Guild(_ *discordgo.Session, id string) (g *discordgo.Guild, err error) { + g, err = s.st.Guild(id) + return +} diff --git a/state/internal.go b/state/internal.go new file mode 100644 index 0000000..4ec8e03 --- /dev/null +++ b/state/internal.go @@ -0,0 +1,39 @@ +package state + +import "github.com/bwmarrin/discordgo" + +var _ State = (*Internal)(nil) + +// Internal implements the state Interface for +// the internal discordgo.State instance. +type Internal struct{} + +// NewInternal returns a new instance of Internal. +func NewInternal() *Internal { + return &Internal{} +} + +func (*Internal) SelfUser(s *discordgo.Session) (u *discordgo.User, err error) { + u = s.State.User + return +} + +func (*Internal) Channel(s *discordgo.Session, id string) (c *discordgo.Channel, err error) { + if c, err = s.State.Channel(id); err != nil { + return + } + if c == nil { + c, err = s.Channel(id) + } + return +} + +func (*Internal) Guild(s *discordgo.Session, id string) (g *discordgo.Guild, err error) { + if g, err = s.State.Guild(id); err != nil { + return + } + if g == nil { + g, err = s.Guild(id) + } + return +} diff --git a/state/state.go b/state/state.go new file mode 100644 index 0000000..5ae4db6 --- /dev/null +++ b/state/state.go @@ -0,0 +1,21 @@ +package state + +import "github.com/bwmarrin/discordgo" + +// State defines an implementation of the +// state cache. +type State interface { + // SelfUser returns the user objects of the + // authenticated user. + SelfUser(s *discordgo.Session) (*discordgo.User, error) + + // Channel returns a channel object by its ID, whether + // from cache or fetched from the API when not stored + // in the state chache. + Channel(s *discordgo.Session, id string) (*discordgo.Channel, error) + + // Guild returns a guild object by its ID, whether + // from cache or fetched from the API when not stored + // in the state chache. + Guild(s *discordgo.Session, id string) (*discordgo.Guild, error) +}