Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconfigure Golangci Linter #1131

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 33 additions & 8 deletions .golangci.yml
@@ -1,18 +1,43 @@
linters:
disable-all: true
disable-all: false
enable:
# - staticcheck
# - staticcheck (used in bugs preset)
disable:
- wsl # Readability
- gomnd
- lll
- wrapcheck # Recommended
- tagliatelle
- gochecknoglobals
- errcheck # Recommended
- gosimple # Recommended
- exhaustivestruct # Recommended
- nlreturn # Recommended
- stylecheck # Recommended
- paralleltest
- predeclared
- goerr113 # Recommended
- testpackage # Recommended
- godox # Recommended: TODO/BUG/FIXME
- goprintffuncname # Recommended
- gocritic # Recommended
- forbidigo

# Enable presets.
# https://golangci-lint.run/usage/linters
presets:
- style
# - unused
- golint
# - bugs

linters-settings:
staticcheck:
go: "1.13"
# staticcheck:
# go: "1.13"

checks: ["all"]
# checks: ["all"]

unused:
go: "1.13"
# unused:
# go: "1.13"

issues:
include:
Expand Down
11 changes: 5 additions & 6 deletions components.go
Expand Up @@ -31,8 +31,8 @@ func (umc *unmarshalableMessageComponent) UnmarshalJSON(src []byte) error {
var v struct {
Type ComponentType `json:"type"`
}
err := json.Unmarshal(src, &v)
if err != nil {

if err := json.Unmarshal(src, &v); err != nil {
return err
}

Expand All @@ -51,11 +51,10 @@ func (umc *unmarshalableMessageComponent) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, umc.MessageComponent)
}

