godbot

package module
v0.0.0-...-34dacc2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2017 License: MIT Imports: 7 Imported by: 0

README

Discord Bot

This is the second iteration of my Go based Discord bot. First attempt, although successful, quickly snowballed into a much larger project than expected.

After learning a fair bit about Go and using the Discord API bindings, I am making my second attempt at cleaner and more efficient practices by splitting up the code base into more appropriate packages.

The original can still be seen at the link above.

Features

  • Easy to start up and stop
  • Expandable
  • Support for custom message handling functions
Versions

see changelog

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilToken   = errors.New("token is not set")
	ErrNilHandler = errors.New("message handler not assigned")
)

Error constants

View Source
var (
	ErrNilGuilds   = errors.New("godbot: bot.guilds is nil (bot may not be in guilds)")
	ErrNilChannels = errors.New("godbot: bot.channels is nil")
	ErrNilLinks    = errors.New("godbot: bot.links is nil")
	ErrNotFound    = errors.New("godbot: not found")
)

Standard error messages

View Source
var (
	ErrChannelNotLocked = errors.New("channel is not locked")
	ErrChannelLocked    = errors.New("channel is locked")
	ErrNilChannelLock   = errors.New("provided a nil channel lock")
	ErrBadChannel       = errors.New("bad channel for operation")
	ErrBadGuild         = errors.New("bad guild for operation")
)

Constants for locked channels.

Functions

This section is empty.

Types

type Channel

type Channel struct {
	*discordgo.Channel
}

Channel contains channel data.

type ChannelLock

type ChannelLock struct {
	Locked     bool
	Session    *discordgo.Session
	Guild      *Guild
	Channel    *Channel
	Roles      []*discordgo.Role
	Overwrites []*discordgo.PermissionOverwrite
	Message    *discordgo.Message
}

ChannelLock holds Locking information for a Channel.

func (*ChannelLock) ChannelLock

func (cl *ChannelLock) ChannelLock(alert bool) error

ChannelLock will lock a channel preventing @everyone typing.

func (*ChannelLock) ChannelUnlock

func (cl *ChannelLock) ChannelUnlock() error

ChannelUnlock will unlock a channel allowing for @everyone to type.

type Connections

type Connections struct {
	Links    map[string][]*discordgo.Channel
	Guilds   []*discordgo.Guild
	Channels []*discordgo.Channel
}

Connections holds all connection data.

type Core

type Core struct {
	sync.Mutex

	// User Information
	User     *discordgo.User
	Username string
	Status   string
	ID       int
	Token    string

	Stream   bool
	Game     string
	LiteMode bool // If it loads EVERYTHING.

	Ready *discordgo.Ready

	// Connection Information.
	Session     *discordgo.Session
	ChannelMain *discordgo.Channel
	Channels    []*discordgo.Channel
	GuildMain   *discordgo.Guild
	Guilds      []*discordgo.Guild

	// Link map: [guild ID] []*Channels
	Links   map[string][]*discordgo.Channel
	Private []*discordgo.Channel
	// contains filtered or unexported fields
}

Core is the basic structure of the bot.

func New

func New(token string) (*Core, error)

New creates a new instance of the bot.

func (*Core) ChannelDeleteHandler

func (bot *Core) ChannelDeleteHandler(channelHandler func(*discordgo.Session, *discordgo.ChannelDelete))

ChannelDeleteHandler for an event where a channel is removed from a guild.

func (*Core) ChannelLockCreate

func (bot *Core) ChannelLockCreate(cID string) (*ChannelLock, error)

ChannelLockCreate returns a ChannelLock struct.

func (*Core) ChannelMemoryAdd

func (bot *Core) ChannelMemoryAdd(channel *discordgo.Channel)

ChannelMemoryAdd will Add/Replace a channels structure in memory.

func (*Core) ChannelMemoryDelete

func (bot *Core) ChannelMemoryDelete(channel *discordgo.Channel)

ChannelMemoryDelete will remove a channel from the array of channels in memory.

func (*Core) ChannelToSlice

func (bot *Core) ChannelToSlice(guildID, infoType string) (channels []string)

ChannelToSlice grants a list of currently accessible channels for a particular guild. By default, it will return a list of IDs. Giving "name" for infoType will return a list of channel names.

func (*Core) ChannelUpdateHandler

func (bot *Core) ChannelUpdateHandler(channelHandler func(*discordgo.Session, *discordgo.ChannelUpdate))

ChannelUpdateHandler for any events that update a channel.

