89 lines
3.2 KiB
Go
89 lines
3.2 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
// ConvocationConfig holds per-guild convocation configuration.
|
|
type ConvocationConfig struct {
|
|
ID int64
|
|
GuildID string
|
|
CategoryID string
|
|
LogChannelID string
|
|
ModalEnabled bool
|
|
UpdatedAt time.Time
|
|
PanelChannelID string
|
|
PanelMessageID string
|
|
PanelEmbedTitle string
|
|
PanelEmbedDescription string
|
|
PanelEmbedColor string
|
|
}
|
|
|
|
// ConvocationConfigRepo handles CRUD for convocation_config.
|
|
type ConvocationConfigRepo struct{ db *sql.DB }
|
|
|
|
// NewConvocationConfigRepo creates a ConvocationConfigRepo.
|
|
func NewConvocationConfigRepo(db *sql.DB) *ConvocationConfigRepo {
|
|
return &ConvocationConfigRepo{db: db}
|
|
}
|
|
|
|
// Get returns the convocation config for the given guild.
|
|
// Returns an empty (zero-value) config without error if none exists yet.
|
|
func (r *ConvocationConfigRepo) Get(ctx context.Context, guildID string) (*ConvocationConfig, error) {
|
|
row := r.db.QueryRowContext(ctx,
|
|
`SELECT id,guild_id,category_id,log_channel_id,modal_enabled,updated_at,
|
|
panel_channel_id,panel_message_id,panel_embed_title,panel_embed_description,panel_embed_color
|
|
FROM convocation_config WHERE guild_id=?`, guildID)
|
|
var c ConvocationConfig
|
|
var modalEnabled int
|
|
err := row.Scan(&c.ID, &c.GuildID, &c.CategoryID, &c.LogChannelID, &modalEnabled, &c.UpdatedAt,
|
|
&c.PanelChannelID, &c.PanelMessageID, &c.PanelEmbedTitle, &c.PanelEmbedDescription, &c.PanelEmbedColor)
|
|
if err == sql.ErrNoRows {
|
|
c.GuildID = guildID
|
|
c.ModalEnabled = true
|
|
c.PanelEmbedTitle = "Créer une convocation"
|
|
c.PanelEmbedColor = "#5865f2"
|
|
return &c, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.ModalEnabled = modalEnabled != 0
|
|
return &c, nil
|
|
}
|
|
|
|
// Upsert inserts or replaces the convocation config for a guild.
|
|
func (r *ConvocationConfigRepo) Upsert(ctx context.Context, c *ConvocationConfig) error {
|
|
c.UpdatedAt = time.Now().UTC()
|
|
modalInt := 0
|
|
if c.ModalEnabled {
|
|
modalInt = 1
|
|
}
|
|
_, err := r.db.ExecContext(ctx,
|
|
`INSERT INTO convocation_config(guild_id,category_id,log_channel_id,modal_enabled,updated_at,
|
|
panel_channel_id,panel_message_id,panel_embed_title,panel_embed_description,panel_embed_color)
|
|
VALUES(?,?,?,?,?,?,?,?,?,?)
|
|
ON CONFLICT(guild_id) DO UPDATE SET
|
|
category_id=excluded.category_id,
|
|
log_channel_id=excluded.log_channel_id,
|
|
modal_enabled=excluded.modal_enabled,
|
|
updated_at=excluded.updated_at,
|
|
panel_channel_id=excluded.panel_channel_id,
|
|
panel_embed_title=excluded.panel_embed_title,
|
|
panel_embed_description=excluded.panel_embed_description,
|
|
panel_embed_color=excluded.panel_embed_color`,
|
|
c.GuildID, c.CategoryID, c.LogChannelID, modalInt, c.UpdatedAt,
|
|
c.PanelChannelID, c.PanelMessageID, c.PanelEmbedTitle, c.PanelEmbedDescription, c.PanelEmbedColor)
|
|
return err
|
|
}
|
|
|
|
// SetConvocPanelMessage stores the Discord message ID for the convocation panel.
|
|
func (r *ConvocationConfigRepo) SetConvocPanelMessage(ctx context.Context, guildID, channelID, messageID string) error {
|
|
_, err := r.db.ExecContext(ctx,
|
|
`UPDATE convocation_config SET panel_channel_id=?, panel_message_id=?, updated_at=? WHERE guild_id=?`,
|
|
channelID, messageID, time.Now().UTC(), guildID)
|
|
return err
|
|
}
|