Permalink
Browse files

Support stats for plugins.

  • Loading branch information...
1 parent 608bdfa commit ebe35504632fe11c9651dc9245713bb9cf0179d0 @iopred committed Oct 29, 2016
View
@@ -14,7 +14,7 @@ import (
)
// VersionString is the current version of the bot
-const VersionString string = "0.7"
+const VersionString string = "0.8"
type serviceEntry struct {
Service
@@ -2,6 +2,7 @@ package comicplugin
import (
"bytes"
+ "encoding/json"
"fmt"
"image/png"
"log"
@@ -20,10 +21,25 @@ type comicPlugin struct {
sync.Mutex
bruxism.SimplePlugin
- log map[string][]bruxism.Message
+ log map[string][]bruxism.Message
+ Comics int
}
-func (p *comicPlugin) helpFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
+func (p *comicPlugin) Load(bot *bruxism.Bot, service bruxism.Service, data []byte) error {
+ if data != nil {
+ if err := json.Unmarshal(data, p); err != nil {
+ log.Println("Error loading data", err)
+ }
+ }
+
+ return nil
+}
+
+func (p *comicPlugin) Save() ([]byte, error) {
+ return json.Marshal(p)
+}
+
+func (p *comicPlugin) Help(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
help := bruxism.CommandHelp(service, "comic", "[1-10]", "Creates a comic from recent messages, or a number of messages if provided.")
ticks := ""
@@ -75,6 +91,7 @@ func makeScriptFromMessages(service bruxism.Service, message bruxism.Message, me
}
func (p *comicPlugin) makeComic(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, script *comicgen.Script) {
+ p.Comics++
comic := comicgen.NewComicGen("arial", service.Name() != bruxism.DiscordServiceName)
image, err := comic.MakeComic(script)
if err != nil {
@@ -113,7 +130,7 @@ func (p *comicPlugin) makeComic(bot *bruxism.Bot, service bruxism.Service, messa
}
}
-func (p *comicPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
+func (p *comicPlugin) Message(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
if service.IsMe(message) {
return
}
@@ -233,13 +250,18 @@ func (p *comicPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, mes
}
}
+func (p *comicPlugin) Name() string {
+ return "Comic"
+}
+
+// Stats will return the stats for a plugin.
+func (p *comicPlugin) Stats(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) []string {
+ return []string{fmt.Sprintf("Comics created: \t%d\n", p.Comics)}
+}
+
// New will create a new comic plugin.
func New() bruxism.Plugin {
- p := &comicPlugin{
- SimplePlugin: *bruxism.NewSimplePlugin("Comic"),
- log: make(map[string][]bruxism.Message),
+ return &comicPlugin{
+ log: make(map[string][]bruxism.Message),
}
- p.MessageFunc = p.messageFunc
- p.HelpFunc = p.helpFunc
- return p
}
View
@@ -153,6 +153,11 @@ func (p *CommandPlugin) AddCommand(commandString string, message CommandMessageF
}
}
+// Stats will return the stats for a plugin.
+func (p *CommandPlugin) Stats(bot *Bot, service Service, message Message) []string {
+ return nil
+}
+
// NewCommandPlugin will create a new command plugin.
func NewCommandPlugin() *CommandPlugin {
return &CommandPlugin{make(map[string]*command)}
View
@@ -47,7 +47,7 @@ func (p *helpPlugin) Help(bot *Bot, service Service, message Message, detailed b
sort.Strings(commands)
help := []string{}
-
+
if len(commands) > 0 {
help = append(help, CommandHelp(service, "help", "[topic]", fmt.Sprintf("Returns help for a specific topic. Available topics: %s%s%s", ticks, strings.Join(commands, ", "), ticks))[0])
}
@@ -154,6 +154,11 @@ func (p *helpPlugin) Save() ([]byte, error) {
return json.Marshal(p)
}
+// Stats will return the stats for a plugin.
+func (p *helpPlugin) Stats(bot *Bot, service Service, message Message) []string {
+ return nil
+}
+
// NeHelpPlugin will create a new help plugin.
func NewHelpPlugin() Plugin {
p := &helpPlugin{
View
@@ -69,11 +69,15 @@ type HelpFunc func(*Bot, Service, Message, bool) []string
// MessageFunc is the function signature for a message handler.
type MessageFunc func(*Bot, Service, Message)
+// StatsFunc is the function signature for a stats handler.
+type StatsFunc func(*Bot, Service, Message) []string
+
// Plugin is a plugin interface, supports loading and saving to a byte array and has help and message handlers.
type Plugin interface {
Name() string
Load(*Bot, Service, []byte) error
Save() ([]byte, error)
Help(*Bot, Service, Message, bool) []string
Message(*Bot, Service, Message)
+ Stats(*Bot, Service, Message) []string
}
@@ -175,6 +175,11 @@ func (p *livePlugin) Message(bot *bruxism.Bot, service bruxism.Service, message
}
}
+// Stats will return the stats for a plugin.
+func (p *livePlugin) Stats(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) []string {
+ return []string{fmt.Sprintf("YouTube channels monitored: \t%d\n", len(p.youTubeChannelToChannels))}
+}
+
// New will create a new live plugin.
func New(ytLiveChannel *bruxism.YTLiveChannel) bruxism.Plugin {
return &livePlugin{
@@ -758,3 +758,8 @@ func (p *MusicPlugin) play(vc *voiceConnection, close <-chan struct{}, control <
}
}
+
+// Stats will return the stats for a plugin.
+func (p *MusicPlugin) Stats(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) []string {
+ return nil
+}
@@ -226,6 +226,11 @@ func (p *playedPlugin) Name() string {
return "Played"
}
+// Stats will return the stats for a plugin.
+func (p *playedPlugin) Stats(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) []string {
+ return []string{fmt.Sprintf("Users playing: \t%d\n", len(p.Users))}
+}
+
// New will create a played plugin.
func New() bruxism.Plugin {
return &playedPlugin{
@@ -28,9 +28,9 @@ type Reminder struct {
// ReminderPlugin is a plugin that reminds users.
type ReminderPlugin struct {
sync.RWMutex
- bruxism.SimplePlugin
- bot *bruxism.Bot
- Reminders []*Reminder
+ bot *bruxism.Bot
+ Reminders []*Reminder
+ TotalReminders int
}
var randomTimes = []string{
@@ -63,7 +63,7 @@ func (p *ReminderPlugin) randomReminder(service bruxism.Service) string {
return fmt.Sprintf("%s%sreminder %s %s%s", ticks, service.CommandPrefix(), p.random(randomTimes), p.random(randomMessages), ticks)
}
-func (p *ReminderPlugin) helpFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
+func (p *ReminderPlugin) Help(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
help := []string{
bruxism.CommandHelp(service, "reminder", "<time> <reminder>", "Sets a reminder that is sent after the provided time.")[0],
}
@@ -146,11 +146,12 @@ func (p *ReminderPlugin) AddReminder(reminder *Reminder) error {
p.Reminders = append(p.Reminders, reminder)
copy(p.Reminders[i+1:], p.Reminders[i:])
p.Reminders[i] = reminder
+ p.TotalReminders++
return nil
}
-func (p *ReminderPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
+func (p *ReminderPlugin) Message(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
if !service.IsMe(message) {
if bruxism.MatchesCommand(service, "remind", message) || bruxism.MatchesCommand(service, "reminder", message) {
_, parts := bruxism.ParseCommand(service, message)
@@ -238,6 +239,10 @@ func (p *ReminderPlugin) Load(bot *bruxism.Bot, service bruxism.Service, data []
log.Println("Error loading data", err)
}
}
+ if len(p.Reminders) > p.TotalReminders {
+ p.TotalReminders = len(p.Reminders)
+ }
+
go p.Run(bot, service)
return nil
}
@@ -247,13 +252,18 @@ func (p *ReminderPlugin) Save() ([]byte, error) {
return json.Marshal(p)
}
+// Stats will return the stats for a plugin.
+func (p *ReminderPlugin) Stats(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) []string {
+ return []string{fmt.Sprintf("Reminders: \t%d\n", p.TotalReminders)}
+}
+
+func (p *ReminderPlugin) Name() string {
+ return "Reminder"
+}
+
// New will create a new Reminder plugin.
func New() bruxism.Plugin {
- p := &ReminderPlugin{
- SimplePlugin: *bruxism.NewSimplePlugin("Reminder"),
- Reminders: []*Reminder{},
+ return &ReminderPlugin{
+ Reminders: []*Reminder{},
}
- p.MessageFunc = p.messageFunc
- p.HelpFunc = p.helpFunc
- return p
}
View
@@ -7,6 +7,7 @@ type SimplePlugin struct {
SaveFunc SaveFunc `json:"-"`
MessageFunc MessageFunc `json:"-"`
HelpFunc HelpFunc `json:"-"`
+ StatsFunc StatsFunc `json:"-"`
}
// Name returns the name of the plugin.
@@ -46,6 +47,13 @@ func (p *SimplePlugin) Message(bot *Bot, service Service, message Message) {
}
}
+func (p *SimplePlugin) Stats(bot *Bot, service Service, message Message) []string {
+ if p.StatsFunc != nil {
+ return p.StatsFunc(bot, service, message)
+ }
+ return nil
+}
+
// NewSimplePlugin creates a new simple plugin.
func NewSimplePlugin(name string) *SimplePlugin {
return &SimplePlugin{name: name}
@@ -85,6 +85,11 @@ func (p *slowModePlugin) Message(bot *bruxism.Bot, service bruxism.Service, mess
}
}
+// Stats will return the stats for a plugin.
+func (p *slowModePlugin) Stats(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) []string {
+ return nil
+}
+
// New will create a new slow mode plugin.
func New() bruxism.Plugin {
return &slowModePlugin{
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"runtime"
+ "sort"
"strconv"
"strings"
"text/tabwriter"
@@ -66,13 +67,31 @@ func StatsCommand(bot *bruxism.Bot, service bruxism.Service, message bruxism.Mes
fmt.Fprintf(w, "Current shard: \t%d\n", (id>>22)%len(discord.Sessions))
}
}
- fmt.Fprintf(w, "\n```")
} else {
fmt.Fprintf(w, "Connected channels: \t%d\n", service.ChannelCount())
}
+
+ plugins := bot.Services[service.Name()].Plugins
+ names := []string{}
+ for _, plugin := range plugins {
+ names = append(names, plugin.Name())
+ sort.Strings(names)
+ }
+
+ for _, name := range names {
+ stats := plugins[name].Stats(bot, service, message)
+ for _, stat := range stats {
+ fmt.Fprint(w, stat)
+ }
+ }
+
+ if service.Name() == bruxism.DiscordServiceName {
+ fmt.Fprintf(w, "\n```")
+ }
+
w.Flush()
- out := buf.String() + "\nBuilt with love by iopred."
+ out := buf.String() + "\nSeptapus community: https://discord.gg/HWN9pwj\nBuilt with love by iopred."
if service.SupportsMultiline() {
service.SendMessage(message.Channel(), out)
@@ -323,6 +323,11 @@ func (p *triviaPlugin) Message(bot *bruxism.Bot, service bruxism.Service, messag
}
}
+// Stats will return the stats for a plugin.
+func (p *triviaPlugin) Stats(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) []string {
+ return nil
+}
+
// New will create a new slow mode plugin.
func New() bruxism.Plugin {
return &triviaPlugin{
@@ -273,6 +273,11 @@ func (p *wormholePlugin) send(bot *bruxism.Bot, service bruxism.Service, message
}
}
+// Stats will return the stats for a plugin.
+func (p *wormholePlugin) Stats(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) []string {
+ return []string{fmt.Sprintf("Wormholes open: \t%d\n", len(p.Channels))}
+}
+
// New will create a new wormhole plugin.
func New() bruxism.Plugin {
return &wormholePlugin{

0 comments on commit ebe3550

Please sign in to comment.