Permalink
Browse files

Simplify plugin flow. Limit some plugins from private messages.

  • Loading branch information...
1 parent 6f6749a commit 786aab49e14156384363e2672f17be5210a878c6 @iopred committed Jan 3, 2017
View
@@ -14,7 +14,7 @@ import (
)
// VersionString is the current version of the bot
-const VersionString string = "0.10"
+const VersionString string = "0.11"
type serviceEntry struct {
Service
@@ -74,109 +74,111 @@ func (p *chartPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, mes
return
}
- if bruxism.MatchesCommand(service, "chart", message) {
- query, parts := bruxism.ParseCommand(service, message)
- if len(parts) == 0 {
- service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart eg: %s", p.randomChart(service)))
- return
- }
+ if !bruxism.MatchesCommand(service, "chart", message) {
+ return
+ }
- start, end := 0.5, 0.5
-
- switch parts[0] {
- case "up":
- start, end = 0, 1
- case "down":
- start, end = 1, 0
- case "flat":
- case "straight":
- default:
- service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart direction. eg: %s", p.randomChart(service)))
- return
- }
+ query, parts := bruxism.ParseCommand(service, message)
+ if len(parts) == 0 {
+ service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart eg: %s", p.randomChart(service)))
+ return
+ }
- axes := strings.Split(query[len(parts[0]):], ",")
- if len(axes) != 2 {
- service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart axis labels eg: %s", p.randomChart(service)))
- return
- }
+ start, end := 0.5, 0.5
+
+ switch parts[0] {
+ case "up":
+ start, end = 0, 1
+ case "down":
+ start, end = 1, 0
+ case "flat":
+ case "straight":
+ default:
+ service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart direction. eg: %s", p.randomChart(service)))
+ return
+ }
- pl, err := plot.New()
- if err != nil {
- service.SendMessage(message.Channel(), fmt.Sprintf("Error making chart, sorry! eg: %s", p.randomChart(service)))
- return
- }
+ axes := strings.Split(query[len(parts[0]):], ",")
+ if len(axes) != 2 {
+ service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart axis labels eg: %s", p.randomChart(service)))
+ return
+ }
- service.Typing(message.Channel())
+ pl, err := plot.New()
+ if err != nil {
+ service.SendMessage(message.Channel(), fmt.Sprintf("Error making chart, sorry! eg: %s", p.randomChart(service)))
+ return
+ }
- pl.Y.Label.Text = axes[0]
- pl.X.Label.Text = axes[1]
+ service.Typing(message.Channel())
- num := 5 + rand.Intn(15)
+ pl.Y.Label.Text = axes[0]
+ pl.X.Label.Text = axes[1]
- start *= float64(num)
- end *= float64(num)
+ num := 5 + rand.Intn(15)
- pts := make(plotter.XYs, num)
- for i := range pts {
- pts[i].X = float64(i) + rand.Float64()*0.5 - 0.2
- pts[i].Y = start + float64(end-start)/float64(num-1)*float64(i) + rand.Float64()*0.5 - 0.25
- }
+ start *= float64(num)
+ end *= float64(num)
- pl.X.Tick.Label.Color = color.Transparent
- pl.Y.Tick.Label.Color = color.Transparent
+ pts := make(plotter.XYs, num)
+ for i := range pts {
+ pts[i].X = float64(i) + rand.Float64()*0.5 - 0.2
+ pts[i].Y = start + float64(end-start)/float64(num-1)*float64(i) + rand.Float64()*0.5 - 0.25
+ }
- pl.X.Min = -0.5
- pl.X.Max = float64(num) + 0.5
+ pl.X.Tick.Label.Color = color.Transparent
+ pl.Y.Tick.Label.Color = color.Transparent
- pl.Y.Min = -0.5
- pl.Y.Max = float64(num) + 0.5
+ pl.X.Min = -0.5
+ pl.X.Max = float64(num) + 0.5
- lpLine, lpPoints, err := plotter.NewLinePoints(pts)
- if err != nil {
- service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
- }
- lpLine.Color = plotutil.Color(rand.Int())
- lpLine.Width = vg.Points(1 + 0.5*rand.Float64())
- lpLine.Dashes = plotutil.Dashes(rand.Int())
- lpPoints.Shape = plotutil.Shape(rand.Int())
- lpPoints.Color = lpLine.Color
+ pl.Y.Min = -0.5
+ pl.Y.Max = float64(num) + 0.5
- pl.Add(lpLine, lpPoints)
+ lpLine, lpPoints, err := plotter.NewLinePoints(pts)
+ if err != nil {
+ service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
+ }
+ lpLine.Color = plotutil.Color(rand.Int())
+ lpLine.Width = vg.Points(1 + 0.5*rand.Float64())
+ lpLine.Dashes = plotutil.Dashes(rand.Int())
+ lpPoints.Shape = plotutil.Shape(rand.Int())
+ lpPoints.Color = lpLine.Color
- w, err := pl.WriterTo(320, 240, "png")
- if err != nil {
- service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
- return
- }
+ pl.Add(lpLine, lpPoints)
- b := &bytes.Buffer{}
- w.WriteTo(b)
-
- go func() {
- if service.Name() == bruxism.DiscordServiceName {
- discord := service.(*bruxism.Discord)
- p, err := discord.UserChannelPermissions(message.UserID(), message.Channel())
- if err == nil && p&discordgo.PermissionAttachFiles != 0 {
- service.SendFile(message.Channel(), "chart.png", b)
- return
- }
- }
+ w, err := pl.WriterTo(320, 240, "png")
+ if err != nil {
+ service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
+ return
+ }
- url, err := bot.UploadToImgur(b, "chart.png")
- if err != nil {
- service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem uploading the chart to imgur.", message.UserName()))
- log.Println("Error uploading chart: ", err)
+ b := &bytes.Buffer{}
+ w.WriteTo(b)
+
+ go func() {
+ if service.Name() == bruxism.DiscordServiceName {
+ discord := service.(*bruxism.Discord)
+ p, err := discord.UserChannelPermissions(message.UserID(), message.Channel())
+ if err == nil && p&discordgo.PermissionAttachFiles == discordgo.PermissionAttachFiles {
+ service.SendFile(message.Channel(), "chart.png", b)
return
}
+ }
- if service.Name() == bruxism.DiscordServiceName {
- service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart <@%s>: %s", message.UserID(), url))
- } else {
- service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart %s: %s", message.UserName(), url))
- }
- }()
- }
+ url, err := bot.UploadToImgur(b, "chart.png")
+ if err != nil {
+ service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem uploading the chart to imgur.", message.UserName()))
+ log.Println("Error uploading chart: ", err)
+ return
+ }
+
+ if service.Name() == bruxism.DiscordServiceName {
+ service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart <@%s>: %s", message.UserID(), url))
+ } else {
+ service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart %s: %s", message.UserName(), url))
+ }
+ }()
}
// New will create a new chart plugin.
View
@@ -371,7 +371,7 @@ func (d *Discord) IsChannelOwner(message Message) bool {
if err != nil {
return false
}
- return g.OwnerID == message.UserID()
+ return g.OwnerID == message.UserID() || d.IsBotOwner(message)
}
// IsModerator returns whether or not the sender of a message is a moderator.
@@ -18,26 +18,30 @@ func avatarLoadFunc(bot *bruxism.Bot, service bruxism.Service, data []byte) erro
}
func avatarMessageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
- if !service.IsMe(message) {
- if bruxism.MatchesCommand(service, "avatar", message) {
- query := strings.Join(strings.Split(message.RawMessage(), " ")[1:], " ")
+ if service.IsMe(message) {
+ return
+ }
- id := message.UserID()
- match := userIDRegex.FindStringSubmatch(query)
- if match != nil {
- id = match[1]
- }
+ if !bruxism.MatchesCommand(service, "avatar", message) {
+ return
+ }
- discord := service.(*bruxism.Discord)
+ query := strings.Join(strings.Split(message.RawMessage(), " ")[1:], " ")
- u, err := discord.Session.User(id)
- if err != nil {
- return
- }
+ id := message.UserID()
+ match := userIDRegex.FindStringSubmatch(query)
+ if match != nil {
+ id = match[1]
+ }
- service.SendMessage(message.Channel(), discordgo.EndpointUserAvatar(u.ID, u.Avatar))
- }
+ discord := service.(*bruxism.Discord)
+
+ u, err := discord.Session.User(id)
+ if err != nil {
+ return
}
+
+ service.SendMessage(message.Channel(), discordgo.EndpointUserAvatar(u.ID, u.Avatar))
}
func avatarHelpFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
@@ -39,39 +39,47 @@ func emojiLoadFunc(bot *bruxism.Bot, service bruxism.Service, data []byte) error
var discordRegex = regexp.MustCompile("<:.*?:(.*?)>")
func emojiMessageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
- if service.Name() == bruxism.DiscordServiceName && !service.IsMe(message) {
- if bruxism.MatchesCommand(service, "emoji", message) || bruxism.MatchesCommand(service, "hugemoji", message) {
- base := "emoji/twitter"
- if bruxism.MatchesCommand(service, "hugemoji", message) {
- base = "emoji/twitterhuge"
+ if service.Name() != bruxism.DiscordServiceName {
+ return
+ }
+
+ if service.IsMe(message) {
+ return
+ }
+
+ if !bruxism.MatchesCommand(service, "emoji", message) && !bruxism.MatchesCommand(service, "hugemoji", message) {
+ return
+ }
+
+ base := "emoji/twitter"
+ if bruxism.MatchesCommand(service, "hugemoji", message) {
+ base = "emoji/twitterhuge"
+ }
+ _, parts := bruxism.ParseCommand(service, message)
+ if len(parts) == 1 {
+ submatches := discordRegex.FindStringSubmatch(parts[0])
+ if len(submatches) != 0 {
+ h, err := http.Get("https://cdn.discordapp.com/emojis/" + submatches[1] + ".png")
+ if err != nil {
+ return
}
- _, parts := bruxism.ParseCommand(service, message)
- if len(parts) == 1 {
- submatches := discordRegex.FindStringSubmatch(parts[0])
- if len(submatches) != 0 {
- h, err := http.Get("https://cdn.discordapp.com/emojis/" + submatches[1] + ".png")
- if err != nil {
- return
- }
-
- service.SendFile(message.Channel(), "emoji.png", h.Body)
- h.Body.Close()
- return
+ service.SendFile(message.Channel(), "emoji.png", h.Body)
+ h.Body.Close()
- }
+ return
- s := strings.TrimSpace(parts[0])
- for i := range s {
- filename := emojiFile(base, s[i:])
- if filename != "" {
- if f, err := os.Open(fmt.Sprintf("%s/%s.png", base, filename)); err == nil {
- defer f.Close()
- service.SendFile(message.Channel(), "emoji.png", f)
-
- return
- }
- }
+ }
+
+ s := strings.TrimSpace(parts[0])
+ for i := range s {
+ filename := emojiFile(base, s[i:])
+ if filename != "" {
+ if f, err := os.Open(fmt.Sprintf("%s/%s.png", base, filename)); err == nil {
+ defer f.Close()
+ service.SendFile(message.Channel(), "emoji.png", f)
+
+ return
}
}
}
Oops, something went wrong.

0 comments on commit 786aab4

Please sign in to comment.