bot

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// StatusAction for kadok to respond with general information about the itself
	StatusAction = Action{
		security.EmptyPermission,
		"\nJe te dis ce que j'ai dans mon ventre ! `plus` si t'en veux plus !",
		map[string]*Action{},
		EmptyData,
		"status.tmpl",
	}

	// GetCharactersAction to retrieve the list of available characters
	GetCharactersAction = Action{
		security.GetCharacterList,
		"C'est Kadok qui a pleins d'amis",
		map[string]*Action{},
		func(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{} {
			return map[string]interface{}{
				"Characters": Configuration.Characters.List,
			}
		},
		"aqui.tmpl",
	}

	GroupRootAction = Action{
		security.EmptyPermission,
		"Toutes les actions qui concernent les groupes !" +
			"> `kadok groupe liste`\n" +
			"> `kadok groupe rejoindre <groupId|groupName>`\n" +
			"> `kadok groupe quitter <groupId|groupName>`",
		map[string]*Action{
			"LISTE":     &GroupListAction,
			"REJOINDRE": &GroupJoinAction,
			"QUITTER":   &GroupLeaveAction,
		},
		EmptyData,
		"404.tmpl",
	}

	GroupListAction = Action{
		security.EmptyPermission,
		"La liste des groupes disponibles !",
		map[string]*Action{},
		func(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{} {
			groups := Configuration.Security.RolesHierarchy.GetGroups()
			return map[string]interface{}{
				"Groups": groups,
			}
		},
		"groupList.tmpl",
	}

	GroupJoinAction = Action{
		security.EmptyPermission,
		"C'est ici qu'on rejoint un groupe !\n> Commande : `kadok groupe rejoindre <groupId|groupName>`",
		map[string]*Action{},
		func(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{} {
			roleReference := strings.Join(parameters, " ")
			addRole := MakeAddRole(s, m)
			group, err := Configuration.Security.RolesHierarchy.JoinGroup(addRole, roleReference)
			return map[string]interface{}{
				"Username": m.Author.Username,
				"Group":    group,
				"Error":    err,
			}
		},
		"groupJoin.tmpl",
	}

	GroupLeaveAction = Action{
		security.EmptyPermission,
		"C'est ici qu'on part d'un groupe ! Snif..\n> Commande : `kadok groupe quitter <groupId|groupName>`",
		map[string]*Action{},
		func(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{} {
			roleReference := strings.Join(parameters, " ")
			removeRole := MakeRemoveRole(s, m)
			group, err := Configuration.Security.RolesHierarchy.LeaveGroup(removeRole, roleReference)
			return map[string]interface{}{
				"Username": m.Author.Username,
				"Group":    group,
				"Error":    err,
			}
		},
		"groupLeave.tmpl",
	}

	ClanRootAction = Action{
		security.EmptyPermission,
		"Toutes les actions qui concernent les clans !\n" +
			"> `kadok clan liste`\n" +
			"> `kadok clan rejoindre <clanId|clanName>`\n" +
			"> `kadok clan quitter <clanId|clanName>`",
		map[string]*Action{
			"LISTE":     &ClanListAction,
			"REJOINDRE": &ClanJoinAction,
			"QUITTER":   &ClanLeaveAction,
		},
		EmptyData,
		"404.tmpl",
	}

	ClanListAction = Action{
		security.EmptyPermission,
		"La liste des clans disponibles !",
		map[string]*Action{},
		func(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{} {
			clans := Configuration.Security.RolesHierarchy.GetClans()
			return map[string]interface{}{
				"Clans": clans,
			}
		},
		"clanList.tmpl",
	}

	ClanJoinAction = Action{
		security.EmptyPermission,
		"C'est ici qu'on rejoint un clan !\n> Commande : `kadok clan rejoindre <clanId|clanName>`",
		map[string]*Action{},
		func(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{} {
			roleReference := strings.Join(parameters, " ")
			addRole := MakeAddRole(s, m)
			removeRole := MakeRemoveRole(s, m)
			clan, err := Configuration.Security.RolesHierarchy.JoinClan(addRole, removeRole, roleReference)
			return map[string]interface{}{
				"Username": m.Author.Username,
				"Clan":     clan,
				"Error":    err,
			}
		},
		"clanJoin.tmpl",
	}

	ClanLeaveAction = Action{
		security.EmptyPermission,
		"C'est ici qu'on part d'un clan ! Snif..\n> Commande : `kadok clan quitter <clanId|clanName>`",
		map[string]*Action{},
		func(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{} {
			roleReference := strings.Join(parameters, " ")
			removeRole := MakeRemoveRole(s, m)
			clan, err := Configuration.Security.RolesHierarchy.LeaveClan(removeRole, roleReference)
			return map[string]interface{}{
				"Username": m.Author.Username,
				"Clan":     clan,
				"Error":    err,
			}
		},
		"clanLeave.tmpl",
	}

	// RootAction is the first action call by Kadok for resolve
	RootAction = Action{
		security.GetHelp,
		"Tatan elle fait du flan, elle m'a aussi dit de dire des choses intelligentes si on m'appel: \n" +
			"> - `kadok aide` je te dis ce que je fais ! Et `kadok <commande> aide` je te donne plus de details !\n" +
			"> - `kadok aqui` ? Je dis tous mes amis !\n" +
			"> - `kadok tatan` je te parle de moi !\n" +
			"> - `kadok groupe <liste|rejoindre|quitter>` Pour voir et rejoindre un groupe ! Tu peux etre dans autant de groupes que tu veux !\n" +
			"> - `kadok clan <liste|rejoindre|quitter>` Pour voir et rejoindre un clan ! Tu peux avoir seulement un clan !",
		map[string]*Action{
			"AQUI":   &GetCharactersAction,
			"TATAN":  &StatusAction,
			"GROUPE": &GroupRootAction,
			"CLAN":   &ClanRootAction,
		},
		EmptyData,
		"404.tmpl",
	}
)

Functions

func EmptyData

func EmptyData(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{}

EmptyData is an helper to instanciate an action that doesn't rely on data

func GetRoleID

func GetRoleID(s *discordgo.Session, guildID string, roleName string) (string, error)

func GetUserRoles

func GetUserRoles(s *discordgo.Session, m *discordgo.MessageCreate) ([]string, error)

GetUserRoles retrieves the list of roles names of the User who sent the message.

func ResolveAction

func ResolveAction(rootAction *Action, call []string) (*Action, ExecuteAction)

ResolveAction is used to resolve the action that should be executed from a command. It returns the action to execute and the function to execute it as well.

func Run

func Run(token string, configPath string)

Run starts the bot. and call for registering the handlers.

Types

type Action

type Action struct {
	// The permission required to use this action. Use the EmptyPermission if no permission is required
	Permission security.Permission
	// Information regarding the action. It is the message that should be displayed when requesting for help
	Information string
	// The sub actions in the hierarchy. They do not inherit the permission requirement
	SubActions map[string]*Action
	// Executed on call to format data passed to template
	GetData func(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) map[string]interface{}
	// Path to the action template
	Template string
}

Action structure containing information and functions regarding an action

func (*Action) Execute

func (a *Action) Execute(s *discordgo.Session, m *discordgo.MessageCreate, parameters []string) (string, error)

Execute is the function to execute when the action is called

func (*Action) GetPermission

func (a *Action) GetPermission() security.Permission

GetPermission is used with the security module to check the permission of the action

func (*Action) GetTemplate

func (a *Action) GetTemplate(parameters []string) *template.Template

GetTemplate resolves template file

type ExecuteAction

type ExecuteAction func(s *discordgo.Session, m *discordgo.MessageCreate) (string, error)

ExecuteAction is the function type to implement with an Action

func MakeExecuteAction

func MakeExecuteAction(action *Action, parameters []string) ExecuteAction

MakeExecuteAction from an action

type Guild

type Guild struct {
	Name string `yaml:"name"`
	ID   string
}

type Properties

type Properties struct {
	//Prefix value used to call the bot
	Prefix     string                `yaml:"prefix"`
	Guild      Guild                 `yaml:"guild"`
	Characters characters.Properties `yaml:"characters"`
	Security   security.Properties   `yaml:"security"`
	Templates  string                `yaml:"templates"`
}

Properties is used to easily load packages' properties from a single yaml properties file

In order to simplify the initialization of Kadok and left the responsibility of the configuration format to the relevant packages. Each package CAN implement a Properties structure and add it to the main package structure. The configuration structure will automatically be taken into account into the bot properties file.

Properties is a struct in which each field is a Properties structure of a sub-package. The Properties structure of a package MUST implement the Unmarshaler interface https://godoc.org/gopkg.in/yaml.v2#Unmarshaler

var (
	Configuration Properties
)

type TplParams

type TplParams struct {
	Message       discordgo.Message
	Info          info.Info
	Configuration Properties
	Parameters    []string
	Data          map[string]interface{}
}

TplParams as the standard information passed to an action template

Jump to

Keyboard shortcuts

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