1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-11-22 12:33:38 +00:00

Fixed bug where ParseContent interface methods won't return a proper error

This commit is contained in:
diamondburned (Forefront) 2020-01-20 20:25:47 -08:00
parent c7d33f5dcc
commit 7e73f00eb9
9 changed files with 40 additions and 19 deletions

View file

@ -22,6 +22,13 @@ func (bot *Bot) Help(m *gateway.MessageCreateEvent) error {
return err 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 { func (bot *Bot) Ping(m *gateway.MessageCreateEvent) error {
_, err := bot.Ctx.SendMessage(m.ChannelID, "Pong!", nil) _, err := bot.Ctx.SendMessage(m.ChannelID, "Pong!", nil)
return err return err

View file

@ -70,7 +70,7 @@ func (c *Client) Channel(
var channel *discord.Channel var channel *discord.Channel
return channel, return channel,
c.RequestJSON(&channel, "POST", EndpointChannels+channelID.String()) c.RequestJSON(&channel, "GET", EndpointChannels+channelID.String())
} }
type ModifyChannelData struct { type ModifyChannelData struct {

View file

@ -176,7 +176,9 @@ func (ctx *Context) Start() func() {
if err := ctx.callCmd(v); err != nil { if err := ctx.callCmd(v); err != nil {
if str := ctx.FormatError(err); str != "" { if str := ctx.FormatError(err); str != "" {
// Log the main error first // 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) mc, ok := v.(*gateway.MessageCreateEvent)
if !ok { if !ok {

View file

@ -79,7 +79,7 @@ func (ctx *Context) callCmd(ev interface{}) error {
return err return err
} }
if len(args) < 1 { if len(args) == 0 {
return nil // ??? return nil // ???
} }
@ -135,6 +135,16 @@ func (ctx *Context) callCmd(ev interface{}) error {
// Check manual parser // Check manual parser
if cmd.parseType != nil { 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 // Create a zero value instance of this
v := reflect.New(cmd.parseType) v := reflect.New(cmd.parseType)
@ -165,7 +175,7 @@ func (ctx *Context) callCmd(ev interface{}) error {
return &ErrInvalidUsage{ return &ErrInvalidUsage{
Args: args, Args: args,
Prefix: ctx.Prefix, Prefix: ctx.Prefix,
Index: len(cmd.arguments) - start, Index: len(args) - 1,
Err: "Not enough arguments given", Err: "Not enough arguments given",
ctx: cmd, ctx: cmd,
} }

View file

@ -40,7 +40,7 @@ type ErrInvalidUsage struct {
func (err *ErrInvalidUsage) Error() string { func (err *ErrInvalidUsage) Error() string {
if err.Index == 0 { if err.Index == 0 {
return "Invalid usage" return "Invalid usage, error: " + err.Err
} }
if len(err.Args) == 0 { if len(err.Args) == 0 {

View file

@ -4,7 +4,15 @@ import "fmt"
type Color uint32 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 { type Embed struct {
Title string `json:"title,omitempty"` Title string `json:"title,omitempty"`
@ -28,7 +36,7 @@ type Embed struct {
func NewEmbed() *Embed { func NewEmbed() *Embed {
return &Embed{ return &Embed{
Type: NormalEmbed, Type: NormalEmbed,
Color: DefaultColor, Color: DefaultEmbedColor,
} }
} }
@ -55,7 +63,7 @@ func (e *Embed) Validate() error {
} }
if e.Color == 0 { if e.Color == 0 {
e.Color = DefaultColor e.Color = DefaultEmbedColor
} }
if len(e.Title) > 256 { if len(e.Title) > 256 {

View file

@ -8,14 +8,12 @@ import (
const DiscordEpoch = 1420070400000 * int64(time.Millisecond) const DiscordEpoch = 1420070400000 * int64(time.Millisecond)
type Snowflake int64 type Snowflake uint64
func NewSnowflake(t time.Time) Snowflake { func NewSnowflake(t time.Time) Snowflake {
return Snowflake(TimeToDiscordEpoch(t) << 22) return Snowflake(TimeToDiscordEpoch(t) << 22)
} }
const Me = Snowflake(-1)
func (s *Snowflake) UnmarshalJSON(v []byte) error { func (s *Snowflake) UnmarshalJSON(v []byte) error {
id := strings.Trim(string(v), `"`) id := strings.Trim(string(v), `"`)
if id == "null" { if id == "null" {

View file

@ -137,6 +137,9 @@ func (c *Conn) readAll(ctx context.Context) ([]byte, error) {
} }
func (c *Conn) Send(ctx context.Context, b []byte) error { func (c *Conn) Send(ctx context.Context, b []byte) error {
c.mut.Lock()
defer c.mut.Unlock()
// TODO: zlib stream // TODO: zlib stream
return c.Conn.Write(ctx, websocket.MessageText, b) return c.Conn.Write(ctx, websocket.MessageText, b)
} }

View file

@ -297,14 +297,7 @@ func (s *State) Member(
return nil, err return nil, err
} }
if err := s.Store.MemberSet(guildID, m); err != nil { return m, s.Store.MemberSet(guildID, m)
return m, err
}
return m, s.Gateway.RequestGuildMembers(gateway.RequestGuildMembersData{
GuildID: []discord.Snowflake{guildID},
Presences: true,
})
} }
func (s *State) Members(guildID discord.Snowflake) ([]discord.Member, error) { func (s *State) Members(guildID discord.Snowflake) ([]discord.Member, error) {