Lint and Cleanup

This commit is contained in:
Bruce Marriner 2016-04-11 18:54:19 -05:00
parent 836e78d3d9
commit 1a672823a2
7 changed files with 20 additions and 54 deletions

View file

@ -43,7 +43,6 @@ func New(args ...interface{}) (s *Session, err error) {
} }
// If no arguments are passed return the empty Session interface. // If no arguments are passed return the empty Session interface.
// Later I will add default values, if appropriate.
if args == nil { if args == nil {
return return
} }
@ -94,7 +93,7 @@ func New(args ...interface{}) (s *Session, err error) {
} }
// case Config: // case Config:
// TODO: Parse configuration // TODO: Parse configuration struct
default: default:
err = fmt.Errorf("Unsupported parameter type provided.") err = fmt.Errorf("Unsupported parameter type provided.")
@ -127,6 +126,7 @@ func New(args ...interface{}) (s *Session, err error) {
// Session.validateHandler(func (s *discordgo.Session, m *discordgo.MessageCreate)) // Session.validateHandler(func (s *discordgo.Session, m *discordgo.MessageCreate))
// will return the reflect.Type of *discordgo.MessageCreate // will return the reflect.Type of *discordgo.MessageCreate
func (s *Session) validateHandler(handler interface{}) reflect.Type { func (s *Session) validateHandler(handler interface{}) reflect.Type {
handlerType := reflect.TypeOf(handler) handlerType := reflect.TypeOf(handler)
if handlerType.NumIn() != 2 { if handlerType.NumIn() != 2 {
@ -161,6 +161,7 @@ func (s *Session) validateHandler(handler interface{}) reflect.Type {
// The return value of this method is a function, that when called will remove the // The return value of this method is a function, that when called will remove the
// event handler. // event handler.
func (s *Session) AddHandler(handler interface{}) func() { func (s *Session) AddHandler(handler interface{}) func() {
s.initialize() s.initialize()
eventType := s.validateHandler(handler) eventType := s.validateHandler(handler)
@ -192,6 +193,7 @@ func (s *Session) AddHandler(handler interface{}) func() {
// handle calls any handlers that match the event type and any handlers of // handle calls any handlers that match the event type and any handlers of
// interface{}. // interface{}.
func (s *Session) handle(event interface{}) { func (s *Session) handle(event interface{}) {
s.handlersMu.RLock() s.handlersMu.RLock()
defer s.handlersMu.RUnlock() defer s.handlersMu.RUnlock()
@ -216,6 +218,7 @@ func (s *Session) handle(event interface{}) {
// initialize adds all internal handlers and state tracking handlers. // initialize adds all internal handlers and state tracking handlers.
func (s *Session) initialize() { func (s *Session) initialize() {
s.handlersMu.Lock() s.handlersMu.Lock()
if s.handlers != nil { if s.handlers != nil {
s.handlersMu.Unlock() s.handlersMu.Unlock()
@ -233,5 +236,6 @@ func (s *Session) initialize() {
// onReady handles the ready event. // onReady handles the ready event.
func (s *Session) onReady(se *Session, r *Ready) { func (s *Session) onReady(se *Session, r *Ready) {
go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval) go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval)
} }

View file

@ -34,9 +34,9 @@ type Attachment struct {
ID string `json:"id"` ID string `json:"id"`
URL string `json:"url"` URL string `json:"url"`
ProxyURL string `json:"proxy_url"` ProxyURL string `json:"proxy_url"`
Filename string `json:"filename"`
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
Filename string `json:"filename"`
Size int `json:"size"` Size int `json:"size"`
} }

View file

@ -9,10 +9,6 @@
package discordgo package discordgo
import (
"fmt"
)
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Code specific to Discord OAuth2 Applications // Code specific to Discord OAuth2 Applications
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -25,10 +21,6 @@ type Application struct {
Icon string `json:"icon,omitempty"` Icon string `json:"icon,omitempty"`
Secret string `json:"secret,omitempty"` Secret string `json:"secret,omitempty"`
RedirectURIs *[]string `json:"redirect_uris,omitempty"` RedirectURIs *[]string `json:"redirect_uris,omitempty"`
// Concept.. almost guarenteed to be removed.
// Imagine that it's just not even here at all.
ses *Session
} }
// Application returns an Application structure of a specific Application // Application returns an Application structure of a specific Application
@ -41,7 +33,6 @@ func (s *Session) Application(appID string) (st *Application, err error) {
} }
err = unmarshal(body, &st) err = unmarshal(body, &st)
st.ses = s
return return
} }
@ -54,11 +45,7 @@ func (s *Session) Applications() (st []*Application, err error) {
} }
err = unmarshal(body, &st) err = unmarshal(body, &st)
for k, _ := range st {
st[k].ses = s
}
return return
// TODO ..
} }
// ApplicationCreate creates a new Application // ApplicationCreate creates a new Application
@ -78,11 +65,10 @@ func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error
} }
err = unmarshal(body, &st) err = unmarshal(body, &st)
st.ses = s
return return
} }
// ApplicationEdit edits an existing Application // ApplicationUpdate updates an existing Application
// var : desc // var : desc
func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) { func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
@ -98,7 +84,6 @@ func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Applicat
} }
err = unmarshal(body, &st) err = unmarshal(body, &st)
st.ses = s
return return
} }
@ -114,29 +99,6 @@ func (s *Session) ApplicationDelete(appID string) (err error) {
return return
} }
//////////////////////////////////////////////////////////////////////////////
// Below two functions are experimental ideas, they will absolutely change
// one way or another and may be deleted entirely.
// Delete is a concept helper function, may be removed.
// this func depends on the Application.ses pointer
// pointing to the Discord session that the application
// came from. This "magic" makes some very very nice helper
// functions possible.
func (a *Application) Delete() (err error) {
if a.ses == nil {
return fmt.Errorf("ses is nil.")
}
return a.ses.ApplicationDelete(a.ID)
}
// Delete is a concept helper function, may be removed.
// this one doesn't depend on the "magic" of adding the ses
// pointer to each Application
func (a *Application) DeleteB(s *Session) (err error) {
return s.ApplicationDelete(a.ID)
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Code specific to Discord OAuth2 Application Bots // Code specific to Discord OAuth2 Application Bots
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View file

@ -50,7 +50,7 @@ func ExampleApplication() {
} }
// Delete the application we created. // Delete the application we created.
err = ap.Delete() err = dg.ApplicationDelete(ap.ID)
log.Printf("Delete: err: %+v\n", err) log.Printf("Delete: err: %+v\n", err)
return return