// MessageComponentFromJSON is a helper function for unmarshaling message components
// MessageComponentFromJSON is a helper function for unmarshaling message components.
func MessageComponentFromJSON(b []byte) (MessageComponent, error) {
var u unmarshalableMessageComponent
err := u.UnmarshalJSON(b)
if err != nil {
if err := u.UnmarshalJSON(b); err != nil {
return nil, fmt.Errorf("failed to unmarshal into MessageComponent: %w", err)
}
return u.MessageComponent, nil
Expand Down Expand Up @@ -234,7 +233,7 @@ func (m TextInput) MarshalJSON() ([]byte, error) {
// TextInputStyle is style of text in TextInput component.
type TextInputStyle uint

// Text styles
// Text styles.
const (
TextInputShort TextInputStyle = 1
TextInputParagraph TextInputStyle = 2
Expand Down
1 change: 0 additions & 1 deletion discord.go
Expand Up @@ -28,7 +28,6 @@ const VERSION = "0.24.0"
// Or if it is an OAuth2 token, it must be prefixed with "Bearer "
// e.g. "Bearer ..."
func New(token string) (s *Session, err error) {

// Create an empty Session interface.
s = &Session{
State: NewState(),
Expand Down
7 changes: 2 additions & 5 deletions discord_test.go
Expand Up @@ -10,7 +10,7 @@ import (
)

//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////// VARS NEEDED FOR TESTING
////////////////////////////////////////////////////// VARS NEEDED FOR TESTING.
var (
dg *Session // Stores a global discordgo user session
dgBot *Session // Stores a global discordgo bot session
Expand Down Expand Up @@ -49,7 +49,6 @@ func TestMain(m *testing.M) {

// TestNewToken tests the New() function with a Token.
func TestNewToken(t *testing.T) {

if envOAuth2Token == "" {
t.Skip("Skipping New(token), DGU_TOKEN not set")
}
Expand Down Expand Up @@ -118,7 +117,6 @@ func TestOpenClose(t *testing.T) {
}

func TestAddHandler(t *testing.T) {

testHandlerCalled := int32(0)
testHandler := func(s *Session, m *MessageCreate) {
atomic.AddInt32(&testHandlerCalled, 1)
Expand Down Expand Up @@ -162,7 +160,6 @@ func TestAddHandler(t *testing.T) {
}

func TestRemoveHandler(t *testing.T) {

testHandlerCalled := int32(0)
testHandler := func(s *Session, m *MessageCreate) {
atomic.AddInt32(&testHandlerCalled, 1)
Expand Down Expand Up @@ -249,7 +246,7 @@ func TestScheduledEvents(t *testing.T) {
t.Fatal(err)
}
if len(users) != 0 {
t.Fatal("err on GuildScheduledEventUsers. Mismatch of event maybe occured")
t.Fatal("err on GuildScheduledEventUsers. Mismatch of event maybe occurred")
}

err = dgBot.GuildScheduledEventDelete(envGuild, event.ID)
Expand Down
4 changes: 2 additions & 2 deletions endpoints.go
Expand Up @@ -45,7 +45,7 @@ var (
EndpointVoice = EndpointAPI + "/voice/"
EndpointVoiceRegions = EndpointVoice + "regions"

// TODO: EndpointUserGuildMember
// TODO: EndpointUserGuildMember.

EndpointUser = func(uID string) string { return EndpointUsers + uID }
EndpointUserAvatar = func(uID, aID string) string { return EndpointCDNAvatars + uID + "/" + aID + ".png" }
Expand Down Expand Up @@ -201,7 +201,7 @@ var (
EndpointOAuth2ApplicationsBot = func(aID string) string { return EndpointOAuth2Applications + "/" + aID + "/bot" }
EndpointOAuth2ApplicationAssets = func(aID string) string { return EndpointOAuth2Applications + "/" + aID + "/assets" }

// TODO: Deprecated, remove in the next release
// TODO: Deprecated, remove in the next release.
EndpointOauth2 = EndpointOAuth2
EndpointOauth2Applications = EndpointOAuth2Applications
EndpointOauth2Application = EndpointOAuth2Application
Expand Down
1 change: 0 additions & 1 deletion event.go
Expand Up @@ -241,7 +241,6 @@ func (s *Session) onInterface(i interface{}) {

// onReady handles the ready event.
func (s *Session) onReady(r *Ready) {

// Store the SessionID within the Session struct.
s.sessionID = r.SessionID
}
10 changes: 5 additions & 5 deletions events.go
Expand Up @@ -334,20 +334,20 @@ type VoiceStateUpdate struct {
BeforeUpdate *VoiceState `json:"-"`
}

// MessageDeleteBulk is the data for a MessageDeleteBulk event
// MessageDeleteBulk is the data for a MessageDeleteBulk event.
type MessageDeleteBulk struct {
Messages []string `json:"ids"`
ChannelID string `json:"channel_id"`
GuildID string `json:"guild_id"`
}

// WebhooksUpdate is the data for a WebhooksUpdate event
// WebhooksUpdate is the data for a WebhooksUpdate event.
type WebhooksUpdate struct {
GuildID string `json:"guild_id"`
ChannelID string `json:"channel_id"`
}

// InteractionCreate is the data for a InteractionCreate event
// InteractionCreate is the data for a InteractionCreate event.
type InteractionCreate struct {
*Interaction
}
Expand All @@ -357,14 +357,14 @@ func (i *InteractionCreate) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &i.Interaction)
}

// InviteCreate is the data for a InviteCreate event
// InviteCreate is the data for a InviteCreate event.
type InviteCreate struct {
*Invite
ChannelID string `json:"channel_id"`
GuildID string `json:"guild_id"`
}

// InviteDelete is the data for a InviteDelete event
// InviteDelete is the data for a InviteDelete event.
type InviteDelete struct {
ChannelID string `json:"channel_id"`
GuildID string `json:"guild_id"`
Expand Down
35 changes: 22 additions & 13 deletions interactions.go
Expand Up @@ -18,7 +18,7 @@ const InteractionDeadline = time.Second * 3
// ApplicationCommandType represents the type of application command.
type ApplicationCommandType uint8

// Application command types
// Application command types.
const (
// ChatApplicationCommand is default command type. They are slash commands (i.e. called directly from the chat).
ChatApplicationCommand ApplicationCommandType = 1
Expand Down Expand Up @@ -149,7 +149,7 @@ const (
// InteractionType indicates the type of an interaction event.
type InteractionType uint8

// Interaction types
// Interaction types.
const (
InteractionPing InteractionType = 1
InteractionApplicationCommand InteractionType = 2
Expand Down Expand Up @@ -366,39 +366,39 @@ type ApplicationCommandInteractionDataOption struct {
Focused bool `json:"focused,omitempty"`
}

// IntValue is a utility function for casting option value to integer
// IntValue is a utility function for casting option value to integer.
func (o ApplicationCommandInteractionDataOption) IntValue() int64 {
if o.Type != ApplicationCommandOptionInteger {
panic("IntValue called on data option of type " + o.Type.String())
}
return int64(o.Value.(float64))
}

// UintValue is a utility function for casting option value to unsigned integer
// UintValue is a utility function for casting option value to unsigned integer.
func (o ApplicationCommandInteractionDataOption) UintValue() uint64 {
if o.Type != ApplicationCommandOptionInteger {
panic("UintValue called on data option of type " + o.Type.String())
}
return uint64(o.Value.(float64))
}

// FloatValue is a utility function for casting option value to float
// FloatValue is a utility function for casting option value to float.
func (o ApplicationCommandInteractionDataOption) FloatValue() float64 {
if o.Type != ApplicationCommandOptionNumber {
panic("FloatValue called on data option of type " + o.Type.String())
}
return o.Value.(float64)
}

// StringValue is a utility function for casting option value to string
// StringValue is a utility function for casting option value to string.
func (o ApplicationCommandInteractionDataOption) StringValue() string {
if o.Type != ApplicationCommandOptionString {
panic("StringValue called on data option of type " + o.Type.String())
}
return o.Value.(string)
}

// BoolValue is a utility function for casting option value to bool
// BoolValue is a utility function for casting option value to bool.
func (o ApplicationCommandInteractionDataOption) BoolValue() bool {
if o.Type != ApplicationCommandOptionBoolean {
panic("BoolValue called on data option of type " + o.Type.String())
Expand All @@ -407,12 +407,15 @@ func (o ApplicationCommandInteractionDataOption) BoolValue() bool {
}

// ChannelValue is a utility function for casting option value to channel object.
// s : Session object, if not nil, function additionally fetches all channel's data
// s : Session object, if not nil, function additionally fetches all channel's data.
func (o ApplicationCommandInteractionDataOption) ChannelValue(s *Session) *Channel {
if o.Type != ApplicationCommandOptionChannel {
panic("ChannelValue called on data option of type " + o.Type.String())
}
chanID := o.Value.(string)
chanID, ok := o.Value.(string)
if !ok {
panic("ChannelValue could not type assert string to channel.")
}

if s == nil {
return &Channel{ID: chanID}
Expand All @@ -430,12 +433,15 @@ func (o ApplicationCommandInteractionDataOption) ChannelValue(s *Session) *Chann
}

// RoleValue is a utility function for casting option value to role object.
// s : Session object, if not nil, function additionally fetches all role's data
// s : Session object, if not nil, function additionally fetches all role's data.
func (o ApplicationCommandInteractionDataOption) RoleValue(s *Session, gID string) *Role {
if o.Type != ApplicationCommandOptionRole && o.Type != ApplicationCommandOptionMentionable {
panic("RoleValue called on data option of type " + o.Type.String())
}
roleID := o.Value.(string)
roleID, ok := o.Value.(string)
if !ok {
panic("RoleValue could not type assert string to Role.")
}

if s == nil || gID == "" {
return &Role{ID: roleID}
Expand All @@ -458,12 +464,15 @@ func (o ApplicationCommandInteractionDataOption) RoleValue(s *Session, gID strin
}

// UserValue is a utility function for casting option value to user object.
// s : Session object, if not nil, function additionally fetches all user's data
// s : Session object, if not nil, function additionally fetches all user's data.
func (o ApplicationCommandInteractionDataOption) UserValue(s *Session) *User {
if o.Type != ApplicationCommandOptionUser && o.Type != ApplicationCommandOptionMentionable {
panic("UserValue called on data option of type " + o.Type.String())
}
userID := o.Value.(string)
userID, ok := o.Value.(string)
if !ok {
panic("UserValue could not type assert string to User.")
}

if s == nil {
return &User{ID: userID}
Expand Down
5 changes: 2 additions & 3 deletions interactions_test.go
Expand Up @@ -18,8 +18,9 @@ func TestVerifyInteraction(t *testing.T) {
}
timestamp := "1608597133"

const body = "body"

t.Run("success", func(t *testing.T) {
body := "body"
request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader(body))
request.Header.Set("X-Signature-Timestamp", timestamp)

Expand All @@ -35,7 +36,6 @@ func TestVerifyInteraction(t *testing.T) {
})

t.Run("failure/modified body", func(t *testing.T) {
body := "body"
request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader("WRONG"))
request.Header.Set("X-Signature-Timestamp", timestamp)

Expand All @@ -51,7 +51,6 @@ func TestVerifyInteraction(t *testing.T) {
})

t.Run("failure/modified timestamp", func(t *testing.T) {
body := "body"
request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader("WRONG"))
request.Header.Set("X-Signature-Timestamp", strconv.FormatInt(time.Now().Add(time.Minute).Unix(), 10))

Expand Down
4 changes: 2 additions & 2 deletions locales.go
Expand Up @@ -4,15 +4,15 @@ package discordgo
// https://discord.com/developers/docs/reference#locales
type Locale string

// String returns the human-readable string of the locale
// String returns the human-readable string of the locale.
func (l Locale) String() string {
if name, ok := Locales[l]; ok {
return name
}
return Unknown.String()
}

// All defined locales in Discord
// All defined locales in Discord.
const (
EnglishUS Locale = "en-US"
EnglishGB Locale = "en-GB"
Expand Down