Documentation ¶
Overview ¶
discord.go is a wrapper for bwmarrin/discordgo to enable a few "oh that's neat" functions.
d.g is heavily context-based and focues on bot development.
The goal is that all functionality a bot might need is provided out of the box and can be used without having to think about the discord API.
To start a new bot, simply call NewBot with the token for your bot and you're basically good to go.
An example bot is provided in example/helloworld
Index ¶
- func EnableDebug()
- func EnableLogging()
- func ResetLogLevel()
- func SetLogOutput(output io.Writer)
- type Bot
- func (b *Bot) BlockForExit()
- func (b *Bot) CurrentContext() Context
- func (b *Bot) GetDiscordgoSession() *discordgo.Session
- func (b *Bot) Me() (*User, error)
- func (b *Bot) SetAvatarFromFile(file string) error
- func (b *Bot) SetAvatarFromReader(image io.Reader) error
- func (b *Bot) SetAvatarFromURI(uri string) error
- func (b *Bot) SetSink(ev EventSink)
- func (b *Bot) Sink() EventSink
- type Channel
- type Context
- func (c Context) ChannelFromID(id string) (*Channel, error)
- func (c Context) GuildFromID(id string) (*Guild, error)
- func (c Context) InviteFromID(id string) (*Invite, error)
- func (c Context) RequestExit()
- func (c Context) RoleFromID(gid, id string) (*Role, error)
- func (c Context) Self() *User
- func (c Context) UserFromID(id string) (*User, error)
- type Emoji
- type Event
- type EventMessageUpdate
- type EventNewMessage
- type EventSink
- type EventUserTyping
- type Guild
- type Invite
- type Member
- type Message
- func (m *Message) Author() (*Member, error)
- func (m *Message) AuthorUser() *User
- func (m *Message) Channel() (*Channel, error)
- func (m *Message) DisplayText() string
- func (m *Message) Edit(newText string) (*Message, error)
- func (m *Message) EditedTimestamp() (time.Time, error)
- func (m *Message) FromMe() bool
- func (m *Message) Guild() (*Guild, error)
- func (m *Message) ID() string
- func (m *Message) Mentioned(u *User) bool
- func (m *Message) MentionedMe() (bool, error)
- func (m *Message) MentionedMember(member *Member) (bool, error)
- func (m *Message) Respond(text string) (*Message, error)
- func (m *Message) Text() string
- func (m *Message) Timestamp() (time.Time, error)
- type Role
- type SimpleMessageHandler
- type User
- type UserSession
- func (b *UserSession) CurrentContext() Context
- func (b *UserSession) GetDiscordgoSession() *discordgo.Session
- func (b *UserSession) Me() (*User, error)
- func (b *UserSession) SetAvatarFromFile(file string) error
- func (b *UserSession) SetAvatarFromReader(image io.Reader) error
- func (b *UserSession) SetAvatarFromURI(uri string) error
- func (b *UserSession) SetSink(ev EventSink)
- func (b *UserSession) Sink() EventSink
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnableDebug ¶
func EnableDebug()
EnableDebug will call EnableLogging and then set the highest logging level. Use this to see what might go wrong under the hood.
func EnableLogging ¶
func EnableLogging()
EnableLogging enables default Level logging. The Logging Level is not reset
func ResetLogLevel ¶
func ResetLogLevel()
ResetLogLevel returns the logger to Warn messages or higher.
func SetLogOutput ¶
SetLogOutput will set the log output to the given io.Writer. It will not enable logging and if it was already enabled, EnableLogging **MUST** be called again otherwise it will continue to output on the previous io.Writer.
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
A bot is a running discord session intended for usage with a Bot-type Login
func NewBot ¶
NewBot uses the given token to log into discord and return a Bot-type session.
The "Bot " authorization string is automatically appended if not found.
func (*Bot) BlockForExit ¶
func (b *Bot) BlockForExit()
BlockForExit will block on the exit channel of the bot. Any code that has access to the application context can unblock this method, which usually means exiting the application. Exiting the entire application is not enforced, this function only blocks, so it may be used to restart the bot too.
func (*Bot) CurrentContext ¶
CurrentContext returns the current context pointer.
func (*Bot) GetDiscordgoSession ¶
GetDiscordgoSession returns the underlying session the bot instance is using. This is not recommended unless the functionality you want to use is not implemented on d.g yet.
func (*Bot) SetAvatarFromFile ¶
SetAvatarFromFile will read the file and set it as avatar
func (*Bot) SetAvatarFromReader ¶
SetAvatarFromReader will read the given io.Reader and set the content as avatar
Remember to close the reader after use if applicable.
func (*Bot) SetAvatarFromURI ¶
SetAvatarFromURI will download the URI data and set it as avatar
type Channel ¶
type Channel struct {
// contains filtered or unexported fields
}
Channel wraps a channel with context
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context represents a particular Discord Session. It is available to all objects it spawns. If operating a user, it will usually contain a context and give all objects it creates the context too. This allows to access the DiscordSession from everywhere, thus allowing to use any method of a type in any code context.
An example would be to ban a user that has a mention in a message received. Simply retrieve the mention, get the user object from this mention and call the Ban() method on the user.
func (Context) ChannelFromID ¶
ChannelFromID converts a ChannelID to a Channel instance or returns an error
func (Context) GuildFromID ¶
GuildFromID converts a GuildID to a Guild instance or return an error
func (Context) InviteFromID ¶
InviteFromID converts a InviteID to an Invite instance or returns an error
func (Context) RequestExit ¶
func (c Context) RequestExit()
RequestExit will send a value to the exit channel. If your bot is blocking on BlockForExit() it will return from this function. Beware that this function will block if the bot is not waiting on it.
type EventMessageUpdate ¶
type EventMessageUpdate struct {
// contains filtered or unexported fields
}
func (*EventMessageUpdate) Channel ¶
func (n *EventMessageUpdate) Channel() (*Channel, error)
func (EventMessageUpdate) Context ¶
func (n EventMessageUpdate) Context() Context
func (EventMessageUpdate) Message ¶
func (n EventMessageUpdate) Message() *Message
func (EventMessageUpdate) Name ¶
func (n EventMessageUpdate) Name() string
type EventNewMessage ¶
type EventNewMessage struct {
// contains filtered or unexported fields
}
func (*EventNewMessage) Channel ¶
func (n *EventNewMessage) Channel() (*Channel, error)
func (EventNewMessage) Context ¶
func (n EventNewMessage) Context() Context
func (*EventNewMessage) Message ¶
func (n *EventNewMessage) Message() *Message
func (EventNewMessage) Name ¶
func (n EventNewMessage) Name() string
type EventUserTyping ¶
type EventUserTyping struct {
// contains filtered or unexported fields
}
func (EventUserTyping) Channel ¶
func (n EventUserTyping) Channel() (*Channel, error)
func (EventUserTyping) Context ¶
func (n EventUserTyping) Context() Context
func (EventUserTyping) Name ¶
func (n EventUserTyping) Name() string
func (EventUserTyping) User ¶
func (n EventUserTyping) User() (*User, error)
type Member ¶
type Member struct {
// contains filtered or unexported fields
}
func (*Member) BanAndDeleteMessages ¶
BanAndDeleteMessages will create a ban and also delete messages of the past days, specified by the days parameter, or return an error.
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message wraps the message type with context
func (*Message) AuthorUser ¶
AuthorUser returns the user that sent the message
func (*Message) DisplayText ¶
DisplayText returns the text of a message with mentions replaced.
func (*Message) Edit ¶
Edit edits the message and returns the new, edited message and an error if one occured.
func (*Message) EditedTimestamp ¶
EditedTimestamp returns the timestamp of a message when it was edited
func (*Message) Guild ¶
Guild returns the guild the message was send in. It returns an error if the lookup fails, for example if it's a private message channel.
func (*Message) Mentioned ¶
Mentioned returns true if the given user was mentioned. Note that it will not check against roles. Use MentionedMember for this.
func (*Message) MentionedMe ¶
MentionedMe returns true if the current context user was mentioned. It might return an error in which case it will also return false.
func (*Message) MentionedMember ¶
MentionedMember returns true if the specified member was mentioned. It might return an error in which case it will also return false.
type Role ¶
type Role struct {
// contains filtered or unexported fields
}
type SimpleMessageHandler ¶
type SimpleMessageHandler struct {
// contains filtered or unexported fields
}
SimpleMessageHandler is a very simple event dispatcher meant for simple hello-world type bots.
It is recommended to write your own EventHandler that fits your need. This is much safer in terms of typesafety and simplicity.
func (SimpleMessageHandler) Dispatch ¶
func (s SimpleMessageHandler) Dispatch(ev Event) error
type UserSession ¶
type UserSession struct {
// contains filtered or unexported fields
}
A bot is a running discord session intended for usage with a Bot-type Login
func NewUser ¶
func NewUser(token string) (*UserSession, error)
NewUser uses the given token to log into discord and return a User-type session.
func (*UserSession) CurrentContext ¶
func (b *UserSession) CurrentContext() Context
CurrentContext returns the current context pointer.
func (*UserSession) GetDiscordgoSession ¶
func (b *UserSession) GetDiscordgoSession() *discordgo.Session
GetDiscordgoSession returns the underlying session the bot instance is using. This is not recommended unless the functionality you want to use is not implemented on d.g yet.
func (*UserSession) Me ¶
func (b *UserSession) Me() (*User, error)
Me returns the current bot user (using @me) or an error.
func (*UserSession) SetAvatarFromFile ¶
func (b *UserSession) SetAvatarFromFile(file string) error
SetAvatarFromFile will read the file and set it as avatar
func (*UserSession) SetAvatarFromReader ¶
func (b *UserSession) SetAvatarFromReader(image io.Reader) error
SetAvatarFromReader will read the given io.Reader and set the content as avatar
Remember to close the reader after use if applicable.
func (*UserSession) SetAvatarFromURI ¶
func (b *UserSession) SetAvatarFromURI(uri string) error
SetAvatarFromURI will download the URI data and set it as avatar
func (*UserSession) SetSink ¶
func (b *UserSession) SetSink(ev EventSink)
SetSink sets the EventSink to be used by D.G
func (*UserSession) Sink ¶
func (b *UserSession) Sink() EventSink
Sink returns the current EventSink
This may be used by plugins to dispatch custom events.