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:
2023-06-17 19:38:47 -04:00
parent b252d5e62e
commit 73a63fbf4d
12 changed files with 207 additions and 17 deletions

View File

@ -4,4 +4,12 @@ package persistence
type Database interface {
GetDiscordMessage(id string) (string, error)
SetDiscordMessage(id string, messageID string) error
GetDiscordWebhook(id string) (*DBDiscordWebhook, error)
SetDiscordWebhook(id string, data *DBDiscordWebhook) error
}
type DBDiscordWebhook struct {
ID string
Token string
}

View File

@ -0,0 +1,9 @@
-- +migrate Up
CREATE TABLE IF NOT EXISTS discord_webhooks (
id TEXT NOT NULL PRIMARY KEY,
webhook_id TEXT NOT NULL,
webhook_token TEXT NOT NULL
);
-- +migrate Down
DROP TABLE discord_webhooks;

View File

@ -120,3 +120,49 @@ func (db *Sqlite3Database) SetDiscordMessage(id string, messageID string) error
return nil
}
// GetDiscordWebhook finds a discord webhook based on a given local id
func (db *Sqlite3Database) GetDiscordWebhook(id string) (*DBDiscordWebhook, error) {
var data DBDiscordWebhook = DBDiscordWebhook{}
row := db.db.QueryRow("SELECT webhook_id, webhook_token FROM discord_webhooks WHERE id = $1", id)
if err := row.Scan(&data.ID, &data.Token); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, fmt.Errorf("failed to get discord webhook from sqlite3: %s", err)
}
return &data, nil
}
// SetDiscordWebhook stores a discord webhook based on a given local id
func (db *Sqlite3Database) SetDiscordWebhook(id string, data *DBDiscordWebhook) error {
statement, err := db.db.Prepare("INSERT OR IGNORE INTO discord_webhooks (id, webhook_id, webhook_token) VALUES (?, ?)")
if err != nil {
return err
}
result, err := statement.Exec(id, data.ID, data.Token)
if err != nil {
return err
}
n, _ := result.RowsAffected()
if n == 0 {
statement, err := db.db.Prepare("UPDATE discord_webhooks SET webhook_id = (?), webhook_token = (?) WHERE id = (?)")
if err != nil {
return err
}
if _, err := statement.Exec(data.ID, data.Token, id); err != nil {
return err
}
}
return nil
}