View file

@ -462,7 +462,7 @@ func (s *Session) GuildEdit(guildID string, g GuildParams) (st *Guild, err error
for _, r := range regions { for _, r := range regions {
valid = append(valid, r.ID) valid = append(valid, r.ID)
} }
err = errors.New(fmt.Sprintf("Region not a valid region (%q)", valid)) err = fmt.Errorf("Region not a valid region (%q)", valid)
return return
} }
} }

View file

@ -118,14 +118,14 @@ type Channel struct {
GuildID string `json:"guild_id"` GuildID string `json:"guild_id"`
Name string `json:"name"` Name string `json:"name"`
Topic string `json:"topic"` Topic string `json:"topic"`
Type string `json:"type"`
LastMessageID string `json:"last_message_id"`
Position int `json:"position"` Position int `json:"position"`
Bitrate int `json:"bitrate"` Bitrate int `json:"bitrate"`
Type string `json:"type"`
PermissionOverwrites []*PermissionOverwrite `json:"permission_overwrites"`
IsPrivate bool `json:"is_private"` IsPrivate bool `json:"is_private"`
LastMessageID string `json:"last_message_id"`
Recipient *User `json:"recipient"` Recipient *User `json:"recipient"`
Messages []*Message `json:"-"` Messages []*Message `json:"-"`
PermissionOverwrites []*PermissionOverwrite `json:"permission_overwrites"`
} }
// A PermissionOverwrite holds permission overwrite data for a Channel // A PermissionOverwrite holds permission overwrite data for a Channel
@ -145,7 +145,7 @@ type Emoji struct {
RequireColons bool `json:"require_colons"` RequireColons bool `json:"require_colons"`
} }
// Custom VerificationLevel typedef for int // VerificationLevel type defination
type VerificationLevel int type VerificationLevel int
// Constants for VerificationLevel levels from 0 to 3 inclusive // Constants for VerificationLevel levels from 0 to 3 inclusive
@ -302,10 +302,10 @@ type TypingStart struct {
// A PresenceUpdate stores data for the presence update websocket event. // A PresenceUpdate stores data for the presence update websocket event.
type PresenceUpdate struct { type PresenceUpdate struct {
User *User `json:"user"`
Status string `json:"status"` Status string `json:"status"`
Roles []string `json:"roles"`
GuildID string `json:"guild_id"` GuildID string `json:"guild_id"`
Roles []string `json:"roles"`
User *User `json:"user"`
Game *Game `json:"game"` Game *Game `json:"game"`
} }

View file

@ -28,7 +28,7 @@ import (
// Code related to both VoiceConnection Websocket and UDP connections. // Code related to both VoiceConnection Websocket and UDP connections.
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// A VoiceConnectionConnection struct holds all the data and functions related to a Discord Voice Connection. // A VoiceConnection struct holds all the data and functions related to a Discord Voice Connection.
type VoiceConnection struct { type VoiceConnection struct {
sync.Mutex sync.Mutex
@ -64,6 +64,8 @@ type VoiceConnection struct {
voiceSpeakingUpdateHandlers []VoiceSpeakingUpdateHandler voiceSpeakingUpdateHandlers []VoiceSpeakingUpdateHandler
} }
// VoiceSpeakingUpdateHandler type provides a function defination for the
// VoiceSpeakingUpdate event
type VoiceSpeakingUpdateHandler func(vc *VoiceConnection, vs *VoiceSpeakingUpdate) type VoiceSpeakingUpdateHandler func(vc *VoiceConnection, vs *VoiceSpeakingUpdate)
// Speaking sends a speaking notification to Discord over the voice websocket. // Speaking sends a speaking notification to Discord over the voice websocket.
@ -156,7 +158,7 @@ func (v *VoiceConnection) Close() {
} }
} }
// Adds a Handler for VoiceSpeakingUpdate events. // AddHandler adds a Handler for VoiceSpeakingUpdate events.
func (v *VoiceConnection) AddHandler(h VoiceSpeakingUpdateHandler) { func (v *VoiceConnection) AddHandler(h VoiceSpeakingUpdateHandler) {
v.Lock() v.Lock()
defer v.Unlock() defer v.Unlock()
@ -208,8 +210,6 @@ func (v *VoiceConnection) waitUntilConnected() error {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
i++ i++
} }
return nil
} }
// Open opens a voice connection. This should be called // Open opens a voice connection. This should be called