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.
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
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
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"velox-bot/internal/db/services/schedule"
|
||||
"velox-bot/internal/db/services/twitch"
|
||||
"velox-bot/internal/db/services/usersettings"
|
||||
"velox-bot/internal/db/services/welcome"
|
||||
)
|
||||
|
||||
type Services struct {
|
||||
@@ -20,11 +21,12 @@ type Services struct {
|
||||
Projects *projects.Service
|
||||
RPS *rps.Service
|
||||
Twitch *twitch.Service
|
||||
Welcome *welcome.Service
|
||||
}
|
||||
|
||||
var Global *Services
|
||||
|
||||
func NewServices(level *level.Service, levelSettings *levelsettings.Service, meeting *meeting.Service, schedule *schedule.Service, userSettings *usersettings.Service, projects *projects.Service, rps *rps.Service, twitchSvc *twitch.Service) *Services {
|
||||
func NewServices(level *level.Service, levelSettings *levelsettings.Service, meeting *meeting.Service, schedule *schedule.Service, userSettings *usersettings.Service, projects *projects.Service, rps *rps.Service, twitchSvc *twitch.Service, welcomeSvc *welcome.Service) *Services {
|
||||
s := &Services{
|
||||
Level: level,
|
||||
LevelSettings: levelSettings,
|
||||
@@ -34,6 +36,7 @@ func NewServices(level *level.Service, levelSettings *levelsettings.Service, mee
|
||||
Projects: projects,
|
||||
RPS: rps,
|
||||
Twitch: twitchSvc,
|
||||
Welcome: welcomeSvc,
|
||||
}
|
||||
Global = s
|
||||
return s
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package welcome
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"velox-bot/internal/db/repos/welcomerepo"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo *welcomerepo.Repo
|
||||
}
|
||||
|
||||
func New(repo *welcomerepo.Repo) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) GetSettings(ctx context.Context, guildID int64) (*welcomerepo.Settings, error) {
|
||||
return s.repo.GetSettings(ctx, guildID)
|
||||
}
|
||||
|
||||
func (s *Service) SetChannel(ctx context.Context, guildID, channelID int64) error {
|
||||
return s.repo.UpsertChannel(ctx, guildID, channelID)
|
||||
}
|
||||
|
||||
func (s *Service) SetMessage(ctx context.Context, guildID int64, message string) error {
|
||||
return s.repo.UpsertMessage(ctx, guildID, message)
|
||||
}
|
||||
|
||||
func (s *Service) SetDM(ctx context.Context, guildID int64, dm string) error {
|
||||
return s.repo.UpsertDM(ctx, guildID, dm)
|
||||
}
|
||||
|
||||
func (s *Service) SetGIF(ctx context.Context, guildID int64, gifURL string) error {
|
||||
return s.repo.UpsertGIF(ctx, guildID, gifURL)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user