commons

package module
v0.0.0-...-16eeaae Latest Latest
Warning

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

Go to latest
Published: May 23, 2020 License: GPL-3.0 Imports: 14 Imported by: 1

README

commons


Disclaimer

This Project is a part of doll and uses:

See the examples and docs.

Doll is a discordgo framework, which is written in golang. It simplifies any kind of actions discordgo has to offer. Examples: Events, Connection, Container.

See

Getting started

It is assumed that you have already worked with the Go environment. If this is not the case, see this page first.

Installation

go get github.com/projectdoll/commons

Usage
Import the package
import "github.com/projectdoll/commons"
Create a BotApplication

The bot application is used to control many actions, as simple as possible, for the bot. e.g.: Events, Connection

// simple usage
bot := commons.NewBotApplication("TOKEN_HERE")

// advanced usage
bot := &commons.PlainBotApplication{
     Token:  "TOKEN_HERE",
}
Open Connection

Connect the discordgo.Session to the discord-api.

session, err := bot.OpenConnection(func(application commons.BotApplication, session *discordgo.Session) {
    // This function is executed before the connection. If it is not needed, use nil as parameter.
})
Listen/Receive events

See the list of premade discord-events. If u want to understand the event-emitter system fully, read this.

An advantage over the discordgo.Session.AddHandler(interface{}) event system is, that listeners can still be registered after a successful connection.

bot.ListenDiscordEvent(EVENT_TYPE, func(application commons.BotApplication, session *discordgo.Session, event interface{}) {
    // add code here
})

// full example
bot.ListenDiscordEvent(commons.EventTypeMessageCreate, func(application commons.BotApplication, session *discordgo.Session, event interface{}) {
    create := event.(*discordgo.MessageCreate)
    if commons.EqualsString(create.Content, "ping", true) {
        _, _ = session.ChannelMessageSend(create.ChannelID, "PONG!")
    }
    // add further code here
})
KeepAlive

If you want to keep the application process upright, use this at the end of your main function.

commons.KeepAlive()
Advanced Usage
Use Sharding

commons.NewSharding(int, int) requires the id and the count of the shard. If you have no experience with sharding, see this.

bot := &commons.PlainBotApplication{
    Token:        "TOKEN_HERE",
    Sharding:     commons.NewSharding( /* id: */ 0, /* count: */ 1),
}
Custom Event-Emitter

Publish and Receive Events with an custom EventEmitter from go-event-emitter.

bot := &commons.PlainBotApplication{
    Token:        "TOKEN_HERE",
    EventEmitter: eventemitter.NewEmitter( /* async: */ false),
}
Custom Events
Publish events

Publish custom events, like TwitchStreamBegin.

bot.PublishEvent("EVENT_NAME", ARGUMENTS...)
Receive/Listen events

Receive custom events, like TwitchStreamBegin.

bot.ListenEvent("EVENT_NAME", func(arguments ...interface{}) {
    // add code here
})
Custom Properties

Define custom properties to store more information for a BotApplication (PlainBotApplication). If not needed, don't implement Props or define it as nil. The same principle as for the parameter map.

bot := &commons.PlainBotApplication{
    Token: "",
    Props: commons.NewProps( /* custom set map (nil, if not needed): */ commons.Properties{
        "author": "https://lukasl.dev",
    }),
}

Documentation

Index

Constants

