External Chat Linking (!4)
This PR adds the functionality for plugins to send and recieve messages linked to a specific channel. Co-authored-by: Layla <layla@layla.gg> Reviewed-on: https://gitea.sumulayla.synology.me/layla/birdbot/pulls/4
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/yeslayla/birdbot/common"
|
||||
"github.com/yeslayla/birdbot/persistence"
|
||||
)
|
||||
|
||||
type Discord struct {
|
||||
@ -21,12 +22,14 @@ type Discord struct {
|
||||
commands map[string]*discordgo.ApplicationCommand
|
||||
commandHandlers map[string]func(session *discordgo.Session, i *discordgo.InteractionCreate)
|
||||
|
||||
db persistence.Database
|
||||
|
||||
// Signal for shutdown
|
||||
stop chan os.Signal
|
||||
}
|
||||
|
||||
// New creates a new Discord session
|
||||
func New(applicationID string, guildID string, token string) *Discord {
|
||||
func New(applicationID string, guildID string, token string, db persistence.Database) *Discord {
|
||||
|
||||
// Create Discord Session
|
||||
session, err := discordgo.New(fmt.Sprint("Bot ", token))
|
||||
@ -35,6 +38,8 @@ func New(applicationID string, guildID string, token string) *Discord {
|
||||
}
|
||||
session.ShouldReconnectOnError = true
|
||||
return &Discord{
|
||||
db: db,
|
||||
|
||||
session: session,
|
||||
applicationID: applicationID,
|
||||
guildID: guildID,
|
||||
@ -117,6 +122,17 @@ func (discord *Discord) OnEventUpdate(handler func(*Discord, common.Event)) {
|
||||
})
|
||||
}
|
||||
|
||||
// OnMessageRecieved registers a handler when a message is recieved
|
||||
func (discord *Discord) OnMessageRecieved(handler func(*Discord, string, common.User, string)) {
|
||||
discord.session.AddHandler(func(s *discordgo.Session, r *discordgo.MessageCreate) {
|
||||
if r.GuildID != discord.guildID {
|
||||
return
|
||||
}
|
||||
|
||||
handler(discord, r.ChannelID, NewUser(r.Author), r.Content)
|
||||
})
|
||||
}
|
||||
|
||||
func (discord *Discord) SetStatus(status string) {
|
||||
if err := discord.session.UpdateGameStatus(0, status); err != nil {
|
||||
log.Fatal("Failed to update status: ", err)
|
||||
|
45
discord/message.go
Normal file
45
discord/message.go
Normal file
@ -0,0 +1,45 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/yeslayla/birdbot/core"
|
||||
"github.com/yeslayla/birdbot/persistence"
|
||||
)
|
||||
|
||||
func (discord *Discord) WebhookSendMessage(channel *core.Channel, displayName string, message string) {
|
||||
|
||||
webhookData, err := discord.db.GetDiscordWebhook(channel.ID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting webhook from DB: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if webhookData == nil {
|
||||
webhook, err := discord.session.WebhookCreate(channel.ID, "BirdBot", "")
|
||||
if err != nil {
|
||||
log.Printf("Error creating webhook: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
webhookData = &persistence.DBDiscordWebhook{
|
||||
ID: webhook.ID,
|
||||
Token: webhook.Token,
|
||||
}
|
||||
|
||||
if err := discord.db.SetDiscordWebhook(channel.ID, webhookData); err != nil {
|
||||
log.Fatalf("Error failed to store webhook in DB: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if _, err = discord.session.WebhookExecute(webhookData.ID, webhookData.Token, false, &discordgo.WebhookParams{
|
||||
Content: message,
|
||||
Username: displayName,
|
||||
}); err != nil {
|
||||
log.Printf("Failed to send message over webhook: %s", err)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user