diff --git a/cache.go b/cache.go index 8648960e..71c91432 100644 --- a/cache.go +++ b/cache.go @@ -29,10 +29,11 @@ type idHolder struct { ChannelID Snowflake `json:"channel_id"` } -type userHolder struct { - User *User `json:"user"` -} - +/* + type userHolder struct { + User *User `json:"user"` + } +*/ func NewBasicCache() *BasicCache { cache := &BasicCache{ CurrentUser: &User{}, @@ -859,7 +860,7 @@ func (c *BasicCache) GetMember(guildID, userID Snowflake) (*Member, error) { defer c.Guilds.Unlock() if container, ok := c.Guilds.Store[guildID]; ok { - if member, _ = container.Members[userID]; member != nil { + if member = container.Members[userID]; member != nil { member = DeepCopy(member).(*Member) } } diff --git a/channel.go b/channel.go index 2a3afd4e..59400e9f 100644 --- a/channel.go +++ b/channel.go @@ -137,22 +137,6 @@ func (c *Channel) String() string { return "channel{name:'" + c.Name + "', id:" + c.ID.String() + "}" } -func (c *Channel) valid() bool { - if c.RateLimitPerUser > 120 { - return false - } - - if len(c.Topic) > 1024 { - return false - } - - if c.Name != "" && (len(c.Name) > 100 || len(c.Name) < 2) { - return false - } - - return true -} - // GetPermissions is used to get a members permissions in a channel. func (c *Channel) GetPermissions(ctx context.Context, s GuildQueryBuilderCaller, member *Member) (permissions PermissionBit, err error) { // Get the guild permissions. diff --git a/client.go b/client.go index a39a6bac..4807aad1 100644 --- a/client.go +++ b/client.go @@ -112,7 +112,7 @@ func createClient(ctx context.Context, conf *Config) (c *Client, err error) { return nil, fmt.Errorf("you can not reject the READY event when LoadMembersQuietly is set to true") } conf.RejectEvents = make([]string, 0, len(uniqueEventNames)) - for eventName, _ := range uniqueEventNames { + for eventName := range uniqueEventNames { conf.RejectEvents = append(conf.RejectEvents, eventName) } @@ -311,8 +311,6 @@ type Client struct { config *Config botToken string - permissions PermissionBit - handlers internalHandlers // reactor demultiplexer for events @@ -321,7 +319,8 @@ type Client struct { // cancelRequestWhenRateLimited by default the Client waits until either the HTTPClient.timeout or // the rate limit ends before closing a request channel. If activated, in stead, requests will // instantly be denied, and the process ended with a rate limited error. - cancelRequestWhenRateLimited bool + // TODO: Implement cancelRequestWhenRateLimited into request logic + // cancelRequestWhenRateLimited bool // req holds the rate limiting logic and error parsing unique for Discord req *httd.Client diff --git a/client_test.go b/client_test.go index 53bd6930..26cf9f5f 100644 --- a/client_test.go +++ b/client_test.go @@ -51,14 +51,6 @@ func (d *dispatcher) nrOfAliveHandlers() (counter int) { return } -func ensure(inputs ...interface{}) { - for i := range inputs { - if err, ok := inputs[i].(error); ok && err != nil { - panic(err) - } - } -} - ////////////////////////////////////////////////////// // // Tests diff --git a/disgord.go b/disgord.go index 1842b871..cea9fcab 100644 --- a/disgord.go +++ b/disgord.go @@ -212,12 +212,13 @@ func (e *ErrorMissingSnowflake) Error() string { return e.info } +/* func newErrorEmptyValue(message string) *ErrorEmptyValue { return &ErrorEmptyValue{ info: message, } } - +*/ // ErrorEmptyValue when a required value was set as empty type ErrorEmptyValue struct { info string @@ -242,37 +243,17 @@ func (e *ErrorUnsupportedType) Error() string { return e.info } -// hasher creates a hash for comparing objects. This excludes the identifier and object type as those are expected -// to be the same during a comparison. -type hasher interface { - hash() string -} - -type guilder interface { - getGuildIDs() []Snowflake -} - // Mentioner can be implemented by any type that is mentionable. // https://discord.com/developers/docs/reference#message-formatting-formats type Mentioner interface { Mention() string } -// zeroInitialiser zero initializes a struct by setting all the values to the default initialization values. -// Used in the flyweight pattern. -type zeroInitialiser interface { - zeroInitialize() -} - // internalUpdater is called whenever a socket event or a REST response is created. type internalUpdater interface { updateInternals() } -type internalClientUpdater interface { - updateInternalsWithClient(*Client) -} - // Discord types // helperTypes: timestamp, levels, etc. @@ -545,6 +526,7 @@ func ShardID(guildID Snowflake, nrOfShards uint) uint { // https://discord.com/developers/docs/resources/user#avatar-data func validAvatarPrefix(avatar string) (valid bool) { + valid = true if avatar == "" { return false } @@ -560,6 +542,7 @@ func validAvatarPrefix(avatar string) (valid bool) { encodings := []string{ "jpeg", "png", "gif", } + for _, encoding := range encodings { prefix := construct(encoding) if strings.HasPrefix(avatar, prefix) { @@ -568,7 +551,7 @@ func validAvatarPrefix(avatar string) (valid bool) { } } - return true + return } // ValidateUsername uses Discords rule-set to verify user-names and nicknames @@ -641,30 +624,9 @@ func ValidateUsername(name string) (err error) { return nil } -func validateChannelName(name string) (err error) { - if name == "" { - return ErrMissingChannelName - } - - // attributes - length := len(name) - - // Names must be of length of minimum 2 and maximum 100 characters long. - if length < 2 { - err = fmt.Errorf("name is too short: %w", ErrIllegalValue) - } else if length > 100 { - err = fmt.Errorf("name is too long: %w", ErrIllegalValue) - } - if err != nil { - return err - } - - return nil -} - // CreateTermSigListener create a channel to listen for termination signals (graceful shutdown) func CreateTermSigListener() <-chan os.Signal { termSignal := make(chan os.Signal, 1) - signal.Notify(termSignal, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + signal.Notify(termSignal, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, syscall.SIGTERM) return termSignal } diff --git a/disgord_test.go b/disgord_test.go index 4e6aa697..c2bcf006 100644 --- a/disgord_test.go +++ b/disgord_test.go @@ -157,9 +157,7 @@ func (g *mockerWSReceiveOnly) Disconnected() bool { var _ gateway.Conn = (*mockerWSReceiveOnly)(nil) -var sink1 int = 1 - -// TODO +// TODO: // BenchmarkDiscordEventToHandler from the time Disgord gets the raw byte event data, to the event handler is triggered //func Benchmark1000DiscordEventToHandler_cacheDisabled(b *testing.B) { // mocker := mockerWSReceiveOnly{ @@ -267,15 +265,6 @@ func TestCtrl(t *testing.T) { } -func check(err error, t *testing.T) { - // Hide function from stacktrace, PR#3 - t.Helper() - - if err != nil { - t.Error(err) - } -} - func TestError_InterfaceImplementations(t *testing.T) { var u interface{} = &ErrorUnsupportedType{} diff --git a/events.go b/events.go index 8191495d..65706d9f 100644 --- a/events.go +++ b/events.go @@ -427,7 +427,7 @@ type GuildDelete struct { // UserWasRemoved ... TODO func (obj *GuildDelete) UserWasRemoved() bool { - return obj.UnavailableGuild.Unavailable == false + return !obj.UnavailableGuild.Unavailable } // UnmarshalJSON ... diff --git a/gateway.go b/gateway.go index 3f4cf651..0db271b7 100644 --- a/gateway.go +++ b/gateway.go @@ -230,7 +230,9 @@ func (g gatewayQueryBuilder) Get() (gateway *gateway.Gateway, err error) { } err = json.Unmarshal(body, &gateway) - + if err != nil { + return nil, errors.New("error unmarshaling json response: " + err.Error()) + } if gateway.URL, err = ensureDiscordGatewayURLHasQueryParams(gateway.URL); err != nil { return gateway, err } diff --git a/guild.go b/guild.go index 8f597a11..dc70dc08 100644 --- a/guild.go +++ b/guild.go @@ -201,9 +201,9 @@ type Guild struct { Emojis []*Emoji `json:"emojis"` Features []string `json:"features"` MFALevel MFALvl `json:"mfa_level"` - WidgetEnabled bool `json:"widget_enabled,omit_empty"` // | - WidgetChannelID Snowflake `json:"widget_channel_id,omit_empty"` // |? - SystemChannelID Snowflake `json:"system_channel_id,omitempty"` // |? + WidgetEnabled bool `json:"widget_enabled,omitempty"` // | + WidgetChannelID Snowflake `json:"widget_channel_id,omitempty"` // |? + SystemChannelID Snowflake `json:"system_channel_id,omitempty"` // |? DiscoverySplash string `json:"discovery_splash,omitempty"` VanityUrl string `json:"vanity_url_code,omitempty"` Description string `json:"description,omitempty"` diff --git a/reactor.go b/reactor.go index 11cde9ea..f95288fb 100644 --- a/reactor.go +++ b/reactor.go @@ -1,7 +1,6 @@ package disgord import ( - "errors" "fmt" "sync" "time" @@ -231,18 +230,6 @@ type handlerSpec struct { ctrl HandlerCtrl } -func (hs *handlerSpec) next() bool { - hs.Lock() - defer hs.Unlock() - - if hs.ctrl.IsDead() { - return false - } - - hs.ctrl.Update() - return true -} - // populate is essentially the constructor for a handlerSpec func (hs *handlerSpec) populate(inputs ...interface{}) (err error) { var i int @@ -279,7 +266,7 @@ func (hs *handlerSpec) populate(inputs ...interface{}) (err error) { if len(inputs) != i { format := "unable to add all handlers/middlewares (%d/%d). Are they in correct order? middlewares, then handlers" - err = errors.New(fmt.Sprintf(format, i, len(inputs))) + err = fmt.Errorf(format, i, len(inputs)) } return err diff --git a/rest.go b/rest.go index 42254e04..22ec62a4 100644 --- a/rest.go +++ b/rest.go @@ -101,12 +101,6 @@ func (r *rest) init() { r.doRequest = r.stepDoRequest } -func (r *rest) bindParams(params interface{}) { - if params == nil { - return - } -} - func (r *rest) stepDoRequest() (resp *http.Response, body []byte, err error) { if r.conf == nil { err = errors.New("missing httd.Request configuration") diff --git a/webhook.go b/webhook.go index ea34a11a..312ea2df 100644 --- a/webhook.go +++ b/webhook.go @@ -347,7 +347,7 @@ func (w webhookWithTokenQueryBuilder) Execute(params *ExecuteWebhook, wait *bool ContentType: contentType, }, w.flags) // Discord only returns the message when wait=true. - if wait != nil && *wait == true { + if wait != nil && *wait { r.pool = w.client.pool.message return getMessage(r.Execute) }