Skip to content

Commit

Permalink
Merge pull request #10 from DisgoOrg/feature/all-major-events
Browse files Browse the repository at this point in the history
implemented event tree & a lot more well
  • Loading branch information
topi314 authored Apr 13, 2021
2 parents 1a9a5b1 + 272f226 commit 4961ab4
Show file tree
Hide file tree
Showing 116 changed files with 3,671 additions and 1,148 deletions.
8 changes: 8 additions & 0 deletions api/audio_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package api

// AudioController lets you Connect / Disconnect from a VoiceChannel
type AudioController interface {
Disgo() Disgo
Connect(guildID Snowflake, channelID Snowflake) error
Disconnect(guildID Snowflake) error
}
59 changes: 33 additions & 26 deletions api/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,72 @@ type Cache interface {
DoCleanup()
CacheFlags() CacheFlags

Guild(Snowflake) *Guild
GuildsByName(string, bool) []*Guild
Guilds() []*Guild
GuildCache() map[Snowflake]*Guild
CacheGuild(*Guild)
UncacheGuild(Snowflake)

/*Message(Snowflake) *Message
Messages(Snowflake) []*Message
AllMessages() []*Message
MessageCache(Snowflake) map[Snowflake]*Message
AllMessageCache() map[Snowflake]map[Snowflake]*Message
CacheMessage(*Message)
UncacheMessage(Snowflake)*/
Command(commandID Snowflake) *Command
GuildCommandCache(guildID Snowflake) map[Snowflake]*Command
AllGuildCommandCache() map[Snowflake]map[Snowflake]*Command
GlobalCommandCache() map[Snowflake]*Command
CacheGlobalCommand(command *Command) *Command
CacheGuildCommand(command *Command) *Command
UncacheCommand(commandID Snowflake)

User(Snowflake) *User
UserByTag(string) *User
UsersByName(string, bool) []*User
Users() []*User
UserCache() map[Snowflake]*User
CacheUser(*User)
CacheUser(*User) *User
UncacheUser(Snowflake)
FindUser(func(*User) bool) *User
FindUsers(func(*User) bool) []*User

Guild(Snowflake) *Guild
GuildsByName(string, bool) []*Guild
Guilds() []*Guild
GuildCache() map[Snowflake]*Guild
CacheGuild(*Guild) *Guild
UncacheGuild(Snowflake)

Message(channelID Snowflake, messageID Snowflake) *Message
Messages(channelID Snowflake) []*Message
MessageCache(channelID Snowflake) map[Snowflake]*Message
AllMessageCache() map[Snowflake]map[Snowflake]*Message
CacheMessage(message *Message) *Message
UncacheMessage(channelID Snowflake, messageID Snowflake)

Member(Snowflake, Snowflake) *Member
MemberByTag(Snowflake, string) *Member
MembersByName(Snowflake, string, bool) []*Member
Members(Snowflake) []*Member
AllMembers() []*Member
MemberCache(Snowflake) map[Snowflake]*Member
AllMemberCache() map[Snowflake]map[Snowflake]*Member
CacheMember(*Member)
CacheMember(member *Member) *Member
UncacheMember(Snowflake, Snowflake)
FindMember(Snowflake, func(*Member) bool) *Member
FindMembers(Snowflake, func(*Member) bool) []*Member

VoiceState(guildID Snowflake, userID Snowflake) *VoiceState
VoiceStates(guildID Snowflake) []*VoiceState
VoiceStateCache(guildID Snowflake) map[Snowflake]*VoiceState
CacheVoiceState(voiceState *VoiceState)
CacheVoiceState(voiceState *VoiceState) *VoiceState
UncacheVoiceState(guildID Snowflake, userID Snowflake)

Role(Snowflake, Snowflake) *Role
Role(roleID Snowflake) *Role
RolesByName(Snowflake, string, bool) []*Role
Roles(Snowflake) []*Role
AllRoles() []*Role
RoleCache(Snowflake) map[Snowflake]*Role
AllRoleCache() map[Snowflake]map[Snowflake]*Role
CacheRole(*Role)
CacheRole(*Role) *Role
UncacheRole(Snowflake, Snowflake)
FindRole(Snowflake, func(*Role) bool) *Role
FindRoles(Snowflake, func(*Role) bool) []*Role

DMChannel(Snowflake) *DMChannel
DMChannels() []*DMChannel
DMChannelCache() map[Snowflake]*DMChannel
CacheDMChannel(*DMChannel)
UncacheDMChannel(Snowflake)
CacheDMChannel(*DMChannel) *DMChannel
UncacheDMChannel(dmChannelID Snowflake)
FindDMChannel(func(*DMChannel) bool) *DMChannel
FindDMChannels(func(*DMChannel) bool) []*DMChannel

Expand All @@ -77,7 +84,7 @@ type Cache interface {
TextChannelsByName(Snowflake, string, bool) []*TextChannel
TextChannels(Snowflake) []*TextChannel
TextChannelCache(Snowflake) map[Snowflake]*TextChannel
CacheTextChannel(*TextChannel)
CacheTextChannel(*TextChannel) *TextChannel
UncacheTextChannel(Snowflake, Snowflake)
FindTextChannel(Snowflake, func(*TextChannel) bool) *TextChannel
FindTextChannels(Snowflake, func(*TextChannel) bool) []*TextChannel
Expand All @@ -86,7 +93,7 @@ type Cache interface {
StoreChannelsByName(Snowflake, string, bool) []*StoreChannel
StoreChannels(Snowflake) []*StoreChannel
StoreChannelCache(Snowflake) map[Snowflake]*StoreChannel
CacheStoreChannel(*StoreChannel)
CacheStoreChannel(*StoreChannel) *StoreChannel
UncacheStoreChannel(Snowflake, Snowflake)
FindStoreChannel(Snowflake, func(*StoreChannel) bool) *StoreChannel
FindStoreChannels(Snowflake, func(*StoreChannel) bool) []*StoreChannel
Expand All @@ -95,7 +102,7 @@ type Cache interface {
VoiceChannelsByName(Snowflake, string, bool) []*VoiceChannel
VoiceChannels(Snowflake) []*VoiceChannel
VoiceChannelCache(Snowflake) map[Snowflake]*VoiceChannel
CacheVoiceChannel(*VoiceChannel)
CacheVoiceChannel(*VoiceChannel) *VoiceChannel
UncacheVoiceChannel(Snowflake, Snowflake)
FindVoiceChannel(Snowflake, func(*VoiceChannel) bool) *VoiceChannel
FindVoiceChannels(Snowflake, func(*VoiceChannel) bool) []*VoiceChannel
Expand All @@ -106,7 +113,7 @@ type Cache interface {
AllCategories() []*Category
CategoryCache(Snowflake) map[Snowflake]*Category
AllCategoryCache() map[Snowflake]map[Snowflake]*Category
CacheCategory(*Category)
CacheCategory(*Category) *Category
UncacheCategory(Snowflake, Snowflake)
FindCategory(Snowflake, func(*Category) bool) *Category
FindCategories(Snowflake, func(*Category) bool) []*Category
Expand All @@ -116,6 +123,6 @@ type Cache interface {
Emotes(guildID Snowflake) []*Emote
EmoteCache(guildID Snowflake) map[Snowflake]*Emote
AllEmoteCache() map[Snowflake]map[Snowflake]*Emote
CacheEmote(*Emote)
CacheEmote(*Emote) *Emote
UncacheEmote(guildID Snowflake, emoteID Snowflake)
}
2 changes: 2 additions & 0 deletions api/cache_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (
CacheFlagRoles
CacheFlagEmotes
CacheFlagVoiceState
CacheFlagCommands
CacheFlagCommandPermissions

CacheFlagsDefault = CacheFlagDMChannels |
CacheFlagCategories |
Expand Down
12 changes: 9 additions & 3 deletions api/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,19 @@ func (c MessageChannel) CrosspostMessage(messageID Snowflake) (*Message, error)
// DMChannel is used for interacting in private Message(s) with users
type DMChannel struct {
MessageChannel
Users []User `json:"recipients"`
}

// GuildChannel is a generic type for all server channels
type GuildChannel struct {
Channel
GuildID Snowflake `json:"guild_id"`
}

// Guild returns the channel's Guild
func (c GuildChannel) Guild() *Guild {
return c.Disgo.Cache().Guild(c.GuildID)
if c.GuildID == nil {
return nil
}
return c.Disgo.Cache().Guild(*c.GuildID)
}

// Category groups text & voice channels in servers together
Expand All @@ -100,6 +101,11 @@ type VoiceChannel struct {
GuildChannel
}

// Connect sends a api.GatewayCommand to connect to this VoiceChannel
func (c *VoiceChannel) Connect() error {
return c.Disgo.AudioController().Connect(*c.GuildID, c.ID)
}

// TextChannel allows you to interact with discord's text channels
type TextChannel struct {
GuildChannel
Expand Down
25 changes: 13 additions & 12 deletions api/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func (c *Command) Create() error {
var rC *Command
var err error
if c.GuildID == nil {
rC, err = c.Disgo.RestClient().CreateGlobalCommand(c.Disgo.SelfUserID(), *c)
rC, err = c.Disgo.RestClient().CreateGlobalCommand(c.Disgo.ApplicationID(), *c)

} else {
rC, err = c.Disgo.RestClient().CreateGuildCommand(c.Disgo.SelfUserID(), *c.GuildID, *c)
rC, err = c.Disgo.RestClient().CreateGuildCommand(c.Disgo.ApplicationID(), *c.GuildID, *c)
}
if err != nil {
return err
Expand All @@ -58,10 +58,10 @@ func (c *Command) Fetch() error {
var rC *Command
var err error
if c.GuildID == nil {
rC, err = c.Disgo.RestClient().GetGlobalCommand(c.Disgo.SelfUserID(), c.ID)
rC, err = c.Disgo.RestClient().GetGlobalCommand(c.Disgo.ApplicationID(), c.ID)

} else {
rC, err = c.Disgo.RestClient().GetGuildCommand(c.Disgo.SelfUserID(), *c.GuildID, c.ID)
rC, err = c.Disgo.RestClient().GetGuildCommand(c.Disgo.ApplicationID(), *c.GuildID, c.ID)
}
if err != nil {
return err
Expand All @@ -78,10 +78,10 @@ func (c *Command) Update(command UpdateCommand) error {
var rC *Command
var err error
if c.GuildID == nil {
rC, err = c.Disgo.RestClient().EditGlobalCommand(c.Disgo.SelfUserID(), c.ID, command)
rC, err = c.Disgo.RestClient().EditGlobalCommand(c.Disgo.ApplicationID(), c.ID, command)

} else {
rC, err = c.Disgo.RestClient().EditGuildCommand(c.Disgo.SelfUserID(), *c.GuildID, c.ID, command)
rC, err = c.Disgo.RestClient().EditGuildCommand(c.Disgo.ApplicationID(), *c.GuildID, c.ID, command)
}
if err != nil {
return err
Expand All @@ -92,11 +92,10 @@ func (c *Command) Update(command UpdateCommand) error {

// SetPermissions sets the GuildCommandPermissions for a specific Guild. this overrides all existing CommandPermission(s). thx discord for that
func (c *Command) SetPermissions(guildID Snowflake, permissions ...CommandPermission) error {
perms, err := c.Disgo.RestClient().SetGuildCommandPermissions(c.Disgo.SelfUserID(), guildID, c.ID, SetGuildCommandPermissions{Permissions: permissions})
_, err := c.Disgo.RestClient().SetGuildCommandPermissions(c.Disgo.ApplicationID(), guildID, c.ID, SetGuildCommandPermissions{Permissions: permissions})
if err != nil {
return err
}
c.GuildPermissions[guildID] = perms
return nil
}

Expand All @@ -107,11 +106,10 @@ func (c Command) GetPermissions(guildID Snowflake) *GuildCommandPermissions {

// FetchPermissions fetched the GuildCommandPermissions for a specific Guild from discord
func (c *Command) FetchPermissions(guildID Snowflake) (*GuildCommandPermissions, error) {
perms, err := c.Disgo.RestClient().GetGuildCommandPermissions(c.Disgo.SelfUserID(), guildID, c.ID)
perms, err := c.Disgo.RestClient().GetGuildCommandPermissions(c.Disgo.ApplicationID(), guildID, c.ID)
if err != nil {
return nil, err
}
c.GuildPermissions[guildID] = perms
return perms, nil
}

Expand All @@ -121,10 +119,10 @@ func (c Command) Delete() error {
return errNoDisgoInstance
}
if c.GuildID == nil {
return c.Disgo.RestClient().DeleteGlobalCommand(c.Disgo.SelfUserID(), c.ID)
return c.Disgo.RestClient().DeleteGlobalCommand(c.Disgo.ApplicationID(), c.ID)

}
return c.Disgo.RestClient().DeleteGuildCommand(c.Disgo.SelfUserID(), *c.GuildID, c.ID)
return c.Disgo.RestClient().DeleteGuildCommand(c.Disgo.ApplicationID(), *c.GuildID, c.ID)
}

// CommandOptionType specifies the type of the arguments used in Command.Options
Expand Down Expand Up @@ -160,12 +158,15 @@ type OptionChoice struct {

// GuildCommandPermissions holds all permissions for a Command
type GuildCommandPermissions struct {
Disgo Disgo
ID Snowflake `json:"id"`
ApplicationID Snowflake `json:"application_id"`
GuildID Snowflake `json:"guild_id"`
Permissions []CommandPermission `json:"permissions"`
}

// TODO: add methods to update those

// CommandPermissionType is the type of the CommandPermission
type CommandPermissionType int

Expand Down
24 changes: 16 additions & 8 deletions api/disgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"time"

"github.com/DisgoOrg/disgo/api/endpoints"
"github.com/DisgoOrg/log"
)

// Disgo is the main discord interface
type Disgo interface {
Logger() log.Logger
Connect() error
Start()
Close()
Expand All @@ -20,13 +22,17 @@ type Disgo interface {
WebhookServer() WebhookServer
Cache() Cache
Intents() Intents
SelfUserID() Snowflake
RawGatewayEventsEnabled() bool
ApplicationID() Snowflake
SelfUser() *User
EntityBuilder() EntityBuilder
EventManager() EventManager
VoiceDispatchInterceptor() VoiceDispatchInterceptor
SetVoiceDispatchInterceptor(voiceInterceptor VoiceDispatchInterceptor)
AudioController() AudioController
HeartbeatLatency() time.Duration
LargeThreshold() int
HasGateway() bool

GetCommand(commandID Snowflake) (*Command, error)
GetCommands() ([]*Command, error)
Expand All @@ -38,38 +44,40 @@ type Disgo interface {

// EventHandler provides info about the EventHandler
type EventHandler interface {
Event() GatewayEventName
Event() GatewayEventType
New() interface{}
}

// GatewayEventHandler is used to handle raw gateway events
type GatewayEventHandler interface {
EventHandler
Handle(Disgo, EventManager, interface{})
HandleGatewayEvent(disgo Disgo, eventManager EventManager, sequenceNumber int, payload interface{})
}

// WebhookEventHandler is used to handle raw webhook events
type WebhookEventHandler interface {
EventHandler
Handle(Disgo, EventManager, chan interface{}, interface{})
HandleWebhookEvent(disgo Disgo, eventManager EventManager, replyChannel chan interface{}, payload interface{})
}

// EventListener is used to create new EventListener to listen to events
type EventListener interface {
OnEvent(interface{})
OnEvent(event interface{})
}

// Event the basic interface each event implement
type Event interface {
Disgo() Disgo
SequenceNumber() int
}

// EventManager lets you listen for specific events triggered by raw gateway events
type EventManager interface {
Disgo() Disgo
Close()
AddEventListeners(...EventListener)
Handle(GatewayEventName, json.RawMessage, chan interface{})
Dispatch(Event)
AddEventListeners(eventListeners ...EventListener)
Handle(eventType GatewayEventType, replyChannel chan interface{}, sequenceNumber int, payload json.RawMessage)
Dispatch(event Event)
}

// GetOS returns the simplified version of the operating system for sending to Discord in the IdentifyCommandDataProperties.OS payload
Expand Down
8 changes: 5 additions & 3 deletions api/disgo_builder.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package api

import (
log "github.com/sirupsen/logrus"
"github.com/DisgoOrg/log"

"github.com/DisgoOrg/disgo/api/endpoints"
)

// DisgoBuilder allows you to create a Disgo client through a series of methods
type DisgoBuilder interface {
SetLogLevel(level log.Level) DisgoBuilder
SetLogger(level log.Logger) DisgoBuilder
SetToken(token endpoints.Token) DisgoBuilder
SetIntents(intents Intents) DisgoBuilder
SetVoiceDispatchInterceptor(VoiceDispatchInterceptor) DisgoBuilder
SetRawGatewayEventsEnabled(enabled bool) DisgoBuilder
SetVoiceDispatchInterceptor(voiceDispatchInterceptor VoiceDispatchInterceptor) DisgoBuilder
SetEntityBuilder(entityBuilder EntityBuilder) DisgoBuilder
SetEventManager(eventManager EventManager) DisgoBuilder
AddEventListeners(eventsListeners ...EventListener) DisgoBuilder
SetWebhookServer(webhookServer WebhookServer) DisgoBuilder
Expand Down
Loading

0 comments on commit 4961ab4

Please sign in to comment.