Advertisement
lfed255-dev

Paste From Computer

Aug 22nd, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. package api
  2.  
  3. import (
  4. "log"
  5.  
  6. dsgo "github.com/bwmarrin/discordgo"
  7. // "reflect"
  8. // strs "strings"
  9. // "errors"
  10. )
  11.  
  12. type BotConfig struct {
  13. Prefix string `json:"prefix, omitempty"`
  14. Suffix string `json:"suffix, omitempty"`
  15. Token string `json:"token"`
  16. }
  17.  
  18.  
  19. type Bot struct {
  20. Conn *dsgo.Session
  21. User *dsgo.User
  22. Config BotConfig
  23. Controllers map[string]IController
  24. }
  25.  
  26. const (
  27. EventTypeDeleteMessage = iota
  28. EventTypeCreateMessage
  29. EventTypeGuildCreate
  30. EventTypeGuildDelete
  31. EventTypeGuildMemberAdd
  32. EventTypeGuildMemberRem
  33. )
  34.  
  35. const (
  36. EventClassMessage = iota
  37. EventClassGuild
  38. )
  39.  
  40. type Event struct {
  41. Class int
  42. Type int
  43. Value interface{}
  44. }
  45.  
  46. type EventHandler func(*Bot,*Event)
  47.  
  48. type IController interface {
  49. Init() error
  50. RegisterEventHandler(string,EventHandler) bool
  51. UnregisterEventHandler(string) bool
  52. GetHandlers() map[string]EventHandler
  53. Route(*Bot,*Event)
  54. DefApiFunc(string, []string, func(map[string]interface{}) []interface{})
  55. UndefApiFunc(string) bool
  56. DoApiFunc(string,...interface{}) []interface{} // for call controller functions
  57. }
  58.  
  59. type BasicController struct {
  60. ApiFuncs map[string]UnifuncInst
  61. Handlers map[string]EventHandler
  62. }
  63.  
  64. // type ApiFuncHelper struct {
  65. // args map[string]reflect.Type
  66. // finst interface{}
  67. // }
  68.  
  69. // func (this *ApiFuncHelper) Call(args ...reflect.Value) []reflect.Value {
  70. // if reflect.ValueOf(this.finst).Kind() != reflect.Func {
  71. // return nil
  72. // }
  73.  
  74. // _func := reflect.ValueOf(this.finst)
  75.  
  76. // ret := _func.Call([]reflect.Value {args...})
  77. // return ret
  78. // }
  79.  
  80.  
  81.  
  82.  
  83. // type ApiHelper struct {
  84. // props map[string]reflect.Value
  85. // funcs map[string]ApiFuncHelper
  86. // }
  87.  
  88.  
  89. func (this *Bot) GetController(label string) IController {
  90. return this.Controllers[label].(IController)
  91. }
  92.  
  93.  
  94. // func (this *MessageController) CallFunction(label string, args ...interface{}) {
  95. // switch label {
  96. // case "send":
  97. // this.SendFunc(args...)
  98. // }
  99. // }
  100.  
  101. /*
  102.  
  103. @param 0 - bot *Bot
  104. @param 1 - channelID string
  105. @param 2 - content string
  106. */
  107.  
  108. func (this *MessageController) SendFunc(args map[string]interface{}) []interface{} {
  109. (args["bot"].(*Bot)).Conn.ChannelMessageSend(args["chid"].(string),args["msg"].(string))
  110. return nil
  111. }
  112.  
  113. // func (this *MessageController) SendFunc(args ...interface{}) {
  114. // (args[0].(*Bot)).Conn.ChannelMessageSend((args[1].(string)),(args[2].(string)))
  115. // }
  116.  
  117. func (this *Bot) Router(conn *dsgo.Session, event interface{}) {
  118. switch event.(type) {
  119. case *dsgo.MessageCreate:
  120. log.Printf("MessageCreate Event")
  121. this.GetController("Message").Route(this,&Event{Class: EventClassMessage, Type: EventTypeCreateMessage, Value: (event.(*dsgo.MessageCreate)).Message})
  122. case *dsgo.MessageDelete:
  123. log.Printf("MessageDelete Event")
  124. this.GetController("Message").Route(this,&Event{Class: EventClassMessage, Type: EventTypeDeleteMessage, Value: (event.(*dsgo.MessageDelete)).Message})
  125. case *dsgo.GuildCreate:
  126. log.Printf("GuildCreate Event")
  127. this.GetController("Guild").Route(this,&Event{Class: EventClassGuild, Type: EventTypeGuildCreate, Value: (event.(*dsgo.GuildCreate)).Guild})
  128. case *dsgo.GuildMemberAdd:
  129. log.Printf("GuildMemberA Event")
  130. this.GetController("Guild").Route(this,&Event{Class: EventClassGuild, Type: EventTypeGuildMemberAdd, Value: (event.(*dsgo.GuildMemberAdd)).Member})
  131. case *dsgo.GuildMemberRemove:
  132. log.Printf("GuildMemberR Event")
  133. this.GetController("Guild").Route(this,&Event{Class: EventClassGuild, Type: EventTypeGuildMemberRem, Value: (event.(*dsgo.GuildMemberRemove)).Member})
  134.  
  135. }
  136. }
  137.  
  138.  
  139.  
  140. func (this *Bot) Init(autocmd bool) error {
  141. if this.Config.Token == "" {
  142. panic("empty bot token")
  143. }
  144. if this.Controllers == nil {
  145. this.Controllers = map[string]IController{
  146. "Message": &MessageController{
  147. bc : BasicController {
  148. Handlers: map[string]EventHandler{},
  149. ApiFuncs: map[string]UnifuncInst{},
  150. },
  151. },
  152. "Guild": &GuildController {
  153. bc : BasicController {
  154. Handlers: map[string]EventHandler{},
  155. ApiFuncs: map[string]UnifuncInst{},
  156. },
  157. },
  158. }
  159. }
  160.  
  161. conn,err := dsgo.New("Bot " + this.Config.Token)
  162.  
  163. if err != nil {
  164. return err
  165. }
  166.  
  167. this.Conn = conn
  168.  
  169. u, err := this.Conn.User("@me")
  170. if err != nil {
  171. return err
  172. }
  173.  
  174. this.User = u
  175.  
  176. this.Conn.AddHandler(this.Router)
  177. if autocmd {
  178. this.GetController("Message").RegisterEventHandler("Create",Utils_CommandHandler)
  179. }
  180.  
  181. for _,v := range this.Controllers {
  182. v.Init()
  183. }
  184.  
  185. return nil
  186.  
  187. }
  188.  
  189. func (this *Bot) Run() error {
  190. if this.Conn == nil {
  191. this.Init(false)
  192. }
  193.  
  194. err := this.Conn.Open()
  195.  
  196. return err
  197. }
  198.  
  199. func (this *Bot) Stop() error {
  200. err := this.Conn.Close()
  201.  
  202. return err
  203. }
  204.  
  205. // commands
  206.  
  207. /*
  208.  
  209. you can register command handler as message create event handler and use commands api without manual routing
  210.  
  211. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement