Skip to content

Commit

Permalink
add proper analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed Mar 23, 2020
1 parent 527eaa4 commit 5976e1a
Show file tree
Hide file tree
Showing 31 changed files with 220 additions and 122 deletions.
3 changes: 3 additions & 0 deletions automod/automod_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/automod/models"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
Expand Down Expand Up @@ -387,6 +388,8 @@ func (p *Plugin) RulesetRulesTriggeredCondsPassed(ruleset *ParsedRuleset, trigge

loggedModels := make([]*models.AutomodTriggeredRule, len(triggeredRules))

go analytics.RecordActiveUnit(ruleset.RSModel.GuildID, p, "rule_triggered")

// apply the effects
for i, rule := range triggeredRules {
ctxData.CurrentRule = rule
Expand Down
10 changes: 9 additions & 1 deletion automod_legacy/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/commands"
Expand Down Expand Up @@ -111,12 +112,14 @@ func CheckMessage(m *discordgo.Message) bool {

rules := []Rule{config.Spam, config.Invite, config.Mention, config.Links, config.Words, config.Sites}

didCheck := false

// We gonna need to have this locked while we check
for _, r := range rules {
if r.ShouldIgnore(m, member) {
continue
}

didCheck = true
d, punishment, msg, err := r.Check(m, cs)
if d {
del = true
Expand All @@ -140,9 +143,14 @@ func CheckMessage(m *discordgo.Message) bool {
}

if !del {
if didCheck {
go analytics.RecordActiveUnit(cs.Guild.ID, &Plugin{}, "checked")
}
return false
}

go analytics.RecordActiveUnit(cs.Guild.ID, &Plugin{}, "rule_triggered")

if punishMsg != "" {
// Strip last newline
punishMsg = punishMsg[:len(punishMsg)-1]
Expand Down
8 changes: 7 additions & 1 deletion autorole/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/commands"
Expand All @@ -26,7 +27,7 @@ var _ bot.BotStopperHandler = (*Plugin)(nil)
var _ commands.CommandProvider = (*Plugin)(nil)

func (p *Plugin) AddCommands() {
commands.AddRootCommands(roleCommands...)
commands.AddRootCommands(p, roleCommands...)
}

type assignRoleEventdata struct {
Expand Down Expand Up @@ -330,6 +331,7 @@ func onMemberJoin(evt *eventsystem.EventData) (retry bool, err error) {

if config.RequiredDuration < 1 && config.CanAssignTo(addEvt.Roles, time.Now()) {
err = common.BotSession.GuildMemberRoleAdd(addEvt.GuildID, addEvt.User.ID, config.Role)
go analytics.RecordActiveUnit(addEvt.GuildID, &Plugin{}, "assigned_role")
return bot.CheckDiscordErrRetry(err), err
}

Expand Down Expand Up @@ -527,6 +529,8 @@ func handleAssignRole(evt *scheduledEventsModels.ScheduledEvent, data interface{
return false, nil
}

go analytics.RecordActiveUnit(evt.GuildID, &Plugin{}, "assigned_role")

err = common.BotSession.GuildMemberRoleAdd(evt.GuildID, dataCast.UserID, config.Role)
return bot.CheckDiscordErrRetry(err), err
}
Expand Down Expand Up @@ -563,6 +567,8 @@ func handleGuildMemberUpdate(evt *eventsystem.EventData) (retry bool, err error)
}
}

go analytics.RecordActiveUnit(update.GuildID, &Plugin{}, "assigned_role")

// if we branched here then all the checks passed and they should be assigned the role
err = common.BotSession.GuildMemberRoleAdd(update.GuildID, update.User.ID, config.Role)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion aylien/aylien.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func RegisterPlugin() {
var _ commands.CommandProvider = (*Plugin)(nil)

func (p *Plugin) AddCommands() {
commands.AddRootCommands(&commands.YAGCommand{
commands.AddRootCommands(p, &commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Cooldown: 5,
Name: "Sentiment",
Expand Down
2 changes: 2 additions & 0 deletions cmd/yagpdb/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/common/run"

// Core yagpdb packages
Expand Down Expand Up @@ -50,6 +51,7 @@ func main() {
paginatedmessages.RegisterPlugin()

// Setup plugins
analytics.RegisterPlugin()
safebrowsing.RegisterPlugin()
discordlogger.Register()
commands.RegisterPlugin()
Expand Down
7 changes: 5 additions & 2 deletions commands/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,16 @@ func FilterResp(in interface{}, guildID int64) interface{} {
return in
}

func AddRootCommands(cmds ...*YAGCommand) {
func AddRootCommands(p common.Plugin, cmds ...*YAGCommand) {
for _, v := range cmds {
v.Plugin = p
CommandSystem.Root.AddCommand(v, v.GetTrigger())
}
}
func AddRootCommandsWithMiddlewares(middlewares []dcmd.MiddleWareFunc, cmds ...*YAGCommand) {

func AddRootCommandsWithMiddlewares(p common.Plugin, middlewares []dcmd.MiddleWareFunc, cmds ...*YAGCommand) {
for _, v := range cmds {
v.Plugin = p
CommandSystem.Root.AddCommand(v, v.GetTrigger().SetMiddlewares(middlewares...))
}
}
Expand Down
7 changes: 7 additions & 0 deletions commands/yagcommmand.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/commands/models"
"github.com/jonas747/yagpdb/common"
Expand Down Expand Up @@ -114,6 +115,8 @@ type YAGCommand struct {
// It returns a reply and an error
// the reply can have a type of string, *MessageEmbed or error
RunFunc dcmd.RunFunc

Plugin common.Plugin
}

// CmdWithCategory puts the command in a category, mostly used for the help generation
Expand Down Expand Up @@ -196,6 +199,10 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) {
if err != nil {
logger.WithError(err).Error("Failed setting cooldown")
}

if yc.Plugin != nil {
go analytics.RecordActiveUnit(data.Msg.GuildID, yc.Plugin, "cmd_executed_"+strings.ToLower(yc.Name))
}
}

// set cmdErr to nil if this was a user error top stop it from being recorded and logged as an actual error
Expand Down
5 changes: 4 additions & 1 deletion customcommands/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"
"unicode/utf8"

"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/premium"

"emperror.dev/errors"
Expand Down Expand Up @@ -53,7 +54,7 @@ var _ bot.BotInitHandler = (*Plugin)(nil)
var _ commands.CommandProvider = (*Plugin)(nil)

func (p *Plugin) AddCommands() {
commands.AddRootCommands(cmdListCommands)
commands.AddRootCommands(p, cmdListCommands)
}

func (p *Plugin) BotInit() {
Expand Down Expand Up @@ -590,6 +591,8 @@ func ExecuteCustomCommand(cmd *models.CustomCommand, tmplCtx *templates.Context)

defer CCExecLock.Unlock(lockKey, lockHandle)

go analytics.RecordActiveUnit(cmd.GuildID, &Plugin{}, "executed_cc")

// pick a response and execute it
f.Info("Custom command triggered")

Expand Down
8 changes: 4 additions & 4 deletions logs/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ bot.BotInitHandler = (*Plugin)(nil)
var _ commands.CommandProvider = (*Plugin)(nil)

func (p *Plugin) AddCommands() {
commands.AddRootCommands(cmdLogs, cmdWhois, cmdNicknames, cmdUsernames, cmdMigrate)
commands.AddRootCommands(p, cmdLogs, cmdWhois, cmdNicknames, cmdUsernames, cmdMigrate)
}

func (p *Plugin) BotInit() {
Expand Down Expand Up @@ -86,7 +86,7 @@ var cmdWhois = &commands.YAGCommand{
if memberCPY != nil {
member = memberCPY
}

if parsed.Args[0].Value != nil {
member = parsed.Args[0].Value.(*dstate.MemberState)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ var cmdWhois = &commands.YAGCommand{
if createdDurStr == "" {
createdDurStr = "Less than an hour ago"
}

var memberStatus string
state := [4]string{"Playing", "Streaming", "Listening", "Watching"}
if !member.PresenceSet || member.PresenceGame == nil {
Expand All @@ -128,7 +128,7 @@ var cmdWhois = &commands.YAGCommand{
memberStatus = fmt.Sprintf("%s: %s", state[member.PresenceGame.Type], member.PresenceGame.Name)
}
}

embed := &discordgo.MessageEmbed{
Title: fmt.Sprintf("%s#%04d%s", member.Username, member.Discriminator, nick),
Fields: []*discordgo.MessageEmbedField{
Expand Down
3 changes: 3 additions & 0 deletions moderation/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/paginatedmessages"
"github.com/jonas747/yagpdb/commands"
Expand Down Expand Up @@ -89,6 +90,8 @@ func MBaseCmdSecond(cmdData *dcmd.Data, reason string, reasonArgOptional bool, n
permsMet = true
}

go analytics.RecordActiveUnit(cmdData.GS.ID, &Plugin{}, "executed_cmd_"+cmdName)

return oreason, nil
}

Expand Down
2 changes: 1 addition & 1 deletion moderation/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ bot.BotInitHandler = (*Plugin)(nil)
var _ bot.ShardMigrationReceiver = (*Plugin)(nil)

func (p *Plugin) AddCommands() {
commands.AddRootCommands(ModerationCommands...)
commands.AddRootCommands(p, ModerationCommands...)
}

func (p *Plugin) BotInit() {
Expand Down
9 changes: 9 additions & 0 deletions notifications/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"emperror.dev/errors"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
Expand Down Expand Up @@ -60,6 +61,8 @@ func HandleGuildMemberAdd(evtData *eventsystem.EventData) (retry bool, err error
Type: discordgo.ChannelTypeDM,
}

go analytics.RecordActiveUnit(gs.ID, &Plugin{}, "posted_join_server_msg")

if sendTemplate(thinCState, config.JoinDMMsg, ms, "join dm", false) {
return true, nil
}
Expand All @@ -72,6 +75,8 @@ func HandleGuildMemberAdd(evtData *eventsystem.EventData) (retry bool, err error
return
}

go analytics.RecordActiveUnit(gs.ID, &Plugin{}, "posted_join_server_dm")

chanMsg := config.JoinServerMsgs[rand.Intn(len(config.JoinServerMsgs))]
if sendTemplate(channel, chanMsg, ms, "join server msg", config.CensorInvites) {
return true, nil
Expand Down Expand Up @@ -107,6 +112,8 @@ func HandleGuildMemberRemove(evt *eventsystem.EventData) (retry bool, err error)

chanMsg := config.LeaveMsgs[rand.Intn(len(config.LeaveMsgs))]

go analytics.RecordActiveUnit(gs.ID, &Plugin{}, "posted_leave_server_msg")

if sendTemplate(channel, chanMsg, ms, "leave", config.CensorInvites) {
return true, nil
}
Expand Down Expand Up @@ -196,6 +203,8 @@ func HandleChannelUpdate(evt *eventsystem.EventData) (retry bool, err error) {
}
}

go analytics.RecordActiveUnit(cu.GuildID, &Plugin{}, "posted_topic_change")

go func() {
_, err := common.BotSession.ChannelMessageSend(topicChannel, fmt.Sprintf("Topic in channel <#%d> changed to **%s**", cu.ID, cu.Topic))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion premium/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (p *Plugin) BotInit() {
}

func (p *Plugin) AddCommands() {
commands.AddRootCommands(cmdGenerateCode)
commands.AddRootCommands(p, cmdGenerateCode)
}

const (
Expand Down
2 changes: 1 addition & 1 deletion reddit/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (p *Plugin) RemoveGuild(g int64) error {
}

func (p *Plugin) AddCommands() {
commands.AddRootCommands(&commands.YAGCommand{
commands.AddRootCommands(p, &commands.YAGCommand{
CmdCategory: commands.CategoryDebug,
HideFromCommandsPage: true,
Name: "testreddit",
Expand Down
4 changes: 4 additions & 0 deletions reddit/redditbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/jonas747/discordgo"
"github.com/jonas747/go-reddit"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
"github.com/jonas747/yagpdb/common/mqueue"
Expand Down Expand Up @@ -186,7 +187,10 @@ func (p *PostHandlerImpl) handlePost(post *reddit.Link, filterGuild int64) error
if common.Statsd != nil {
go common.Statsd.Count("yagpdb.reddit.matches", 1, []string{"subreddit:" + post.Subreddit, "guild:" + strconv.FormatInt(item.GuildID, 10)}, 1)
}

go analytics.RecordActiveUnit(item.GuildID, &Plugin{}, "posted_reddit_message")
}

return nil
}

Expand Down
9 changes: 5 additions & 4 deletions reminders/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package reminders

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/jinzhu/gorm"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
Expand All @@ -10,9 +14,6 @@ import (
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/scheduledevents2"
seventsmodels "github.com/jonas747/yagpdb/common/scheduledevents2/models"
"strconv"
"strings"
"time"
)

var logger = common.GetPluginLogger(&Plugin{})
Expand All @@ -21,7 +22,7 @@ var _ bot.BotInitHandler = (*Plugin)(nil)
var _ commands.CommandProvider = (*Plugin)(nil)

func (p *Plugin) AddCommands() {
commands.AddRootCommands(cmds...)
commands.AddRootCommands(p, cmds...)
}

func (p *Plugin) BotInit() {
Expand Down
5 changes: 4 additions & 1 deletion reputation/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/bot/paginatedmessages"

"github.com/jonas747/dcmd"
Expand All @@ -23,7 +24,7 @@ var _ bot.BotInitHandler = (*Plugin)(nil)
var _ commands.CommandProvider = (*Plugin)(nil)

func (p *Plugin) AddCommands() {
commands.AddRootCommands(cmds...)
commands.AddRootCommands(p, cmds...)
}

func (p *Plugin) BotInit() {
Expand Down Expand Up @@ -78,6 +79,8 @@ func handleMessageCreate(evt *eventsystem.EventData) {
return
}

go analytics.RecordActiveUnit(msg.GuildID, &Plugin{}, "auto_add_rep")

content := fmt.Sprintf("Gave +1 %s to **%s**", conf.PointsName, who.Mention())
common.BotSession.ChannelMessageSend(msg.ChannelID, content)
}
Expand Down
Loading

0 comments on commit 5976e1a

Please sign in to comment.