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 was merged in pull request #4.
This commit is contained in:
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