Skip to content

Commit

Permalink
Fixed bug where ParseContent interface methods won't return a proper …
Browse files Browse the repository at this point in the history
…error
  • Loading branch information
diamondburned committed Jan 21, 2020
1 parent c7d33f5 commit 7e73f00
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 19 deletions.
7 changes: 7 additions & 0 deletions _example/advanced_bot/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func (bot *Bot) Help(m *gateway.MessageCreateEvent) error {
return err
}

func (bot *Bot) Add(m *gateway.MessageCreateEvent, a, b int) error {
content := fmt.Sprintf("%d + %d = %d", a, b, a+b)

_, err := bot.Ctx.SendMessage(m.ChannelID, content, nil)
return err
}

func (bot *Bot) Ping(m *gateway.MessageCreateEvent) error {
_, err := bot.Ctx.SendMessage(m.ChannelID, "Pong!", nil)
return err
Expand Down
2 changes: 1 addition & 1 deletion api/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *Client) Channel(
var channel *discord.Channel

return channel,
c.RequestJSON(&channel, "POST", EndpointChannels+channelID.String())
c.RequestJSON(&channel, "GET", EndpointChannels+channelID.String())
}

type ModifyChannelData struct {
Expand Down
4 changes: 3 additions & 1 deletion bot/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ func (ctx *Context) Start() func() {
if err := ctx.callCmd(v); err != nil {
if str := ctx.FormatError(err); str != "" {
// Log the main error first
ctx.ErrorLogger(errors.Wrap(err, str))
if !ctx.ReplyError {
ctx.ErrorLogger(errors.Wrap(err, "Command error"))
}

mc, ok := v.(*gateway.MessageCreateEvent)
if !ok {
Expand Down
14 changes: 12 additions & 2 deletions bot/ctx_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (ctx *Context) callCmd(ev interface{}) error {
return err
}

if len(args) < 1 {
if len(args) == 0 {
return nil // ???
}

Expand Down Expand Up @@ -135,6 +135,16 @@ func (ctx *Context) callCmd(ev interface{}) error {

// Check manual parser
if cmd.parseType != nil {
if len(args[start:]) == 0 {
return &ErrInvalidUsage{
Args: args,
Prefix: ctx.Prefix,
Index: len(args) - start,
Err: "Not enough arguments given",
ctx: cmd,
}
}

// Create a zero value instance of this
v := reflect.New(cmd.parseType)

Expand Down Expand Up @@ -165,7 +175,7 @@ func (ctx *Context) callCmd(ev interface{}) error {
return &ErrInvalidUsage{
Args: args,
Prefix: ctx.Prefix,
Index: len(cmd.arguments) - start,
Index: len(args) - 1,
Err: "Not enough arguments given",
ctx: cmd,
}
Expand Down
2 changes: 1 addition & 1 deletion bot/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ErrInvalidUsage struct {

func (err *ErrInvalidUsage) Error() string {
if err.Index == 0 {
return "Invalid usage"
return "Invalid usage, error: " + err.Err
}

if len(err.Args) == 0 {
Expand Down
14 changes: 11 additions & 3 deletions discord/message_embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import "fmt"

type Color uint32

const DefaultColor Color = 0x303030
var DefaultEmbedColor Color = 0x303030

func (c Color) Uint32() uint32 {
return uint32(c)
}

func (c Color) Int() int {
return int(c)
}

type Embed struct {
Title string `json:"title,omitempty"`
Expand All @@ -28,7 +36,7 @@ type Embed struct {
func NewEmbed() *Embed {
return &Embed{
Type: NormalEmbed,
Color: DefaultColor,
Color: DefaultEmbedColor,
}
}

Expand All @@ -55,7 +63,7 @@ func (e *Embed) Validate() error {
}

if e.Color == 0 {
e.Color = DefaultColor
e.Color = DefaultEmbedColor
}

if len(e.Title) > 256 {
Expand Down
4 changes: 1 addition & 3 deletions discord/snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (

const DiscordEpoch = 1420070400000 * int64(time.Millisecond)

type Snowflake int64
type Snowflake uint64

func NewSnowflake(t time.Time) Snowflake {
return Snowflake(TimeToDiscordEpoch(t) << 22)
}

const Me = Snowflake(-1)

func (s *Snowflake) UnmarshalJSON(v []byte) error {
id := strings.Trim(string(v), `"`)
if id == "null" {
Expand Down
3 changes: 3 additions & 0 deletions internal/wsutil/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (c *Conn) readAll(ctx context.Context) ([]byte, error) {
}

func (c *Conn) Send(ctx context.Context, b []byte) error {
c.mut.Lock()
defer c.mut.Unlock()

// TODO: zlib stream
return c.Conn.Write(ctx, websocket.MessageText, b)
}
Expand Down
9 changes: 1 addition & 8 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,7 @@ func (s *State) Member(
return nil, err
}

if err := s.Store.MemberSet(guildID, m); err != nil {
return m, err
}

return m, s.Gateway.RequestGuildMembers(gateway.RequestGuildMembersData{
GuildID: []discord.Snowflake{guildID},
Presences: true,
})
return m, s.Store.MemberSet(guildID, m)
}

func (s *State) Members(guildID discord.Snowflake) ([]discord.Member, error) {
Expand Down

0 comments on commit 7e73f00

Please sign in to comment.