98 lines
2.0 KiB
Go
98 lines
2.0 KiB
Go
package discord
|
|
|
|
import (
|
|
dsmodel "dorisbot/models/discord"
|
|
pubmodel "dorisbot/models/public"
|
|
"dorisbot/pkg/cmdparser"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
// EvtInfo - event source info
|
|
type EvtInfo struct {
|
|
Server string
|
|
Channel string
|
|
User string
|
|
}
|
|
|
|
func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
|
|
// skip bot message
|
|
if evt.Author.ID == client.ID {
|
|
return
|
|
}
|
|
if len(evt.Content) == 0 {
|
|
return
|
|
}
|
|
guildID := evt.GuildID
|
|
channelID := evt.ChannelID
|
|
uid := evt.Author.ID
|
|
_ = channelID
|
|
_ = uid
|
|
|
|
server, err := dsmodel.GetDiscordServerByID(guildID)
|
|
if err != nil {
|
|
log.Println("get server err :: ", err)
|
|
return
|
|
}
|
|
|
|
cmd, keyword, payload := parseTextMessage(evt.Content)
|
|
|
|
_ = cmd
|
|
_ = payload
|
|
if !keyword && server == nil {
|
|
return
|
|
}
|
|
|
|
var cmdmodel *pubmodel.CommandModel
|
|
var acts []*cmdparser.CmdAction
|
|
|
|
// discord server not init
|
|
if server == nil {
|
|
cmdmodel, acts, err = getNotInitServerCommandAction(s, evt, cmd)
|
|
} else {
|
|
cmdmodel, acts, err = getRegServerCommandAction(s, evt, server, cmd)
|
|
}
|
|
|
|
if len(acts) == 0 {
|
|
if cmdmodel == nil {
|
|
log.Println("cmd is nil")
|
|
return
|
|
}
|
|
client.SendMessage(channelID, cmdmodel.Value)
|
|
return
|
|
}
|
|
|
|
info := EvtInfo{
|
|
Server: guildID,
|
|
Channel: channelID,
|
|
User: uid,
|
|
}
|
|
|
|
for _, it := range acts {
|
|
res := selectAction(info, it.Key, it.Value, payload)
|
|
cmdmodel.Value = strings.ReplaceAll(cmdmodel.Value, fmt.Sprintf("{{%s}}", it.Origin), res)
|
|
}
|
|
|
|
// TODO: switch case message response format
|
|
client.SendMessage(channelID, cmdmodel.Value)
|
|
}
|
|
|
|
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
|
|
}
|