Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package api
- import (
- "log"
- dsgo "github.com/bwmarrin/discordgo"
- // "reflect"
- // strs "strings"
- // "errors"
- )
- type BotConfig struct {
- Prefix string `json:"prefix, omitempty"`
- Suffix string `json:"suffix, omitempty"`
- Token string `json:"token"`
- }
- type Bot struct {
- Conn *dsgo.Session
- User *dsgo.User
- Config BotConfig
- Controllers map[string]IController
- }
- const (
- EventTypeDeleteMessage = iota
- EventTypeCreateMessage
- EventTypeGuildCreate
- EventTypeGuildDelete
- EventTypeGuildMemberAdd
- EventTypeGuildMemberRem
- )
- const (
- EventClassMessage = iota
- EventClassGuild
- )
- type Event struct {
- Class int
- Type int
- Value interface{}
- }
- type EventHandler func(*Bot,*Event)
- type IController interface {
- Init() error
- RegisterEventHandler(string,EventHandler) bool
- UnregisterEventHandler(string) bool
- GetHandlers() map[string]EventHandler
- Route(*Bot,*Event)
- DefApiFunc(string, []string, func(map[string]interface{}) []interface{})
- UndefApiFunc(string) bool
- DoApiFunc(string,...interface{}) []interface{} // for call controller functions
- }
- type BasicController struct {
- ApiFuncs map[string]UnifuncInst
- Handlers map[string]EventHandler
- }
- // type ApiFuncHelper struct {
- // args map[string]reflect.Type
- // finst interface{}
- // }
- // func (this *ApiFuncHelper) Call(args ...reflect.Value) []reflect.Value {
- // if reflect.ValueOf(this.finst).Kind() != reflect.Func {
- // return nil
- // }
- // _func := reflect.ValueOf(this.finst)
- // ret := _func.Call([]reflect.Value {args...})
- // return ret
- // }
- // type ApiHelper struct {
- // props map[string]reflect.Value
- // funcs map[string]ApiFuncHelper
- // }
- func (this *Bot) GetController(label string) IController {
- return this.Controllers[label].(IController)
- }
- // func (this *MessageController) CallFunction(label string, args ...interface{}) {
- // switch label {
- // case "send":
- // this.SendFunc(args...)
- // }
- // }
- /*
- @param 0 - bot *Bot
- @param 1 - channelID string
- @param 2 - content string
- */
- func (this *MessageController) SendFunc(args map[string]interface{}) []interface{} {
- (args["bot"].(*Bot)).Conn.ChannelMessageSend(args["chid"].(string),args["msg"].(string))
- return nil
- }
- // func (this *MessageController) SendFunc(args ...interface{}) {
- // (args[0].(*Bot)).Conn.ChannelMessageSend((args[1].(string)),(args[2].(string)))
- // }
- func (this *Bot) Router(conn *dsgo.Session, event interface{}) {
- switch event.(type) {
- case *dsgo.MessageCreate:
- log.Printf("MessageCreate Event")
- this.GetController("Message").Route(this,&Event{Class: EventClassMessage, Type: EventTypeCreateMessage, Value: (event.(*dsgo.MessageCreate)).Message})
- case *dsgo.MessageDelete:
- log.Printf("MessageDelete Event")
- this.GetController("Message").Route(this,&Event{Class: EventClassMessage, Type: EventTypeDeleteMessage, Value: (event.(*dsgo.MessageDelete)).Message})
- case *dsgo.GuildCreate:
- log.Printf("GuildCreate Event")
- this.GetController("Guild").Route(this,&Event{Class: EventClassGuild, Type: EventTypeGuildCreate, Value: (event.(*dsgo.GuildCreate)).Guild})
- case *dsgo.GuildMemberAdd:
- log.Printf("GuildMemberA Event")
- this.GetController("Guild").Route(this,&Event{Class: EventClassGuild, Type: EventTypeGuildMemberAdd, Value: (event.(*dsgo.GuildMemberAdd)).Member})
- case *dsgo.GuildMemberRemove:
- log.Printf("GuildMemberR Event")
- this.GetController("Guild").Route(this,&Event{Class: EventClassGuild, Type: EventTypeGuildMemberRem, Value: (event.(*dsgo.GuildMemberRemove)).Member})
- }
- }
- func (this *Bot) Init(autocmd bool) error {
- if this.Config.Token == "" {
- panic("empty bot token")
- }
- if this.Controllers == nil {
- this.Controllers = map[string]IController{
- "Message": &MessageController{
- bc : BasicController {
- Handlers: map[string]EventHandler{},
- ApiFuncs: map[string]UnifuncInst{},
- },
- },
- "Guild": &GuildController {
- bc : BasicController {
- Handlers: map[string]EventHandler{},
- ApiFuncs: map[string]UnifuncInst{},
- },
- },
- }
- }
- conn,err := dsgo.New("Bot " + this.Config.Token)
- if err != nil {
- return err
- }
- this.Conn = conn
- u, err := this.Conn.User("@me")
- if err != nil {
- return err
- }
- this.User = u
- this.Conn.AddHandler(this.Router)
- if autocmd {
- this.GetController("Message").RegisterEventHandler("Create",Utils_CommandHandler)
- }
- for _,v := range this.Controllers {
- v.Init()
- }
- return nil
- }
- func (this *Bot) Run() error {
- if this.Conn == nil {
- this.Init(false)
- }
- err := this.Conn.Open()
- return err
- }
- func (this *Bot) Stop() error {
- err := this.Conn.Close()
- return err
- }
- // commands
- /*
- you can register command handler as message create event handler and use commands api without manual routing
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement