Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. package chartplugin
  2.  
  3. import (
  4. "bytes"
  5. "fmt"
  6. "image/color"
  7. "log"
  8. "math/rand"
  9. "strings"
  10.  
  11. "github.com/ThyLeader/thyy"
  12. "github.com/gonum/plot"
  13. "github.com/gonum/plot/plotter"
  14. "github.com/gonum/plot/plotutil"
  15. "github.com/gonum/plot/vg"
  16. "github.com/iopred/discordgo"
  17. )
  18.  
  19. type chartPlugin struct {
  20. thyy.SimplePlugin
  21. }
  22.  
  23. var randomDirection = []string{
  24. "up",
  25. "down",
  26. "flat",
  27. }
  28.  
  29. var randomY = []string{
  30. "interest",
  31. "care",
  32. "success",
  33. "fail",
  34. "happiness",
  35. "sadness",
  36. "money",
  37. }
  38.  
  39. var randomX = []string{
  40. "time",
  41. "releases",
  42. "days",
  43. "years",
  44. }
  45.  
  46. func (p *chartPlugin) random(list []string) string {
  47. return list[rand.Intn(len(list))]
  48. }
  49.  
  50. func (p *chartPlugin) randomChart(service thyy.Service) string {
  51. ticks := ""
  52. if service.Name() == thyy.DiscordServiceName {
  53. ticks = "`"
  54. }
  55.  
  56. return fmt.Sprintf("%s%schart %s %s, %s%s", ticks, service.CommandPrefix(), p.random(randomDirection), p.random(randomY), p.random(randomX), ticks)
  57. }
  58.  
  59. func (p *chartPlugin) helpFunc(bot *thyy.Bot, service thyy.Service, message thyy.Message, detailed bool) []string {
  60. help := thyy.CommandHelp(service, "chart", "<up|down|flat> <vertical message>, <horizontal message>", "Creates a chart trending in the desired direction.")
  61.  
  62. if detailed {
  63. help = append(help, []string{
  64. "Examples:",
  65. thyy.CommandHelp(service, "chart", "down interest, time", "Creates a chart showing declining interest over time")[0],
  66. }...)
  67. }
  68.  
  69. return help
  70. }
  71.  
  72. func (p *chartPlugin) messageFunc(bot *thyy.Bot, service thyy.Service, message thyy.Message) {
  73. if service.IsMe(message) {
  74. return
  75. }
  76.  
  77. if !thyy.MatchesCommand(service, "chart", message) {
  78. return
  79. }
  80.  
  81. query, parts := thyy.ParseCommand(service, message)
  82. if len(parts) == 0 {
  83. service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart eg: %s", p.randomChart(service)))
  84. return
  85. }
  86.  
  87. start, end := 0.5, 0.5
  88.  
  89. switch parts[0] {
  90. case "up":
  91. start, end = 0, 1
  92. case "down":
  93. start, end = 1, 0
  94. case "flat":
  95. case "straight":
  96. default:
  97. service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart direction. eg: %s", p.randomChart(service)))
  98. return
  99. }
  100.  
  101. axes := strings.Split(query[len(parts[0]):], ",")
  102. if len(axes) != 2 {
  103. service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart axis labels eg: %s", p.randomChart(service)))
  104. return
  105. }
  106.  
  107. pl, err := plot.New()
  108. if err != nil {
  109. service.SendMessage(message.Channel(), fmt.Sprintf("Error making chart, sorry! eg: %s", p.randomChart(service)))
  110. return
  111. }
  112.  
  113. service.Typing(message.Channel())
  114.  
  115. pl.Title.Text = fmt.Sprintf("%s's chart", message.UserName())
  116. pl.Title.Color = color.White
  117. pl.Y.Label.Text = axes[0]
  118. pl.Y.Color = color.White
  119. pl.Y.Label.Color = color.White
  120. pl.X.Label.Text = axes[1]
  121. pl.X.Color = color.White
  122. pl.X.Label.Color = color.White
  123. pl.BackgroundColor = color.RGBA{54, 57, 62, 255}
  124.  
  125. num := 5 + rand.Intn(15)
  126.  
  127. start *= float64(num)
  128. end *= float64(num)
  129.  
  130. pts := make(plotter.XYs, num)
  131. for i := range pts {
  132. pts[i].X = float64(i) + rand.Float64()*0.5 - 0.2
  133. pts[i].Y = start + float64(end-start)/float64(num-1)*float64(i) + rand.Float64()*0.5 - 0.25
  134. }
  135.  
  136. pl.X.Tick.Label.Color = color.White
  137. pl.X.Tick.Color = color.White
  138. pl.Y.Tick.Label.Color = color.White
  139. pl.Y.Tick.Color = color.White
  140.  
  141. pl.X.Min = -0.5
  142. pl.X.Max = float64(num) + 0.5
  143.  
  144. pl.Y.Min = -0.5
  145. pl.Y.Max = float64(num) + 0.5
  146.  
  147. lpLine, lpPoints, err := plotter.NewLinePoints(pts)
  148. if err != nil {
  149. service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
  150. }
  151. lpLine.Color = plotutil.Color(rand.Int())
  152. lpLine.Width = vg.Points(1 + 0.5*rand.Float64())
  153. lpLine.Dashes = plotutil.Dashes(rand.Int())
  154. lpPoints.Shape = plotutil.Shape(rand.Int())
  155. lpPoints.Color = lpLine.Color
  156.  
  157. pl.Add(lpLine, lpPoints)
  158.  
  159. w, err := pl.WriterTo(320, 240, "png")
  160. if err != nil {
  161. service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
  162. return
  163. }
  164.  
  165. b := &bytes.Buffer{}
  166. w.WriteTo(b)
  167.  
  168. go func() {
  169. if service.Name() == thyy.DiscordServiceName {
  170. discord := service.(*thyy.Discord)
  171. p, err := discord.UserChannelPermissions(message.UserID(), message.Channel())
  172. if err == nil && p&discordgo.PermissionAttachFiles == discordgo.PermissionAttachFiles {
  173. service.SendFile(message.Channel(), "chart.png", b)
  174. return
  175. }
  176. }
  177.  
  178. url, err := bot.UploadToImgur(b, "chart.png")
  179. if err != nil {
  180. service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem uploading the chart to imgur.", message.UserName()))
  181. log.Println("Error uploading chart: ", err)
  182. return
  183. }
  184.  
  185. if service.Name() == thyy.DiscordServiceName {
  186. service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart <@%s>: %s", message.UserID(), url))
  187. } else {
  188. service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart %s: %s", message.UserName(), url))
  189. }
  190. }()
  191. }
  192.  
  193. // New will create a new chart plugin.
  194. func New() thyy.Plugin {
  195. p := &chartPlugin{
  196. SimplePlugin: *thyy.NewSimplePlugin("Chart"),
  197. }
  198. p.MessageFunc = p.messageFunc
  199. p.HelpFunc = p.helpFunc
  200. return p
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement