dorisbot/pkg/svc/discord/events.go

98 lines
2.0 KiB
Go
Raw Normal View History

2019-06-28 09:37:37 +00:00
package discord
import (
2019-06-30 15:05:54 +00:00
dsmodel "dorisbot/models/discord"
2019-07-02 10:05:26 +00:00
pubmodel "dorisbot/models/public"
2019-07-04 09:00:16 +00:00
"dorisbot/pkg/cmdparser"
2019-07-09 15:05:36 +00:00
"fmt"
2019-06-28 09:37:37 +00:00
"log"
2019-07-09 15:05:36 +00:00
"strings"
2019-06-28 09:37:37 +00:00
"github.com/bwmarrin/discordgo"
)
2019-07-09 15:05:36 +00:00
// EvtInfo - event source info
type EvtInfo struct {
Server string
Channel string
User string
}
2019-06-28 09:37:37 +00:00
func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
2019-06-30 15:05:54 +00:00
// skip bot message
if evt.Author.ID == client.ID {
return
}
if len(evt.Content) == 0 {
return
}
2019-06-28 09:37:37 +00:00
guildID := evt.GuildID
channelID := evt.ChannelID
uid := evt.Author.ID
_ = channelID
_ = uid
2019-06-30 15:05:54 +00:00
server, err := dsmodel.GetDiscordServerByID(guildID)
2019-06-28 09:37:37 +00:00
if err != nil {
2019-06-30 15:05:54 +00:00
log.Println("get server err :: ", err)
2019-06-28 09:37:37 +00:00
return
}
2019-06-30 15:05:54 +00:00
cmd, keyword, payload := parseTextMessage(evt.Content)
_ = cmd
_ = payload
if !keyword && server == nil {
return
2019-06-28 09:37:37 +00:00
}
2019-07-02 09:02:31 +00:00
2019-07-04 09:00:16 +00:00
var cmdmodel *pubmodel.CommandModel
var acts []*cmdparser.CmdAction
2019-07-02 10:05:26 +00:00
// discord server not init
if server == nil {
2019-07-09 15:05:36 +00:00
cmdmodel, acts, err = getNotInitServerCommandAction(s, evt, cmd)
2019-07-02 10:05:26 +00:00
} else {
2019-07-09 15:05:36 +00:00
cmdmodel, acts, err = getRegServerCommandAction(s, evt, server, cmd)
}
if len(acts) == 0 {
if cmdmodel == nil {
log.Println("cmd is nil")
2019-07-02 10:05:26 +00:00
return
}
2019-07-09 15:05:36 +00:00
client.SendMessage(channelID, cmdmodel.Value)
return
}
2019-07-02 10:05:26 +00:00
2019-07-09 15:05:36 +00:00
info := EvtInfo{
Server: guildID,
Channel: channelID,
User: uid,
2019-07-02 10:05:26 +00:00
}
2019-07-09 15:05:36 +00:00
for _, it := range acts {
res := selectAction(info, it.Key, it.Value, payload)
cmdmodel.Value = strings.ReplaceAll(cmdmodel.Value, fmt.Sprintf("{{%s}}", it.Origin), res)
2019-07-04 09:00:16 +00:00
}
2019-07-09 15:05:36 +00:00
2019-07-13 04:56:11 +00:00
// TODO: switch case message response format
2019-07-09 15:05:36 +00:00
client.SendMessage(channelID, cmdmodel.Value)
2019-06-28 09:37:37 +00:00
}
func guildMemberAddEvt(s *discordgo.Session, evt *discordgo.GuildMemberAdd) {}
func guildMemberRemoveEvt(s *discordgo.Session, evt *discordgo.GuildMemberRemove) {}
func rateLimitEvt(s *discordgo.Session, evt *discordgo.RateLimit) {}
func connectEvt(s *discordgo.Session, evt *discordgo.Connect) {
log.Println(":: Connect ::")
client.connected = true
}
func disconnectEvt(s *discordgo.Session, evt *discordgo.Disconnect) {
log.Println(":: Disconnect ::")
client.connected = false
}