View Source
const (
	EventTypeEvent                             = EventType("discordgo.Event")
	EventTypeReady                             = EventType("discordgo.Ready")
	EventTypeChannelCreate                     = EventType("discordgo.ChannelCreate")
	EventTypeChannelUpdate                     = EventType("discordgo.ChannelUpdate")
	EventTypeChannelDelete                     = EventType("discordgo.ChannelDelete")
	EventTypeChannelPinsUpdate                 = EventType("discordgo.ChannelPinsUpdate")
	EventTypeGuildCreate                       = EventType("discordgo.GuildCreate")
	EventTypeGuildUpdate                       = EventType("discordgo.GuildUpdate")
	EventTypeGuildDelete                       = EventType("discordgo.GuildDelete")
	EventTypeGuildBanAdd                       = EventType("discordgo.GuildBanAdd")
	EventTypeGuildBanRemove                    = EventType("discordgo.GuildBanRemove")
	EventTypeGuildMemberAdd                    = EventType("discordgo.GuildMemberAdd")
	EventTypeGuildMemberUpdate                 = EventType("discordgo.GuildMemberUpdate")
	EventTypeGuildMemberRemove                 = EventType("discordgo.GuildMemberRemove")
	EventTypeGuildRoleCreate                   = EventType("discordgo.GuildRoleCreate")
	EventTypeGuildRoleUpdate                   = EventType("discordgo.GuildRoleUpdate")
	EventTypeGuildRoleDelete                   = EventType("discordgo.GuildRoleDelete")
	EventTypeGuildEmojisUpdate                 = EventType("discordgo.GuildEmojisUpdate")
	EventTypeGuildMembersChunk                 = EventType("discordgo.GuildMembersChunk")
	EventTypeGuildIntegrationsUpdate           = EventType("discordgo.GuildIntegrationsUpdate")
	EventTypeMessageAck                        = EventType("discordgo.MessageAck")
	EventTypeMessageCreate                     = EventType("discordgo.MessageCreate")
	EventTypeMessageUpdate                     = EventType("discordgo.MessageUpdate")
	EventTypeMessageDelete                     = EventType("discordgo.MessageDelete")
	EventTypeMessageReactionAdd                = EventType("discordgo.MessageReactionAdd")
	EventTypeMessageReactionRemove             = EventType("discordgo.MessageReactionRemove")
	EventTypeMessageReactionRemoveAll          = EventType("discordgo.MessageReactionRemoveAll")
	EventTypePresencesReplace                  = EventType("discordgo.PresencesReplace")
	EventTypePresenceUpdate                    = EventType("discordgo.PresenceUpdate")
	EventTypeResumed                           = EventType("discordgo.Resumed")
	EventTypeRelationshipAdd                   = EventType("discordgo.RelationshipAdd")
	EventTypeRelationshipRemove                = EventType("discordgo.RelationshipRemove")
	EventTypeTypingStart                       = EventType("discordgo.TypingStart")
	EventTypeUserUpdate                        = EventType("discordgo.UserUpdate")
	EventTypeUserSettingsUpdate                = EventType("discordgo.UserSettingsUpdate")
	EventTypeUserGuildSettingsUpdate           = EventType("discordgo.UserGuildSettingsUpdate")
	EventTypeUserNoteUpdate                    = EventType("discordgo.UserNoteUpdate")
	EventTypeVoiceServerUpdate                 = EventType("discordgo.VoiceServerUpdate")
	EventTypeVoiceStateUpdate                  = EventType("discordgo.VoiceStateUpdate")
	EventTypeVoiceStatMessageDeleteBulkeUpdate = EventType("discordgo.MessageDeleteBulk")
	EventTypeWebhooksUpdate                    = EventType("discordgo.WebhooksUpdate")
)

noinspection ALL

Variables

View Source
var (
	LogLevelInfo  = LogLevel{Prefix: "INFO"}
	LogLevelDebug = LogLevel{Prefix: "DEBUG"}
	LogLevelWarn  = LogLevel{Prefix: "WARN"}
	LogLevelFatal = LogLevel{Prefix: "FATAL", Printer: func(out string) {
		fmt.Print(out)
		os.Exit(1)
	}}
	LogLevelPanic = LogLevel{Prefix: "PANIC", Printer: func(out string) {
		panic(out)
	}}
)

noinspection GoUnusedGlobalVariable

Functions

func ContainsString

func ContainsString(slice []string, key string, ignoreCase bool) bool

func CurrentTimeMillis

func CurrentTimeMillis() int64

func EqualsString

func EqualsString(string1, string2 string, ignoreCase bool) bool

func FormattedTime

func FormattedTime() string

func IsConnectionFailure

func IsConnectionFailure(err error) bool

func KeepAlive

func KeepAlive()

func Print

func Print(level LogLevel, action LogAction, message string, v ...interface{})

func TrimString

func TrimString(content string) string

Types

type BotApplication

type BotApplication interface {
	GetToken() string
	GetSharding() Sharding

	GetProps() Props

	GetEmitter() *eventemitter.Emitter
	PublishEvent(eventType eventemitter.EventType, arguments ...interface{})
	PublishDiscordEvent(session *discordgo.Session, event interface{})
	ListenEvent(eventType eventemitter.EventType, listener eventemitter.HandleFunc)
	ListenDiscordEvent(eventType EventType, listener DiscordListener)

	TestAttributes()
	OpenConnection(configurator SessionConfigurator) (*discordgo.Session, error)
	IsConnected() bool
	Session() (*discordgo.Session, error)
	ForceSession() *discordgo.Session
}

func NewBotApplication

func NewBotApplication(token string) BotApplication

type DiscordListener

type DiscordListener = func(application BotApplication, session *discordgo.Session, event interface{})

type EventType

type EventType string

type LogAction

type LogAction string

type LogLevel

type LogLevel struct {
	Prefix  string
	Printer LogPrinter
}

type LogPrinter

type LogPrinter func(out string)

type Permission

type Permission int

func (Permission) HasPermission

func (permission Permission) HasPermission(session *discordgo.Session, userID, channelID string) bool

type PlainBotApplication

type PlainBotApplication struct {
	BotApplication

	Token    string
	Sharding Sharding

	EventEmitter *eventemitter.Emitter

	Props Props
	// contains filtered or unexported fields
}

func (*PlainBotApplication) ForceSession

func (plain *PlainBotApplication) ForceSession() *discordgo.Session

func (*PlainBotApplication) GetEmitter

func (plain *PlainBotApplication) GetEmitter() *eventemitter.Emitter

func (*PlainBotApplication) GetProps

func (plain *PlainBotApplication) GetProps() Props

func (*PlainBotApplication) GetSharding

func (plain *PlainBotApplication) GetSharding() Sharding

func (*PlainBotApplication) GetToken

func (plain *PlainBotApplication) GetToken() string

func (*PlainBotApplication) IsConnected

func (plain *PlainBotApplication) IsConnected() bool

func (*PlainBotApplication) ListenDiscordEvent

func (plain *PlainBotApplication) ListenDiscordEvent(eventType EventType, listener DiscordListener)

func (*PlainBotApplication) ListenEvent

func (plain *PlainBotApplication) ListenEvent(eventType eventemitter.EventType, listener eventemitter.HandleFunc)

func (*PlainBotApplication) OpenConnection

func (plain *PlainBotApplication) OpenConnection(configurator SessionConfigurator) (*discordgo.Session, error)

func (*PlainBotApplication) PublishDiscordEvent

func (plain *PlainBotApplication) PublishDiscordEvent(session *discordgo.Session, event interface{})

func (*PlainBotApplication) PublishEvent

func (plain *PlainBotApplication) PublishEvent(eventType eventemitter.EventType, arguments ...interface{})

func (*PlainBotApplication) Session

func (plain *PlainBotApplication) Session() (*discordgo.Session, error)

func (*PlainBotApplication) TestAttributes

func (plain *PlainBotApplication) TestAttributes()

type PlainProps

type PlainProps struct {
	Properties Properties
}

func (*PlainProps) FilterProperties

func (plain *PlainProps) FilterProperties(filter PropertyFilter) []interface{}

func (*PlainProps) GetMap

func (plain *PlainProps) GetMap() Properties

func (*PlainProps) GetProperty

func (plain *PlainProps) GetProperty(property string) (interface{}, error)

func (*PlainProps) GetPropertyOrDefault

func (plain *PlainProps) GetPropertyOrDefault(property string, alternative interface{}) interface{}

func (*PlainProps) HasProperty

func (plain *PlainProps) HasProperty(property string) bool

func (*PlainProps) RemoveProperty

func (plain *PlainProps) RemoveProperty(property string)

func (*PlainProps) SetProperties

func (plain *PlainProps) SetProperties(props Properties)

func (*PlainProps) SetProperty

func (plain *PlainProps) SetProperty(property string, value interface{})

type PlainSharding

type PlainSharding struct {
	Sharding
	ID    int // default: '0'
	Count int // default '1'
}

func (*PlainSharding) GetCount

func (plain *PlainSharding) GetCount() int

func (*PlainSharding) GetID

func (plain *PlainSharding) GetID() int

type Properties

type Properties map[string]interface{}

type PropertyFilter

type PropertyFilter func(key string, value interface{}) bool

type Props

type Props interface {
	GetMap() Properties
	FilterProperties(filter PropertyFilter) []interface{}
	HasProperty(property string) bool
	GetProperty(property string) (interface{}, error)
	GetPropertyOrDefault(property string, alternative interface{}) interface{}
	SetProperty(property string, value interface{})
	SetProperties(props Properties)
	RemoveProperty(property string)
}

func NewProps

func NewProps(values Properties) Props

type SessionConfigurator

type SessionConfigurator = func(application BotApplication, session *discordgo.Session)

type Sharding

type Sharding interface {
	GetID() int    // default: '0'
	GetCount() int // default: '1'
}

func NewSharding

func NewSharding(id, count int) Sharding

Jump to

Keyboard shortcuts

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