onyxcord

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 15 Imported by: 0

README

onyxcord

onyxcord

Discord bots with Go as straightforward as possible.

DocumentationDiscord serverLicense

⚠ This is only a prototype and for a personal use. Don't plan to make huge bots with this library.

🔧 Setup

Get the dependency from the source:

go get -u github.com/theovidal/onyxcord

In your code, create the bot instance:

bot := onyxcord.RegisterBot("MyBot", true)

You can then register commands:

pingCommand := *onyxcord.Command{
	// ...
}
bot.RegisterCommand("ping", &pingCommand)

Specify the intents of your bot, so it can receive proper events:

bot.Client.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages)

Finally, connect it to Discord with the Run method! See more on the documentation of the library.

💻 Development

TODO

📜 Credits

🔐 License

GNU GPL v3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeEmbed

func MakeEmbed(config *Config, base *discordgo.MessageEmbed) *discordgo.MessageEmbed

MakeEmbed returns a Discord embed with the style of the bot

func OpenCache

func OpenCache(config *Config) (client *redis.Client)

func OpenDatabase

func OpenDatabase(config *Config) (client *mongo.Client)

func OpenFile

func OpenFile(path string) (data []byte, err error)

OpenFile opens a file from a path

Types

type Bot

type Bot struct {
	// Name of the bot
	Name string
	// The Discord client associated with the bot
	Client *discordgo.Session
	// A list of all the commands available on the bot
	Commands map[string]*Command
	// The mongodb database attached to the bot (if used)
	Database *mongo.Client
	// The Redis cache attached to the bot (if used)
	Cache *redis.Client
	// The configuration of the bot, as defined in the corresponding file
	Config *Config
	// The profile of the bot
	User *discordgo.User
}

Bot represents the Discord bot with its assets

func RegisterBot

func RegisterBot(name string, registerHandler bool) Bot

RegisterBot creates a new instance of the Discord Bot

func (*Bot) ExecuteCommand

func (bot *Bot) ExecuteCommand(command *Command, arguments []string, message *discordgo.MessageCreate)

ExecuteCommand executes the command parsed in the OnCommand function

func (*Bot) OnCommand

func (bot *Bot) OnCommand(session *discordgo.Session, message *discordgo.MessageCreate)

OnCommand reacts to a newly-created message and treats it

func (*Bot) RegisterCommand

func (bot *Bot) RegisterCommand(name string, command *Command)

func (*Bot) Run

func (bot *Bot) Run()

Run starts the bot and connects it to Discord

type Command

type Command struct {
	// Description of the command (e.g : Prints weather for a specific location)
	Description string
	// Usage of the command (e.g : weather <location>)
	Usage string
	// Category of the command, as defined in the configuration (e.g : utilities)
	Category string
	// An alias to another command (e.g : w)
	Alias string
	// Choose if the command is shown in the help or not
	Show bool
	// Whether the bot should listen to the command in public channels
	ListenInPublic bool
	// Whether the bot should listen to the command in direct messages
	ListenInDM bool
	// Lock the command only for certain channels
	Channels []int
	// Lock the command only for certain user roles
	Roles []int
	// Lock the command only for certain members on the server
	Members []int
	// Action to execute if the command is triggered
	Execute func(arguments []string, bot Bot, message *discordgo.MessageCreate) (err error)
}

Command represents a command that can be executed by a user

func Help

func Help() *Command

func (Command) Prettify

func (command Command) Prettify(name string, prefix string) (prettified string)

Prettify returns a string with information about a command, ready to be printed to the user

type Config

type Config struct {
	// Information for the development
	Dev struct {
		// Activates the debug mode, so the bot logs everything
		Debug bool
		// Version of the bot
		Version string
		// Information about the maintainer
		Maintainer struct {
			Name string
			Link string
		}
	}
	// Information about the bot itself
	Bot struct {
		// Its name
		Name string
		// The token to connect to Discord
		Token string
		// Discord's client ID
		ID string
		// An illustration, for example its logo
		Illustration string
		// A website on which users can go for further information
		Website string
		// Prefix for all the commands (e.g: !)
		Prefix string
		// The color theme of the bot (e.g: #b7c1c)
		Color int
	}
	// A list of command categories
	Categories map[string]struct {
		// Their name, displayed in the help command
		Name string
		// Their emoji, displayed in the help command
		Emoji string
	}
	// Some assets user can add
	Assets map[string]string
	// Information to connect to the MongoDB database
	Database struct {
		// Address of the database (e.g: localhost)
		Address string
		// Port of the database (e.g: 1234)
		Port int
		// Username to connect to the database (e.g: onyxcord)
		Username string
		// Password for this username
		Password string
		// Database to use
		Database string
		// Database to connect from (e.g: onyxcord)
		AuthSource string `yaml:"auth_source"`
	}
	Cache struct {
		// Address of the cache (e.g: localhost)
		Address string
		// Port of the database (e.g: 6379)
		Port string
	}
}

Config represents the configuration stored in the file

func GetConfig

func GetConfig() (config Config, err error)

GetConfig reads a specific bot configuration and parses it into the Config structure

Jump to

Keyboard shortcuts

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