Files
velox-bot/internal/db/repos/welcomerepo/repo.go
T
FernandoJVideira e6aa5def96 feat: add welcome message functionality
- Introduced a new welcome service and repository for managing welcome messages, channels, and GIFs.
- Added commands to set welcome channel, message, DM, and GIF through the bot's configuration.
- Implemented event handling for new guild members to send welcome messages and DMs based on configuration.
- Updated main application to include the new welcome service in the bot's services.
2026-03-18 05:07:11 +00:00

112 lines
2.5 KiB
Go

package welcomerepo
import (
"context"
"database/sql"
)
type Repo struct {
db *sql.DB
}
type Settings struct {
GuildID int64
ChannelID int64
WelcomeMessage string
WelcomeDM string
WelcomeGIFURL string
HasConfiguration bool
}
func NewRepo(db *sql.DB) *Repo {
return &Repo{db: db}
}
func (r *Repo) GetSettings(ctx context.Context, guildID int64) (*Settings, error) {
const q = `
SELECT welcome_channel_id, welcome_message, welcome_dm, welcome_gif_url
FROM welcome
WHERE guild_id = $1
`
row := r.db.QueryRowContext(ctx, q, guildID)
var ch sql.NullInt64
var msg, dm, gif sql.NullString
switch err := row.Scan(&ch, &msg, &dm, &gif); err {
case nil:
s := &Settings{
GuildID: guildID,
ChannelID: 0,
WelcomeMessage: "",
WelcomeDM: "",
WelcomeGIFURL: "",
HasConfiguration: true,
}
if ch.Valid {
s.ChannelID = ch.Int64
}
if msg.Valid {
s.WelcomeMessage = msg.String
}
if dm.Valid {
s.WelcomeDM = dm.String
}
if gif.Valid {
s.WelcomeGIFURL = gif.String
}
return s, nil
case sql.ErrNoRows:
return &Settings{
GuildID: guildID,
HasConfiguration: false,
}, nil
default:
return nil, err
}
}
func (r *Repo) UpsertChannel(ctx context.Context, guildID, channelID int64) error {
const q = `
INSERT INTO welcome (guild_id, welcome_channel_id)
VALUES ($1, $2)
ON CONFLICT (guild_id)
DO UPDATE SET welcome_channel_id = EXCLUDED.welcome_channel_id
`
_, err := r.db.ExecContext(ctx, q, guildID, channelID)
return err
}
func (r *Repo) UpsertMessage(ctx context.Context, guildID int64, message string) error {
const q = `
INSERT INTO welcome (guild_id, welcome_message)
VALUES ($1, $2)
ON CONFLICT (guild_id)
DO UPDATE SET welcome_message = EXCLUDED.welcome_message
`
_, err := r.db.ExecContext(ctx, q, guildID, message)
return err
}
func (r *Repo) UpsertDM(ctx context.Context, guildID int64, dm string) error {
const q = `
INSERT INTO welcome (guild_id, welcome_dm)
VALUES ($1, $2)
ON CONFLICT (guild_id)
DO UPDATE SET welcome_dm = EXCLUDED.welcome_dm
`
_, err := r.db.ExecContext(ctx, q, guildID, dm)
return err
}
func (r *Repo) UpsertGIF(ctx context.Context, guildID int64, gifURL string) error {
const q = `
INSERT INTO welcome (guild_id, welcome_gif_url)
VALUES ($1, $2)
ON CONFLICT (guild_id)
DO UPDATE SET welcome_gif_url = EXCLUDED.welcome_gif_url
`
_, err := r.db.ExecContext(ctx, q, guildID, gifURL)
return err
}