func (*Core) ConnectionsReset

func (bot *Core) ConnectionsReset() error

ConnectionsReset sets the defaults for the bots connections.

func (*Core) GetChannel

func (bot *Core) GetChannel(cID string) *Channel

GetChannel gets a Channel struct based on Channel ID.

func (*Core) GetConnections

func (bot *Core) GetConnections() (*Connections, error)

GetConnections returns the current connection structure.

func (*Core) GetGuild

func (bot *Core) GetGuild(gID string) *Guild

GetGuild gets a Guild structure from a Guild ID.

func (*Core) GetGuildID

func (bot *Core) GetGuildID(cID string) (string, error)

GetGuildID gets the ID of a guild from a Channel ID.

func (*Core) GetGuildMembers

func (bot *Core) GetGuildMembers(guildID string, userAmount int) ([]*discordgo.Member, error)

GetGuildMembers returns EVERY user in a guild.

func (*Core) GetMainChannel

func (bot *Core) GetMainChannel(gID string) *discordgo.Channel

GetMainChannel sets the main channel for the bot.

func (*Core) GuildCreateHandler

func (bot *Core) GuildCreateHandler(createHandler func(*discordgo.Session, *discordgo.GuildCreate))

GuildCreateHandler assigns a function to deal with newly create guilds.

func (*Core) GuildMemberAddHandler

func (bot *Core) GuildMemberAddHandler(userHandler func(*discordgo.Session, *discordgo.GuildMemberAdd))

GuildMemberAddHandler assigns a function to deal with newly joining users.

func (*Core) GuildMemberRemoveHandler

func (bot *Core) GuildMemberRemoveHandler(userHandler func(*discordgo.Session, *discordgo.GuildMemberRemove))

GuildMemberRemoveHandler assigns a function to deal with leaving users.

func (*Core) GuildMemberUpdateHandler

func (bot *Core) GuildMemberUpdateHandler(userHandler func(*discordgo.Session, *discordgo.GuildMemberUpdate))

GuildMemberUpdateHandler assigns a function to deal with updating users.

func (*Core) GuildRoleDeleteHandler

func (bot *Core) GuildRoleDeleteHandler(deleteHandler func(*discordgo.Session, *discordgo.GuildRoleDelete))

GuildRoleDeleteHandler checks if guild roles are removed.

func (*Core) GuildRoleUpdateHandler

func (bot *Core) GuildRoleUpdateHandler(updateHandler func(*discordgo.Session, *discordgo.GuildRoleUpdate))

GuildRoleUpdateHandler will process new updates for guild roles.

func (*Core) GuildToSlice

func (bot *Core) GuildToSlice(infoType string) (guilds []string)

GuildToSlice gives a list of current accessible Guild infoType defaults to Guild ID, if infoType is set to "name" it will return names. otherwise it will return the IDs.

func (*Core) GuildsString

func (bot *Core) GuildsString() string

GuildsString converts the entire guild list into string format.

func (*Core) MessageCreateHandler

func (bot *Core) MessageCreateHandler(msgHandler func(*discordgo.Session, *discordgo.MessageCreate))

MessageCreateHandler assigns a function to handle messages.

func (*Core) MessageUpdateHandler

func (bot *Core) MessageUpdateHandler(msgHandler func(*discordgo.Session, *discordgo.MessageUpdate))

MessageUpdateHandler assigns a function to handle messages.

func (*Core) SetMainChannel

func (bot *Core) SetMainChannel(gID, cID string) error

SetMainChannel sets the channel to primarily sit in.

func (*Core) SetMainGuild

func (bot *Core) SetMainGuild(gID string)

SetMainGuild assigns the guild and channel to the main server.

func (*Core) SetNickname

func (bot *Core) SetNickname(gID, name string, append bool) error

SetNickname will set the current name of the bot to the guild.

func (*Core) Start

func (bot *Core) Start() error

Start initiates the bot, attempts to connect to Discord.

func (*Core) Stop

func (bot *Core) Stop() error

Stop shuts down the bot.

func (*Core) UpdateConnections

func (bot *Core) UpdateConnections() error

UpdateConnections is a public wrapper queries discord for all information needed by bot.

func (*Core) UserID

func (bot *Core) UserID(name string) (string, error)

UserID turns a Username#Discriminator into an ID.

type Guild

type Guild struct {
	*discordgo.Guild
}

Guild contains guild data.

type User

type User struct {
	*discordgo.User
}

User contains user data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL