Documentation ¶
Overview ¶
rolecommands is a plugin which allows users to assign roles to themselves
Index ¶
- Constants
- Variables
- func CheckIgnoredRoles(ignore []int64, has []int64) error
- func CheckRequiredRoles(requireOneOf []int64, has []int64) bool
- func ClearRolemenuCache(gID int64)
- func CmdFuncListCommands(parsed *dcmd.Data) (interface{}, error)
- func CmdFuncRole(parsed *dcmd.Data) (interface{}, error)
- func ContinueRoleMenuSetup(ctx context.Context, rm *models.RoleMenu, emoji *discordgo.Emoji, userID int64) (resp string, err error)
- func FindRolemenuFull(ctx context.Context, mID int64, guildID int64) (*models.RoleMenu, error)
- func FindToggleRole(ctx context.Context, ms *dstate.MemberState, name string) (gaveRole bool, err error)
- func GetAllRoleCommandsSorted(ctx context.Context, guildID int64) (groups []*models.RoleGroup, ...)
- func GetRolemenuCached(ctx context.Context, gs *dstate.GuildSet, messageID int64) (*models.RoleMenu, error)
- func HandleDeleteRoleCommands(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
- func HandleGetGroup(groupID int64, w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error)
- func HandleGetIndex(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error)
- func HandleMoveCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
- func HandleNewCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
- func HandleNewGroup(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
- func HandleRemoveCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
- func HandleRemoveGroup(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
- func HandleUpdateCommand(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error)
- func HandleUpdateGroup(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error)
- func HumanizeAssignError(guild *dstate.GuildSet, err error) (string, error)
- func IsRoleCommandError(err error) bool
- func MemberChooseOption(ctx context.Context, rm *models.RoleMenu, gs *dstate.GuildSet, ...) (resp string, err error)
- func MenuReactedNotDone(ctx context.Context, gs *dstate.GuildSet, rm *models.RoleMenu, ...) (resp string, err error)
- func NewCommonRoleError(msg string, r *CommonRoleSettings) error
- func NewGroupError(msg string, group *models.RoleGroup) error
- func NewLmitError(msg string, limit int) error
- func NewRoleError(msg string, role int64) error
- func NewSimpleError(format string, args ...interface{}) error
- func NextRoleMenuSetupStep(ctx context.Context, rm *models.RoleMenu, first bool) (resp string, err error)
- func OptionName(gs *dstate.GuildSet, opt *models.RoleMenuOption) string
- func OptionsLessFunc(standalone bool, slice []*models.RoleMenuOption) func(int, int) bool
- func RegisterPlugin()
- func RoleCommandsLessFunc(slice []*models.RoleCommand) func(int, int) bool
- func StrFlags(rm *models.RoleMenu) string
- func StringCommands(cmds []*models.RoleCommand) string
- func UpdateMenu(parsed *dcmd.Data, menu *models.RoleMenu) (interface{}, error)
- func UpdateRoleMenuMessage(ctx context.Context, rm *models.RoleMenu) error
- type CacheKey
- type CommonRoleError
- type CommonRoleSettings
- func (c *CommonRoleSettings) AllGroupRoles(ctx context.Context) []*CommonRoleSettings
- func (c *CommonRoleSettings) AssignRole(ctx context.Context, ms *dstate.MemberState) (gaveRole bool, err error)
- func (c *CommonRoleSettings) CanRole(ctx context.Context, ms *dstate.MemberState) (can bool, err error)
- func (c *CommonRoleSettings) CheckToggleRole(ctx context.Context, ms *dstate.MemberState) (gaveRole bool, err error)
- func (c *CommonRoleSettings) GroupToggleRole(ctx context.Context, ms *dstate.MemberState) (gaveRole bool, err error)
- func (c *CommonRoleSettings) MaybeScheduleRoleRemoval(ctx context.Context, ms *dstate.MemberState) error
- func (c *CommonRoleSettings) ModeSettings() *ModeSettings
- func (c *CommonRoleSettings) ParentCanRole(ctx context.Context, ms *dstate.MemberState) (can bool, err error)
- func (c *CommonRoleSettings) RemoveRole(ctx context.Context, ms *dstate.MemberState) (removedRole bool, err error)
- func (c *CommonRoleSettings) ToggleRole(ms *dstate.MemberState) (gaveRole bool, err error)
- type FormCommand
- type FormGroup
- type GroupError
- type LmitError
- type ModeSettings
- type Plugin
- type RecentMenusTracker
- type RecentTrackedMenu
- type RoleError
- type ScheduledEventUpdateMenuMessageData
- type ScheduledMemberRoleRemoveData
- type SimpleError
Constants ¶
const ( GroupModeNone = iota GroupModeSingle GroupModeMultiple )
const ( RoleMenuStateSettingUp = 0 RoleMenuStateDone = 1 RoleMenuStateEditingOptionSelecting = 2 RoleMenuStateEditingOptionReplacing = 3 )
Variables ¶
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS role_groups (
id bigserial NOT NULL PRIMARY KEY,
guild_id bigint NOT NULL,
name text NOT NULL,
require_roles bigint[],
ignore_roles bigint[],
mode bigint NOT NULL,
multiple_max bigint NOT NULL,
multiple_min bigint NOT NULL,
single_auto_toggle_off boolean NOT NULL,
single_require_one boolean NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS role_groups_guild_idx ON role_groups(guild_id);
`, `
CREATE TABLE IF NOT EXISTS role_commands (
id bigserial NOT NULL PRIMARY KEY,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
guild_id bigint NOT NULL,
name text NOT NULL,
role_group_id bigint REFERENCES role_groups(id) ON DELETE SET NULL,
role bigint NOT NULL,
require_roles bigint[],
ignore_roles bigint[],
position bigint NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS role_commands_guild_idx ON role_commands(guild_id);
`, `
CREATE INDEX IF NOT EXISTS role_commands_role_group_idx ON role_commands(role_group_id);
`, `
CREATE TABLE IF NOT EXISTS role_menus (
message_id bigint NOT NULL PRIMARY KEY,
guild_id bigint NOT NULL,
channel_id bigint NOT NULL,
owner_id bigint NOT NULL,
own_message boolean NOT NULL,
state bigint NOT NULL,
next_role_command_id bigint REFERENCES role_commands(id) ON DELETE SET NULL,
role_group_id bigint REFERENCES role_groups(id) ON DELETE CASCADE
);
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS disable_send_dm BOOLEAN NOT NULL DEFAULT false;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS remove_role_on_reaction_remove BOOLEAN NOT NULL DEFAULT false;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS fixed_amount BOOLEAN NOT NULL DEFAULT false;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS skip_amount INT NOT NULL DEFAULT 0;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS setup_msg_id BIGINT NOT NULL DEFAULT 0;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS standalone_mode SMALLINT;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS standalone_multiple_min INT;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS standalone_multiple_max INT;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS standalone_single_auto_toggle_off BOOLEAN;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS standalone_single_require_one BOOLEAN;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS standalone_blacklist_roles BIGINT[];
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS standalone_whitelist_roles BIGINT[];
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS saved_content TEXT;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS saved_embed TEXT;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS kind SMALLINT NOT NULL DEFAULT 0;
`, `
ALTER TABLE role_menus ADD COLUMN IF NOT EXISTS editing_option_id BIGINT;
`, `
CREATE INDEX IF NOT EXISTS role_menus_setup_msg_idx ON role_menus(setup_msg_id);
`, `
CREATE TABLE IF NOT EXISTS role_menu_options (
id bigserial NOT NULL PRIMARY KEY,
role_command_id bigint REFERENCES role_commands(id) ON DELETE CASCADE,
emoji_id bigint NOT NULL,
unicode_emoji text NOT NULL,
role_menu_id bigint NOT NULL REFERENCES role_menus(message_id) ON DELETE CASCADE
);
`, `
ALTER TABLE role_menu_options ADD COLUMN IF NOT EXISTS standalone_role_id BIGINT;
`, `
ALTER TABLE role_menu_options ADD COLUMN IF NOT EXISTS blacklist_roles BIGINT[];
`, `
ALTER TABLE role_menu_options ADD COLUMN IF NOT EXISTS whitelist_roles BIGINT[];
`, `
DO $$
BEGIN
BEGIN
ALTER TABLE role_menus ADD CONSTRAINT role_menus_editing_option_id_fkey FOREIGN KEY (editing_option_id) REFERENCES role_menu_options(id) ON DELETE SET NULL;
EXCEPTION
WHEN duplicate_object THEN RAISE NOTICE 'Table constraint role_menus.role_menus_editing_option_id_fkey already exists';
END;
END $$;
`, `
ALTER TABLE role_menu_options ADD COLUMN IF NOT EXISTS emoji_animated BOOLEAN NOT NULL DEFAULT false;
`, `
CREATE INDEX IF NOT EXISTS role_menu_options_role_command_idx ON role_menu_options(role_command_id);
`, `
CREATE INDEX IF NOT EXISTS role_menu_options_role_menu_id_idx ON role_menu_options(role_menu_id);
`, `
ALTER TABLE role_groups ADD COLUMN IF NOT EXISTS temporary_role_duration INT NOT NULL DEFAULT 0;
`}
var PageHTML string
Functions ¶
func CheckIgnoredRoles ¶
func CheckRequiredRoles ¶
func ClearRolemenuCache ¶
func ClearRolemenuCache(gID int64)
func CmdFuncListCommands ¶
func CmdFuncRole ¶
func ContinueRoleMenuSetup ¶
func FindRolemenuFull ¶
func FindToggleRole ¶
func GetRolemenuCached ¶
func HandleDeleteRoleCommands ¶
func HandleDeleteRoleCommands(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
func HandleGetGroup ¶
func HandleGetGroup(groupID int64, w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error)
func HandleGetIndex ¶
func HandleGetIndex(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error)
func HandleMoveCommand ¶
func HandleMoveCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
func HandleNewCommand ¶
func HandleNewCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
func HandleNewGroup ¶
func HandleNewGroup(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
func HandleRemoveCommand ¶
func HandleRemoveCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
func HandleRemoveGroup ¶
func HandleRemoveGroup(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
func HandleUpdateCommand ¶
func HandleUpdateCommand(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error)
func HandleUpdateGroup ¶
func HandleUpdateGroup(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error)
func HumanizeAssignError ¶
func IsRoleCommandError ¶
func MemberChooseOption ¶
func MenuReactedNotDone ¶
func NewCommonRoleError ¶
func NewCommonRoleError(msg string, r *CommonRoleSettings) error
func NewLmitError ¶
func NewRoleError ¶
func NewSimpleError ¶
func NextRoleMenuSetupStep ¶
func OptionName ¶
func OptionName(gs *dstate.GuildSet, opt *models.RoleMenuOption) string
func OptionsLessFunc ¶
func RegisterPlugin ¶
func RegisterPlugin()
func RoleCommandsLessFunc ¶
func RoleCommandsLessFunc(slice []*models.RoleCommand) func(int, int) bool
func StringCommands ¶
func StringCommands(cmds []*models.RoleCommand) string
StringCommands pretty formats a bunch of commands into a string
Types ¶
type CommonRoleError ¶
type CommonRoleError struct { Group *CommonRoleSettings Message string }
func (*CommonRoleError) Error ¶
func (r *CommonRoleError) Error() string
type CommonRoleSettings ¶
type CommonRoleSettings struct { // Either the menu or group is provided, we use the settings from one of them ParentMenu *models.RoleMenu ParentGroup *models.RoleGroup // these are provided depending on wether the parent is a menu in standalone mode or not RoleCmd *models.RoleCommand MenuOption *models.RoleMenuOption // convience fields taken from above for ease of use RoleId int64 ParentWhitelistRoles []int64 ParentBlacklistRoles []int64 ParentGroupMode int // White list and blacklist roles for this specific role WhitelistRoles []int64 BlacklistRoles []int64 }
CommonRoleSettings helps the bot logic by abstracting away the type of the role settings Right now it's only abstracting away whether its a menu connected to a rolegroup or a standalone menu
func CommonRoleFromRoleCommand ¶
func CommonRoleFromRoleCommand(group *models.RoleGroup, cmd *models.RoleCommand) *CommonRoleSettings
Assumes relationships are loaded, group is optional
func CommonRoleFromRoleMenuCommand ¶
func CommonRoleFromRoleMenuCommand(rm *models.RoleMenu, option *models.RoleMenuOption) *CommonRoleSettings
Assumes relationships are loaded for non standalone menus Falls back to CommonRoleFromRoleCommand if there is a rolegroup attached to the menu
func (*CommonRoleSettings) AllGroupRoles ¶
func (c *CommonRoleSettings) AllGroupRoles(ctx context.Context) []*CommonRoleSettings
func (*CommonRoleSettings) AssignRole ¶
func (c *CommonRoleSettings) AssignRole(ctx context.Context, ms *dstate.MemberState) (gaveRole bool, err error)
func (*CommonRoleSettings) CanRole ¶
func (c *CommonRoleSettings) CanRole(ctx context.Context, ms *dstate.MemberState) (can bool, err error)
func (*CommonRoleSettings) CheckToggleRole ¶
func (c *CommonRoleSettings) CheckToggleRole(ctx context.Context, ms *dstate.MemberState) (gaveRole bool, err error)
AssignRole attempts to assign the given role command, returns an error if the role does not exists or is unable to receie said role It also calls c.CanRole to check if we can assign it beforehand
func (*CommonRoleSettings) GroupToggleRole ¶
func (c *CommonRoleSettings) GroupToggleRole(ctx context.Context, ms *dstate.MemberState) (gaveRole bool, err error)
func (*CommonRoleSettings) MaybeScheduleRoleRemoval ¶
func (c *CommonRoleSettings) MaybeScheduleRoleRemoval(ctx context.Context, ms *dstate.MemberState) error
func (*CommonRoleSettings) ModeSettings ¶
func (c *CommonRoleSettings) ModeSettings() *ModeSettings
func (*CommonRoleSettings) ParentCanRole ¶
func (c *CommonRoleSettings) ParentCanRole(ctx context.Context, ms *dstate.MemberState) (can bool, err error)
func (*CommonRoleSettings) RemoveRole ¶
func (c *CommonRoleSettings) RemoveRole(ctx context.Context, ms *dstate.MemberState) (removedRole bool, err error)
func (*CommonRoleSettings) ToggleRole ¶
func (c *CommonRoleSettings) ToggleRole(ms *dstate.MemberState) (gaveRole bool, err error)
ToggleRole toggles the role of a guildmember, adding it if the member does not have the role and removing it if they do
type FormCommand ¶
type FormGroup ¶
type FormGroup struct { ID int64 Name string `valid:",1,100,trimspace"` RequireRoles []int64 `valid:"role,true"` IgnoreRoles []int64 `valid:"role,true"` Mode int MultipleMax int `valid:"0,250"` MultipleMin int `valid:"0,250"` SingleAutoToggleOff bool SingleRequireOne bool TemporaryRoleDuration int `valid:"0,1440"` }
type GroupError ¶
func (*GroupError) Error ¶
func (r *GroupError) Error() string
type ModeSettings ¶
type Plugin ¶
type Plugin struct{}
func (*Plugin) AddCommands ¶
func (p *Plugin) AddCommands()
func (*Plugin) LoadServerHomeWidget ¶
func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)
func (*Plugin) PluginInfo ¶
func (p *Plugin) PluginInfo() *common.PluginInfo
type RecentMenusTracker ¶
type RecentMenusTracker struct { RecentMenus []*RecentTrackedMenu Started time.Time EvictionAge time.Duration // If a guild created a menu from another source this gets set to that time GuildStartTimes map[int64]time.Time // contains filtered or unexported fields }
RecentMenusTracker is simply a way to reduce database queries We keep a small cache of message ids with menus on them created recently, that way we don't need to query the database on all messages with reactions on them if they're created within this tracked time interval
func NewRecentMenusTracker ¶
func NewRecentMenusTracker(evictionThreshold time.Duration) *RecentMenusTracker
func (*RecentMenusTracker) AddMenu ¶
func (r *RecentMenusTracker) AddMenu(msgID int64)
func (*RecentMenusTracker) CheckRecentTrackedMenu ¶
func (r *RecentMenusTracker) CheckRecentTrackedMenu(guildID int64, msgID int64) (outOfTimeRange bool, checkDB bool)
func (*RecentMenusTracker) GuildReset ¶
func (r *RecentMenusTracker) GuildReset(guildID int64)
func (*RecentMenusTracker) RunLoop ¶
func (r *RecentMenusTracker) RunLoop()
type RecentTrackedMenu ¶
type RecentTrackedMenu struct { MsgID int64 // contains filtered or unexported fields }
type SimpleError ¶
type SimpleError string
Just a simple type but distinguishable from errors.Error
func (SimpleError) Error ¶
func (s SimpleError) Error() string