From 7e73f00eb98c87e00dff9188d4c5ead2f77ff25b Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Mon, 20 Jan 2020 20:25:47 -0800 Subject: [PATCH] Fixed bug where ParseContent interface methods won't return a proper error --- _example/advanced_bot/context.go | 7 +++++++ api/channel.go | 2 +- bot/ctx.go | 4 +++- bot/ctx_call.go | 14 ++++++++++++-- bot/error.go | 2 +- discord/message_embed.go | 14 +++++++++++--- discord/snowflake.go | 4 +--- internal/wsutil/conn.go | 3 +++ state/state.go | 9 +-------- 9 files changed, 40 insertions(+), 19 deletions(-) diff --git a/_example/advanced_bot/context.go b/_example/advanced_bot/context.go index f556d6b9..33a16a1e 100644 --- a/_example/advanced_bot/context.go +++ b/_example/advanced_bot/context.go @@ -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 diff --git a/api/channel.go b/api/channel.go index fdeb88fb..3f354f9f 100644 --- a/api/channel.go +++ b/api/channel.go @@ -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 { diff --git a/bot/ctx.go b/bot/ctx.go index f373c334..4fc49dc6 100644 --- a/bot/ctx.go +++ b/bot/ctx.go @@ -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 { diff --git a/bot/ctx_call.go b/bot/ctx_call.go index f4242c3c..38abfe4f 100644 --- a/bot/ctx_call.go +++ b/bot/ctx_call.go @@ -79,7 +79,7 @@ func (ctx *Context) callCmd(ev interface{}) error { return err } - if len(args) < 1 { + if len(args) == 0 { return nil // ??? } @@ -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) @@ -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, } diff --git a/bot/error.go b/bot/error.go index 163cf659..e529c898 100644 --- a/bot/error.go +++ b/bot/error.go @@ -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 { diff --git a/discord/message_embed.go b/discord/message_embed.go index 6ececbc9..bb65790d 100644 --- a/discord/message_embed.go +++ b/discord/message_embed.go @@ -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"` @@ -28,7 +36,7 @@ type Embed struct { func NewEmbed() *Embed { return &Embed{ Type: NormalEmbed, - Color: DefaultColor, + Color: DefaultEmbedColor, } } @@ -55,7 +63,7 @@ func (e *Embed) Validate() error { } if e.Color == 0 { - e.Color = DefaultColor + e.Color = DefaultEmbedColor } if len(e.Title) > 256 { diff --git a/discord/snowflake.go b/discord/snowflake.go index 5857dfb1..0dc673cc 100644 --- a/discord/snowflake.go +++ b/discord/snowflake.go @@ -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" { diff --git a/internal/wsutil/conn.go b/internal/wsutil/conn.go index f57fbf90..6bcbb7e4 100644 --- a/internal/wsutil/conn.go +++ b/internal/wsutil/conn.go @@ -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) } diff --git a/state/state.go b/state/state.go index d4b1b45c..1ad05837 100644 --- a/state/state.go +++ b/state/state.go @@ -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) {