reputation

package
v1.25.2 Latest Latest
Warning

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

Go to latest
Published: May 19, 2021 License: MIT Imports: 30 Imported by: 0

README

Reputation

This YAGPDB plugin adds a reputation system.

Provides the +/giverep, rep and toprep commands.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingRequiredGiveRole    = UserError("You don't have any of the required roles to give points")
	ErrMissingRequiredReceiveRole = UserError("Target don't have any of the required roles to receive points")

	ErrBlacklistedGive    = UserError("Blacklisted from giving points")
	ErrBlacklistedReceive = UserError("Blacklisted from receiving points")
	ErrCooldown           = UserError("You're still on cooldown")
)
View Source
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS reputation_configs (
	guild_id        bigint PRIMARY KEY,
	points_name     varchar(50) NOT NULL,
	enabled         bool NOT NULL,	
	cooldown        int NOT NULL,
	max_give_amount bigint NOT NULL,

	required_give_role       varchar(30),
	required_receive_role    varchar(30),
	blacklisted_give_role    varchar(30),
	blacklisted_receive_role varchar(30),
	admin_role               varchar(30)
);
`, `
ALTER TABLE reputation_configs ADD COLUMN IF NOT EXISTS disable_thanks_detection BOOLEAN NOT NULL DEFAULT false;
`, `
DO $$
BEGIN

-- add the 'max_remove_amount' column, which was added way after when the reputation system was made
-- to preserve backwards compatibility the initial value is the same as max_give_amount, so that requires some special code

IF (SELECT COUNT(*) FROM information_schema.columns WHERE table_name='reputation_configs' and column_name='max_remove_amount') < 1 THEN
    ALTER TABLE reputation_configs ADD COLUMN max_remove_amount BIGINT NOT NULL DEFAULT 0;
	UPDATE reputation_configs SET max_remove_amount=max_give_amount;
END IF;

-- from after 196658c24fc23770d8664d468a3d6e5733669279
-- we have all the role restrictions as BIGINT[]
-- the below converts from the old system to the new one

IF (SELECT COUNT(*) FROM information_schema.columns WHERE table_name='reputation_configs' and column_name='required_give_roles') < 1 THEN
    -- req give roles
    ALTER TABLE reputation_configs ADD COLUMN admin_roles BIGINT[];
	UPDATE reputation_configs SET admin_roles=ARRAY[admin_role]::BIGINT[] WHERE admin_role IS NOT NULL AND admin_role != '';

    -- req give roles
    ALTER TABLE reputation_configs ADD COLUMN required_give_roles BIGINT[];
	UPDATE reputation_configs SET required_give_roles=ARRAY[required_give_role]::BIGINT[] WHERE required_give_role IS NOT NULL AND required_give_role != '';

	-- req rec roles
	ALTER TABLE reputation_configs ADD COLUMN required_receive_roles BIGINT[];
	UPDATE reputation_configs SET required_receive_roles=ARRAY[required_receive_role]::BIGINT[] WHERE required_receive_role IS NOT NULL AND required_receive_role != '';

	-- blacklisted give roles
	ALTER TABLE reputation_configs ADD COLUMN blacklisted_give_roles BIGINT[];
	UPDATE reputation_configs SET blacklisted_give_roles=ARRAY[blacklisted_give_role]::BIGINT[] WHERE blacklisted_give_role IS NOT NULL AND blacklisted_give_role != '';
	
	-- blacklisted rec roles
	ALTER TABLE reputation_configs ADD COLUMN blacklisted_receive_roles BIGINT[];
	UPDATE reputation_configs SET blacklisted_receive_roles=ARRAY[blacklisted_receive_role]::BIGINT[] WHERE blacklisted_receive_role IS NOT NULL AND blacklisted_receive_role != '';
	
