Documentation ¶
Overview ¶
this package provides core functinality to yagpdb, important security stuff here
Index ¶
- Constants
- Variables
- func AddCPLogEntry(user *discordgo.User, guild int64, args ...interface{})
- func AddRole(member *discordgo.Member, role int64, guildID int64) error
- func AddRoleDS(ms *dstate.MemberState, role int64) error
- func BlockingLockRedisKey(key string, maxTryDuration time.Duration, maxLockDur int) error
- func ConnectDatadog()
- func ContainsInt64Slice(slice []int64, search int64) bool
- func ContainsIntSlice(slice []int, search int) bool
- func ContainsStringSlice(strs []string, search string) bool
- func ContainsStringSliceFold(strs []string, search string) bool
- func CutStringShort(s string, l int) string
- func DelayedMessageDelete(session *discordgo.Session, delay time.Duration, cID, mID int64)
- func DiscordError(err error) (code int, msg string)
- func ErrPQIsUniqueViolation(err error) bool
- func ErrWithCaller(err error) error
- func EscapeEveryoneHere(s string, escapeEveryone, escapeHere bool) string
- func EscapeSpecialMentions(in string) string
- func EscapeSpecialMentionsConditional(s string, allowEveryone, allowHere bool, allowRoles []int64) string
- func FallbackEmbed(embed *discordgo.MessageEmbed) string
- func GetCacheData(key string) (data []byte, err error)
- func GetCacheDataJson(key string, dest interface{}) error
- func GetGuild(guildID int64) (guild *discordgo.Guild, err error)
- func GetGuildChannels(guildID int64) (channels []*discordgo.Channel, err error)
- func GetJoinedServerCount() (int64, error)
- func GetRedisJson(key string, out interface{}) error
- func HumanizeDuration(precision DurationFormatPrecision, in time.Duration) string
- func HumanizePermissions(perms int64) (res []string)
- func HumanizeTime(precision DurationFormatPrecision, in time.Time) string
- func Init() error
- func InitTest()
- func IsDiscordErr(err error, codes ...int) bool
- func KeyGuild(guildID int64) string
- func KeyGuildChannels(guildID int64) string
- func LogIgnoreError(err error, msg string, data log.Fields)
- func MustParseInt(s string) int64
- func RandomAdjective() string
- func RegisterPlugin(plugin Plugin)
- func RegisterPluginL(pl Plugin)
- func RemoveRole(member *discordgo.Member, role int64, guildID int64) error
- func RemoveRoleDS(ms *dstate.MemberState, role int64) error
- func ReplaceServerInvites(msg string, guildID int64, replacement string) string
- func RetrySendMessage(channel int64, msg interface{}, maxTries int) error
- func RunBackgroundWorkers()
- func SendEmbedWithFallback(s *discordgo.Session, channelID int64, embed *discordgo.MessageEmbed) (*discordgo.Message, error)
- func SendTempMessage(session *discordgo.Session, duration time.Duration, cID int64, msg string)
- func SetCacheData(key string, expire int, data []byte) error
- func SetCacheDataJson(key string, expire int, data interface{}) error
- func SetCacheDataJsonSimple(key string, data interface{}) error
- func SetCacheDataSimple(key string, data []byte) error
- func SetRedisJson(key string, value interface{}) error
- func StopBackgroundWorkers(wg *sync.WaitGroup)
- func TryLockRedisKey(key string, maxDur int) (bool, error)
- func UnlockRedisKey(key string)
- func ValidateSQLSchema(input string)
- type BackgroundWorkerPlugin
- type BasePlugin
- type CPLogEntry
- type ContextHook
- type ContextKey
- type CoreConfig
- type DurationFormatPrecision
- type GORMLogger
- type LoggedExecutedCommand
- type LoggingTransport
- type Plugin
- type PluginWithLogging
- type STDLogProxy
- type SmallModel
- type WrappedGuild
Constants ¶
const ( VERSIONMAJOR = 1 VERSIONMINOR = 11 VERSIONPATCH = 2 )
Variables ¶
var ( ErrNotFound = errors.New("Not found") CacheKeyPrefix = "cache_" Cache *ccache.Cache )
var ( VERSIONNUMBER = fmt.Sprintf("%d.%d.%d", VERSIONMAJOR, VERSIONMINOR, VERSIONPATCH) VERSION = VERSIONNUMBER + " Life as a bot is hard" GORM *gorm.DB PQ *sql.DB RedisPool *radix.Pool BotSession *discordgo.Session BotUser *discordgo.User Conf *CoreConfig RedisPoolSize = 25 Statsd *statsd.Client Testing = os.Getenv("YAGPDB_TESTING") != "" CurrentRunCounter int64 )
var Adjectives = []string{}/* 1345 elements not displayed */
var DiscordInviteRegex = regexp.MustCompile(`(discord\.gg|discordapp\.com\/invite)(?:\/#)?\/([a-zA-Z0-9-]+)`)
var (
ErrMaxLockAttemptsExceeded = errors.New("Max lock attempts exceeded")
)
var LinkRegex = regexp.MustCompile(`((https?|steam):\/\/[^\s<]+[^<.,:;"')\]\s])`)
var (
Plugins []Plugin
)
var StringPerms = map[int]string{ discordgo.PermissionReadMessages: "Read Messages", discordgo.PermissionSendMessages: "Send Messages", discordgo.PermissionSendTTSMessages: "Send TTS Messages", discordgo.PermissionManageMessages: "Manage Messages", discordgo.PermissionEmbedLinks: "Embed Links", discordgo.PermissionAttachFiles: "Attach Files", discordgo.PermissionReadMessageHistory: "Read Message History", discordgo.PermissionMentionEveryone: "Mention Everyone", discordgo.PermissionVoiceConnect: "Voice Connect", discordgo.PermissionVoiceSpeak: "Voice Speak", discordgo.PermissionVoiceMuteMembers: "Voice Mute Members", discordgo.PermissionVoiceDeafenMembers: "Voice Deafen Members", discordgo.PermissionVoiceMoveMembers: "Voice Move Members", discordgo.PermissionVoiceUseVAD: "Voice Use VAD", discordgo.PermissionCreateInstantInvite: "Create Instant Invite", discordgo.PermissionKickMembers: "Kick Members", discordgo.PermissionBanMembers: "Ban Members", discordgo.PermissionManageRoles: "Manage Roles", discordgo.PermissionManageChannels: "Manage Channels", discordgo.PermissionManageServer: "Manage Server", }
var ThirdPartyDiscordInviteRegex = regexp.MustCompile(`(discord\.me|invite\.gg|discord\.io|disco\.gg|disboard\.org\/server)(?:\/#)?\/([a-zA-Z0-9-]+)`)
Functions ¶
func AddCPLogEntry ¶
func BlockingLockRedisKey ¶
BlockingLockRedisKey blocks until it suceeded to lock the key
func ConnectDatadog ¶ added in v1.4.1
func ConnectDatadog()
func ContainsInt64Slice ¶
func ContainsIntSlice ¶ added in v1.11.0
func ContainsStringSlice ¶
func ContainsStringSliceFold ¶
func CutStringShort ¶
CutStringShort stops a strinng at "l"-3 if it's longer than "l" and adds "..."
func DelayedMessageDelete ¶
DelayedMessageDelete Deletes a message after delay
func DiscordError ¶
DiscordError extracts the errorcode discord sent us
func ErrPQIsUniqueViolation ¶ added in v1.10.0
func ErrWithCaller ¶
func EscapeEveryoneHere ¶ added in v1.4.10
EscapeSpecialMentionsConditional Escapes an everyone mention, adding a zero width space between the '@' and rest
func EscapeSpecialMentions ¶
EscapeSpecialMentions Escapes an everyone mention, adding a zero width space between the '@' and rest
func EscapeSpecialMentionsConditional ¶
func EscapeSpecialMentionsConditional(s string, allowEveryone, allowHere bool, allowRoles []int64) string
EscapeSpecialMentionsConditional Escapes an everyone mention, adding a zero width space between the '@' and rest
func FallbackEmbed ¶
func FallbackEmbed(embed *discordgo.MessageEmbed) string
func GetCacheData ¶
Items in the cache expire after 1 min
func GetCacheDataJson ¶
func GetGuildChannels ¶
GetGuildChannels returns the guilds channels either from cache or api
func GetJoinedServerCount ¶ added in v1.11.2
func GetRedisJson ¶
GetRedisJson executes a get redis command and unmarshals the value into out
func HumanizeDuration ¶
func HumanizeDuration(precision DurationFormatPrecision, in time.Duration) string
func HumanizePermissions ¶ added in v1.6.0
func HumanizeTime ¶
func HumanizeTime(precision DurationFormatPrecision, in time.Time) string
func IsDiscordErr ¶
IsDiscordErr returns true if this was a discord error and one of the codes matches
func KeyGuildChannels ¶
func MustParseInt ¶
func RandomAdjective ¶
func RandomAdjective() string
func RegisterPlugin ¶
func RegisterPlugin(plugin Plugin)
RegisterPlugin registers a plugin, should be called when the bot is starting up
func RegisterPluginL ¶
func RegisterPluginL(pl Plugin)
RegisterPluginL registers a plugin, should be called when the bot is starting up
func RemoveRoleDS ¶ added in v1.4.1
func RemoveRoleDS(ms *dstate.MemberState, role int64) error
func ReplaceServerInvites ¶ added in v1.8.1
func RetrySendMessage ¶
func RunBackgroundWorkers ¶ added in v1.11.0
func RunBackgroundWorkers()
func SendEmbedWithFallback ¶
func SendTempMessage ¶
SendTempMessage sends a message that gets deleted after duration
func SetCacheData ¶
Stores an entry in the cache and sets it to expire after expire
func SetCacheDataJson ¶
Helper methods
func SetCacheDataJsonSimple ¶
func SetCacheDataSimple ¶
Stores an entry in the cache and sets it to expire after a minute
func SetRedisJson ¶
SetRedisJson marshals the value and runs a set redis command for key
func StopBackgroundWorkers ¶ added in v1.11.0
func TryLockRedisKey ¶
Locks the lock and if succeded sets it to expire after maxdur So that if someting went wrong its not locked forever
func UnlockRedisKey ¶
func UnlockRedisKey(key string)
func ValidateSQLSchema ¶
func ValidateSQLSchema(input string)
ValidateSQLSchema does some simple security checks on a sql schema file At the moment it only checks for drop table/index statements accidentally left in the schema file
Types ¶
type BackgroundWorkerPlugin ¶ added in v1.11.0
type BasePlugin ¶
func (*BasePlugin) Logger ¶
func (p *BasePlugin) Logger() *logrus.Entry
func (*BasePlugin) SetLogger ¶
func (p *BasePlugin) SetLogger(entry *logrus.Entry)
type CPLogEntry ¶
type CPLogEntry struct { Timestamp int64 `json:"ts"` Action string `json:"action"` TimestampString string `json:"-"` }
func GetCPLogEntries ¶
func GetCPLogEntries(guild int64) ([]*CPLogEntry, error)
type ContextHook ¶
type ContextHook struct{}
func (ContextHook) Levels ¶
func (hook ContextHook) Levels() []logrus.Level
type ContextKey ¶
type ContextKey int
const ( ContextKeyRedis ContextKey = iota ContextKeyDiscordSession ContextKeyTemplateData ContextKeyUser ContextKeyGuilds ContextKeyCurrentGuild ContextKeyCurrentUserGuild ContextKeyGuildChannels ContextKeyGuildRoles ContextKeyParsedForm ContextKeyFormOk ContextKeyBotMember ContextKeyBotPermissions ContextKeyHighestBotRole ContextKeyLogger ContextKeyIsPartial )
type CoreConfig ¶
type CoreConfig struct { Owner int64 BotID int64 ClientID string ClientSecret string BotToken string Host string Email string // The letsencrypt cert will use this email PQHost string PQUsername string PQPassword string Redis string DogStatsdAddress string }
func LoadConfig ¶
func LoadConfig() (c *CoreConfig, err error)
type DurationFormatPrecision ¶
type DurationFormatPrecision int
const ( DurationPrecisionSeconds DurationFormatPrecision = iota DurationPrecisionMinutes DurationPrecisionHours DurationPrecisionDays DurationPrecisionWeeks DurationPrecisionYears )
func (DurationFormatPrecision) FromSeconds ¶
func (d DurationFormatPrecision) FromSeconds(in int64) int64
func (DurationFormatPrecision) String ¶
func (d DurationFormatPrecision) String() string
type GORMLogger ¶ added in v1.4.7
type GORMLogger struct { }
func (*GORMLogger) Print ¶ added in v1.4.7
func (g *GORMLogger) Print(v ...interface{})
type LoggedExecutedCommand ¶
type LoggedExecutedCommand struct { SmallModel UserID string ChannelID string GuildID string // Name of command that was triggered Command string // Raw command with arguments passed RawCommand string // If command returned any error this will be no-empty Error string TimeStamp time.Time ResponseTime int64 }
func (LoggedExecutedCommand) TableName ¶
func (l LoggedExecutedCommand) TableName() string
type LoggingTransport ¶ added in v1.4.1
type LoggingTransport struct {
Inner http.RoundTripper
}
type Plugin ¶
type Plugin interface {
Name() string
}
Plugin represents a plugin, all plugins needs to implement this at a bare minimum
type PluginWithLogging ¶
type STDLogProxy ¶
type STDLogProxy struct{}
type SmallModel ¶
type WrappedGuild ¶
func GetWrapped ¶
func GetWrapped(in []*discordgo.UserGuild) ([]*WrappedGuild, error)
GetWrapped Returns a wrapped guild with connected set