Skip to content

Commit

Permalink
migrate to snowflake library (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelwing authored Apr 4, 2023
1 parent af623d3 commit 0049a6d
Show file tree
Hide file tree
Showing 31 changed files with 169 additions and 145 deletions.
2 changes: 1 addition & 1 deletion cmd/events/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func generate(sourceTypes []string) error {
jen.Func().
Params(
jen.Qual("context", "Context"),
jen.Op("*").Qual("wumpgo.dev/wumpgo/rest", "Client"),
jen.Qual("wumpgo.dev/wumpgo/rest", "RESTClient"),
jen.Op("*").Qual("wumpgo.dev/wumpgo/objects", sourceType),
),
).Block(jen.Return(jen.Id("newHandler").Call(jen.Id("v")), jen.Lit(pascalToSnakeCase(sourceType)), jen.Nil()))
Expand Down
2 changes: 2 additions & 0 deletions docs/src/interactions/command_example.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build exclude

package example

import (
Expand Down
2 changes: 1 addition & 1 deletion docs/src/interactions/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The second tag used is the `choices` tag. This tag is used to specify the possi
Let's start with a quick example, then we'll go over what each part does.

```go
{{#include command_example.go}}
{{#include command_example.go:3:}}
```

We start off with a go:generate comment. This is required on any file that contains one or more slash commands that needs codegen. wumpgoctl will only perform generation on files that directly call out to gen.
Expand Down
2 changes: 2 additions & 0 deletions docs/src/interactions/component_example.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build exclude

package example

import (
Expand Down
10 changes: 9 additions & 1 deletion docs/src/interactions/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ ctx.Content("Click a button!").View(v)

By default, views will render the components in order, filling out the component rows before adding another, up to 5. Beyond 25 components, the view will be truncated. The exception to this is when using a View in a modal, then it will only have 1 component per row, up to 5. The only valid component type for modals is a Text Input component.

[^unsecure]: Be aware that custom IDs are inherently not secure. You cannot trust the contents of a custom ID. Be sure you validate the data you get as part of a custom ID. For example, if building a reaction role bot, validate the role is actually a valid choice before assigning it.
[^unsecure]: Be aware that custom IDs are inherently not secure. You cannot trust the contents of a custom ID. Be sure you validate the data you get as part of a custom ID. For example, if building a reaction role bot, validate the role is actually a valid choice before assigning it.

## Example

Below is a full example of using a command to send a message containing a component, and a handler to handle events from that component.

```go
{{#include component_example.go:3:}}
```
6 changes: 3 additions & 3 deletions examples/gateway_local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func main() {
}
}

func ready(ctx context.Context, c *rest.Client, r *objects.Ready) {
func ready(ctx context.Context, c rest.RESTClient, r *objects.Ready) {
fmt.Println("Ready as", r.User.Username)
}

func guildCreate(ctx context.Context, c *rest.Client, g *objects.GuildCreate) {
func guildCreate(ctx context.Context, c rest.RESTClient, g *objects.GuildCreate) {
fmt.Println("Added to guild", g.Name)
}

func typing(ctx context.Context, c *rest.Client, t *objects.TypingStart) {
func typing(ctx context.Context, c rest.RESTClient, t *objects.TypingStart) {
fmt.Println(t.Member.User.Username, "started typing")
}
4 changes: 2 additions & 2 deletions examples/gateway_redis/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func main() {
}
}

func ready(ctx context.Context, c *rest.Client, r *objects.Ready) {
func ready(ctx context.Context, c rest.RESTClient, r *objects.Ready) {
fmt.Println("Ready as", r.User.Username)
}

func guildCreate(ctx context.Context, c *rest.Client, g *objects.GuildCreate) {
func guildCreate(ctx context.Context, c rest.RESTClient, g *objects.GuildCreate) {
fmt.Println("Added to guild", g.Name)
}
6 changes: 3 additions & 3 deletions gateway/receiver/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (

type EventHandlerIface interface {
New() interface{}
Handle(context.Context, *rest.Client, interface{})
Handle(context.Context, rest.RESTClient, interface{})
}

type EventHandler[T any] func(context.Context, *rest.Client, *T)
type EventHandler[T any] func(context.Context, rest.RESTClient, *T)

func (eh EventHandler[T]) New() interface{} {
var obj T
return &obj
}

func (eh EventHandler[T]) Handle(ctx context.Context, c *rest.Client, i interface{}) {
func (eh EventHandler[T]) Handle(ctx context.Context, c rest.RESTClient, i interface{}) {
if t, ok := i.(*T); ok {
eh(ctx, c, t)
}
Expand Down
122 changes: 61 additions & 61 deletions gateway/receiver/events_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gateway/receiver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type ReceiverOption func(*eventRouter)

func WithClient(c *rest.Client) ReceiverOption {
func WithClient(c rest.RESTClient) ReceiverOption {
return func(e *eventRouter) {
e.client = c
}
Expand Down
2 changes: 1 addition & 1 deletion gateway/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Receiver interface {
type eventRouter struct {
handlers map[string][]EventHandlerIface
log zerolog.Logger
client *rest.Client
client rest.RESTClient
errHandler func(error)
groupName string
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0
golang.org/x/time v0.3.0
nhooyr.io/websocket v1.8.7
wumpgo.dev/snowflake v1.0.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,5 @@ nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
wumpgo.dev/snowflake v1.0.0 h1:gV3p9RwfDN8L0njhUiamzW656YzXPNiFA/D5v5Od8xg=
wumpgo.dev/snowflake v1.0.0/go.mod h1:WrjG3nP1qe4GoLhusCcXMCSpz4h705RiehMqyjxkYrM=
2 changes: 1 addition & 1 deletion interactions/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func WithLogger(l zerolog.Logger) InteractionOption {
}
}

func WithClient(c *rest.Client) InteractionOption {
func WithClient(c rest.RESTClient) InteractionOption {
return func(a *App) {
a.restClient = c
}
Expand Down
4 changes: 2 additions & 2 deletions interactions/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type (
// App is the primary interactions server
type App struct {
logger zerolog.Logger
restClient *rest.Client
restClient rest.RESTClient
commandHandler HandlerFunc
componentHandler HandlerFunc
autocompleteHandler HandlerFunc
Expand Down Expand Up @@ -229,6 +229,6 @@ func (a *App) ProcessRequest(ctx context.Context, data []byte) (resp *objects.In
}

// Rest exposes the internal Rest client so you can make calls to the Discord API
func (a *App) Rest() *rest.Client {
func (a *App) Rest() rest.RESTClient {
return a.restClient
}
33 changes: 4 additions & 29 deletions objects/snowflake.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,20 @@
package objects

import (
"encoding/json"
"strconv"
"time"

"wumpgo.dev/snowflake"
)

type Snowflake snowflake.Snowflake

var _ SnowflakeObject = (*Snowflake)(nil)

const (
DiscordEpoch = 1420070400000
)

type Snowflake uint64

func (s *Snowflake) UnmarshalJSON(bytes []byte) error {
var snowflake string
err := json.Unmarshal(bytes, &snowflake)
if err != nil {
return err
}

if snowflake == "" || snowflake == "null" {
*s = 0
return nil
}

snowInt, err := strconv.ParseInt(snowflake, 10, 64)
if err != nil {
return err
}

*s = Snowflake(snowInt)

return nil
}

func (s Snowflake) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}

// CreatedAt returns a time.Time representing the time a Snowflake was created
func (s Snowflake) CreatedAt() Time {
timestampMs := (int64(s) >> 22) + DiscordEpoch
Expand Down
Loading

0 comments on commit 0049a6d

Please sign in to comment.