END IF;
END $$;
`, `
CREATE TABLE IF NOT EXISTS reputation_users (
	user_id  bigint NOT NULL,
	guild_id bigint NOT NULL,

	created_at  TIMESTAMP WITH TIME ZONE NOT NULL,
	points 		bigint NOT NULL,

	PRIMARY KEY(guild_id, user_id)
);
`, `
CREATE TABLE IF NOT EXISTS reputation_log (
	id bigserial 	 PRIMARY KEY,
	created_at	     TIMESTAMP WITH TIME ZONE NOT NULL,

	guild_id 		 bigint NOT NULL,
	sender_id 		 bigint NOT NULL,
	receiver_id 	 bigint NOT NULL,
	set_fixed_amount bool NOT NULL,
	amount 			 bigint NOT NULL
);
`, `
ALTER TABLE reputation_log ADD COLUMN IF NOT EXISTS receiver_username TEXT NOT NULL DEFAULT '';
`, `
ALTER TABLE reputation_log ADD COLUMN IF NOT EXISTS sender_username TEXT NOT NULL DEFAULT '';
`, `
CREATE INDEX IF NOT EXISTS reputation_log_guild_idx ON reputation_log (guild_id);
`, `
CREATE INDEX IF NOT EXISTS reputation_log_sender_idx ON reputation_log (sender_id);
`, `
CREATE INDEX IF NOT EXISTS reputation_log_receiver_idx ON reputation_log (receiver_id);	
`}
View Source
var (
	ErrUserNotFound = errors.New("User not found")
)

Functions

func CanModifyRep

func CanModifyRep(conf *models.ReputationConfig, sender, receiver *dstate.MemberState) error

Returns a user error if the sender can not modify the rep of receiver Admins are always able to modify the rep of everyone

func CheckSetCooldown

func CheckSetCooldown(conf *models.ReputationConfig, senderID int64) (bool, error)

CheckSetCooldown checks and updates the reputation cooldown of a user, it returns true if the user was not on cooldown

func ClearCooldown

func ClearCooldown(guildID, senderID int64) error

func CmdGiveRep

func CmdGiveRep(parsed *dcmd.Data) (interface{}, error)

func DefaultConfig

func DefaultConfig(guildID int64) *models.ReputationConfig

func DelRep

func DelRep(ctx context.Context, gid int64, userID int64) error

func GetConfig

func GetConfig(ctx context.Context, guildID int64) (*models.ReputationConfig, error)

func GetUserStats

func GetUserStats(guildID, userID int64) (score int64, rank int, err error)

func HandleGetReputation

func HandleGetReputation(w http.ResponseWriter, r *http.Request) interface{}

func HandleLeaderboardJson

func HandleLeaderboardJson(w http.ResponseWriter, r *http.Request) interface{}

func HandleLogsJson

func HandleLogsJson(W http.ResponseWriter, r *http.Request) interface{}

func HandlePostReputation

func HandlePostReputation(w http.ResponseWriter, r *http.Request) (templateData web.TemplateData, err error)

func HandleResetReputation

func HandleResetReputation(w http.ResponseWriter, r *http.Request) (templateData web.TemplateData, err error)

func IsAdmin

func IsAdmin(gs *dstate.GuildState, member *dstate.MemberState, config *models.ReputationConfig) bool

func KeyCooldown

func KeyCooldown(guildID, userID int64) string

func ModifyRep

func ModifyRep(ctx context.Context, conf *models.ReputationConfig, guildID int64, sender, receiver *dstate.MemberState, amount int64) (err error)

func RegisterPlugin

func RegisterPlugin()

func SetRep

func SetRep(ctx context.Context, gid int64, senderID, userID int64, points int64) error

Types

type LeaderboardEntry

type LeaderboardEntry struct {
	*RankEntry
	Username string `json:"username"`
	Bot      bool   `json:"bot"`
	Avatar   string `json:"avatar"`
}

func DetailedLeaderboardEntries

func DetailedLeaderboardEntries(guildID int64, ranks []*RankEntry) ([]*LeaderboardEntry, error)

type Plugin

type Plugin struct{}

func (*Plugin) AddCommands

func (p *Plugin) AddCommands()

func (*Plugin) AllFeatureFlags

func (p *Plugin) AllFeatureFlags() []string

func (*Plugin) BotInit

func (p *Plugin) BotInit()

func (*Plugin) InitWeb

func (p *Plugin) InitWeb()

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

func (*Plugin) UpdateFeatureFlags

func (p *Plugin) UpdateFeatureFlags(guildID int64) ([]string, error)

type PostConfigForm

type PostConfigForm struct {
	Enabled                 bool
	EnableThanksDetection   bool
	PointsName              string `valid:",50"`
	Cooldown                int    `valid:"0,86401"` // One day
	MaxGiveAmount           int64
	MaxRemoveAmount         int64
	RequiredGiveRoles       []int64 `valid:"role,true"`
	RequiredReceiveRoles    []int64 `valid:"role,true"`
	BlacklistedGiveRoles    []int64 `valid:"role,true"`
	BlacklistedReceiveRoles []int64 `valid:"role,true"`
	AdminRoles              []int64 `valid:"role,true"`
}

func (PostConfigForm) RepConfig

func (p PostConfigForm) RepConfig() *models.ReputationConfig

type RankEntry

type RankEntry struct {
	Rank   int   `json:"rank"`
	UserID int64 `json:"user_id"`
	Points int64 `json:"points"`
}

func TopUsers

func TopUsers(guildID int64, offset, limit int) ([]*RankEntry, error)

type UserError

type UserError string

func (UserError) Error

func (b UserError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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