mirror of https://github.com/velour/catbase.git
add discord integration
This commit is contained in:
parent
cdeae1b019
commit
dba38310e4
|
@ -0,0 +1,147 @@
|
||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/velour/catbase/bot/msg"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/velour/catbase/bot"
|
||||||
|
"github.com/velour/catbase/bot/user"
|
||||||
|
"github.com/velour/catbase/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Discord struct {
|
||||||
|
config *config.Config
|
||||||
|
client *discordgo.Session
|
||||||
|
|
||||||
|
event bot.Callback
|
||||||
|
|
||||||
|
emojiCache map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(config *config.Config) *Discord {
|
||||||
|
client, err := discordgo.New("Bot " + config.Get("DISCORDBOTTOKEN", ""))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("Could not connect to Discord")
|
||||||
|
}
|
||||||
|
d := &Discord{
|
||||||
|
config: config,
|
||||||
|
client: client,
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Discord) RegisterEvent(callback bot.Callback) {
|
||||||
|
d.event = callback
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Discord) Send(kind bot.Kind, args ...interface{}) (string, error) {
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case bot.Message:
|
||||||
|
st, err := d.client.ChannelMessageSend(args[0].(string), args[1].(string))
|
||||||
|
return st.ID, err
|
||||||
|
default:
|
||||||
|
log.Error().Msgf("discord.Send: unknown kind, %+v", kind)
|
||||||
|
return "", errors.New("unknown message type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Discord) GetEmojiList() map[string]string {
|
||||||
|
if d.emojiCache != nil {
|
||||||
|
return d.emojiCache
|
||||||
|
}
|
||||||
|
guidID := d.config.Get("discord.guildid", "")
|
||||||
|
if guidID == "" {
|
||||||
|
}
|
||||||
|
e, err := d.client.GuildEmojis(guidID)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("could not retrieve emojis")
|
||||||
|
return map[string]string{}
|
||||||
|
}
|
||||||
|
emojis := map[string]string{}
|
||||||
|
for _, e := range e {
|
||||||
|
emojis[e.ID] = e.Name
|
||||||
|
}
|
||||||
|
return emojis
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Discord) Who(id string) []string {
|
||||||
|
ch, err := d.client.Channel(id)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msgf("Error getting users")
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
users := []string{}
|
||||||
|
for _, u := range ch.Recipients {
|
||||||
|
users = append(users, u.Username)
|
||||||
|
}
|
||||||
|
return users
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Discord) Profile(id string) (user.User, error) {
|
||||||
|
u, err := d.client.User(id)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error getting user")
|
||||||
|
return user.User{}, err
|
||||||
|
}
|
||||||
|
return *convertUser(u), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertUser(u *discordgo.User) *user.User {
|
||||||
|
return &user.User{
|
||||||
|
ID: u.ID,
|
||||||
|
Name: u.Username,
|
||||||
|
Admin: false,
|
||||||
|
Icon: u.Avatar,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Discord) Serve() error {
|
||||||
|
log.Debug().Msg("starting discord serve function")
|
||||||
|
|
||||||
|
d.client.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuilds |
|
||||||
|
discordgo.IntentsGuildMessages)
|
||||||
|
|
||||||
|
err := d.client.Open()
|
||||||
|
if err != nil {
|
||||||
|
log.Debug().Err(err).Msg("error opening client")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Msg("discord connection open")
|
||||||
|
|
||||||
|
d.client.AddHandler(d.messageCreate)
|
||||||
|
|
||||||
|
sc := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
||||||
|
<-sc
|
||||||
|
|
||||||
|
log.Debug().Msg("closing discord connection")
|
||||||
|
|
||||||
|
// Cleanly close down the Discord session.
|
||||||
|
return d.client.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Discord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
log.Debug().Msgf("discord message: %+v", m)
|
||||||
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := msg.Message{
|
||||||
|
User: convertUser(m.Author),
|
||||||
|
Channel: m.ChannelID,
|
||||||
|
ChannelName: m.ChannelID,
|
||||||
|
Body: m.Content,
|
||||||
|
Time: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
d.event(d, bot.Message, msg)
|
||||||
|
}
|
|
@ -40,7 +40,6 @@ const defaultLogFormat = "[{{fixDate .Time \"2006-01-02 15:04:05\"}}] {{if .Topi
|
||||||
// "User":{"Admin":false,"ID":"U0RLUDELD","Name":"flyngpngn"}}
|
// "User":{"Admin":false,"ID":"U0RLUDELD","Name":"flyngpngn"}}
|
||||||
|
|
||||||
type SlackApp struct {
|
type SlackApp struct {
|
||||||
bot bot.Bot
|
|
||||||
config *config.Config
|
config *config.Config
|
||||||
api *slack.Client
|
api *slack.Client
|
||||||
|
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -14,6 +14,7 @@ require (
|
||||||
github.com/antchfx/xpath v1.1.1 // indirect
|
github.com/antchfx/xpath v1.1.1 // indirect
|
||||||
github.com/armon/go-radix v1.0.0 // indirect
|
github.com/armon/go-radix v1.0.0 // indirect
|
||||||
github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330 // indirect
|
github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330 // indirect
|
||||||
|
github.com/bwmarrin/discordgo v0.22.0
|
||||||
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598
|
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598
|
||||||
github.com/chrissexton/gofuck v1.0.0
|
github.com/chrissexton/gofuck v1.0.0
|
||||||
github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff
|
github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff
|
||||||
|
|
5
go.sum
5
go.sum
|
@ -27,6 +27,8 @@ github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
|
||||||
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||||
github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330 h1:ekDALXAVvY/Ub1UtNta3inKQwZ/jMB/zpOtD8rAYh78=
|
github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330 h1:ekDALXAVvY/Ub1UtNta3inKQwZ/jMB/zpOtD8rAYh78=
|
||||||
github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330/go.mod h1:nH+k0SvAt3HeiYyOlJpLLv1HG1p7KWP7qU9QPp2/pCo=
|
github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330/go.mod h1:nH+k0SvAt3HeiYyOlJpLLv1HG1p7KWP7qU9QPp2/pCo=
|
||||||
|
github.com/bwmarrin/discordgo v0.22.0 h1:uBxY1HmlVCsW1IuaPjpCGT6A2DBwRn0nvOguQIxDdFM=
|
||||||
|
github.com/bwmarrin/discordgo v0.22.0/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
|
||||||
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 h1:j2XRGH5Y5uWtBYXGwmrjKeM/kfu/jh7ZcnrGvyN5Ttk=
|
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 h1:j2XRGH5Y5uWtBYXGwmrjKeM/kfu/jh7ZcnrGvyN5Ttk=
|
||||||
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598/go.mod h1:sduMkaHcXDIWurl/Bd/z0rNEUHw5tr6LUA9IO8E9o0o=
|
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598/go.mod h1:sduMkaHcXDIWurl/Bd/z0rNEUHw5tr6LUA9IO8E9o0o=
|
||||||
github.com/chrissexton/gofuck v1.0.0 h1:vxg/tIfI2HunJOErSotmMqMRNfLRVO+BTjSKpFoAizA=
|
github.com/chrissexton/gofuck v1.0.0 h1:vxg/tIfI2HunJOErSotmMqMRNfLRVO+BTjSKpFoAizA=
|
||||||
|
@ -70,6 +72,7 @@ github.com/gonum/internal v0.0.0-20181124074243-f884aa714029 h1:8jtTdc+Nfj9AR+0s
|
||||||
github.com/gonum/internal v0.0.0-20181124074243-f884aa714029/go.mod h1:Pu4dmpkhSyOzRwuXkOgAvijx4o+4YMUJJo9OvPYMkks=
|
github.com/gonum/internal v0.0.0-20181124074243-f884aa714029/go.mod h1:Pu4dmpkhSyOzRwuXkOgAvijx4o+4YMUJJo9OvPYMkks=
|
||||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/inconshreveable/log15 v0.0.0-20200109203555-b30bc20e4fd1 h1:KUDFlmBg2buRWNzIcwLlKvfcnujcHQRQ1As1LoaCLAM=
|
github.com/inconshreveable/log15 v0.0.0-20200109203555-b30bc20e4fd1 h1:KUDFlmBg2buRWNzIcwLlKvfcnujcHQRQ1As1LoaCLAM=
|
||||||
|
@ -145,7 +148,9 @@ github.com/velour/chat v0.0.0-20180713122344-fd1d1606cb89/go.mod h1:ejwOYCjnDMyO
|
||||||
github.com/velour/velour v0.0.0-20160303155839-8e090e68d158 h1:p3rTUXxzuKsBOsHlkly7+rj9wagFBKeIsCDKkDII9sw=
|
github.com/velour/velour v0.0.0-20160303155839-8e090e68d158 h1:p3rTUXxzuKsBOsHlkly7+rj9wagFBKeIsCDKkDII9sw=
|
||||||
github.com/velour/velour v0.0.0-20160303155839-8e090e68d158/go.mod h1:Ojy3BTOiOTwpHpw7/HNi+TVTFppao191PQs+Qc3sqpE=
|
github.com/velour/velour v0.0.0-20160303155839-8e090e68d158/go.mod h1:Ojy3BTOiOTwpHpw7/HNi+TVTFppao191PQs+Qc3sqpE=
|
||||||
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
|
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
|
||||||
|
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk=
|
||||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
|
|
4
main.go
4
main.go
|
@ -9,6 +9,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/velour/catbase/connectors/discord"
|
||||||
|
|
||||||
"github.com/velour/catbase/plugins/gpt2"
|
"github.com/velour/catbase/plugins/gpt2"
|
||||||
|
|
||||||
"github.com/velour/catbase/plugins/achievements"
|
"github.com/velour/catbase/plugins/achievements"
|
||||||
|
@ -107,6 +109,8 @@ func main() {
|
||||||
client = slack.New(c)
|
client = slack.New(c)
|
||||||
case "slackapp":
|
case "slackapp":
|
||||||
client = slackapp.New(c)
|
client = slackapp.New(c)
|
||||||
|
case "discord":
|
||||||
|
client = discord.New(c)
|
||||||
default:
|
default:
|
||||||
log.Fatal().Msgf("Unknown connection type: %s", c.Get("type", "UNSET"))
|
log.Fatal().Msgf("Unknown connection type: %s", c.Get("type", "UNSET"))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue