Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord Channel ID Updates #563

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/ban/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Start(ctx context.Context, banUsecase domain.BanSteamUsecase, banNetUsecase
name = person.SteamID.String()
}

discordUsecase.SendPayload(domain.ChannelModLog, discord.BanExpiresMessage(ban, person, configUsecase.ExtURL(ban)))
discordUsecase.SendPayload(domain.ChannelBanLog, discord.BanExpiresMessage(ban, person, configUsecase.ExtURL(ban)))

slog.Info("Ban expired",
slog.String("reason", ban.Reason.String()),
Expand Down
2 changes: 1 addition & 1 deletion internal/ban/ban_asn_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s banASN) Unban(ctx context.Context, asnNum string) (bool, error) {
return false, errors.Join(errDrop, domain.ErrDropASNBan)
}

s.discord.SendPayload(domain.ChannelModLog, discord.UnbanASNMessage(asNum))
s.discord.SendPayload(domain.ChannelBanLog, discord.UnbanASNMessage(asNum))

return true, nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/ban/ban_steam_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ func (s banSteamUsecase) Ban(ctx context.Context, curUser domain.PersonInfo, ban
slog.Int64("sid64", banSteam.TargetID.Int64()))
}

s.discord.SendPayload(domain.ChannelModLog, discord.KickPlayerEmbed(target))
s.discord.SendPayload(domain.ChannelBanLog, discord.KickPlayerEmbed(target))
} else if banSteam.BanType == domain.NoComm {
if errSilence := s.state.Silence(ctx, banSteam.TargetID, banSteam.Reason); errSilence != nil && !errors.Is(errSilence, domain.ErrPlayerNotFound) {
slog.Error("Failed to silence player", log.ErrAttr(errSilence),
slog.Int64("sid64", banSteam.TargetID.Int64()))
}

s.discord.SendPayload(domain.ChannelModLog, discord.SilenceEmbed(target))
s.discord.SendPayload(domain.ChannelBanLog, discord.SilenceEmbed(target))
}

return nil
Expand Down Expand Up @@ -186,7 +186,7 @@ func (s banSteamUsecase) Unban(ctx context.Context, targetSID steamid.SteamID, r
return false, errors.Join(err, domain.ErrFetchPerson)
}

s.discord.SendPayload(domain.ChannelModLog, discord.UnbanMessage(s.config, person))
s.discord.SendPayload(domain.ChannelBanLog, discord.UnbanMessage(s.config, person))

return true, nil
}
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func refreshFiltersCmd() *cobra.Command {
stateUsecase := state.NewStateUsecase(eventBroadcaster,
state.NewStateRepository(state.NewCollector(serversUsecase)), configUsecase, serversUsecase)

wordFilterUsecase := wordfilter.NewWordFilterUsecase(wordfilter.NewWordFilterRepository(dbUsecase))
discordRepository, _ := discord.NewDiscordRepository(conf)

discordUsecase := discord.NewDiscordUsecase(discordRepository, configUsecase)

wordFilterUsecase := wordfilter.NewWordFilterUsecase(wordfilter.NewWordFilterRepository(dbUsecase), discordUsecase)
if errImport := wordFilterUsecase.Import(ctx); errImport != nil {
slog.Error("Failed to load filters")
}

discordRepository, _ := discord.NewDiscordRepository(conf)

discordUsecase := discord.NewDiscordUsecase(discordRepository, wordFilterUsecase)

personUsecase := person.NewPersonUsecase(person.NewPersonRepository(conf, dbUsecase), configUsecase)
reportUsecase := report.NewReportUsecase(report.NewReportRepository(dbUsecase), discordUsecase, configUsecase, personUsecase, nil)
// banGroupUsecase := steamgroup.NewBanGroupUsecase(steamgroup.NewSteamGroupRepository(dbUsecase), personUsecase)
Expand Down
91 changes: 44 additions & 47 deletions internal/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"syscall"
"time"

"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
"github.com/leighmacdonald/gbans/internal/app"
"github.com/leighmacdonald/gbans/internal/appeal"
Expand Down Expand Up @@ -69,20 +68,20 @@ func serveCmd() *cobra.Command { //nolint:maintidx
panic(fmt.Sprintf("Failed to read static config: %v", errStatic))
}

dbUsecase := database.New(staticConfig.DatabaseDSN, staticConfig.DatabaseAutoMigrate, staticConfig.DatabaseLogQueries)
if errConnect := dbUsecase.Connect(ctx); errConnect != nil {
dbConn := database.New(staticConfig.DatabaseDSN, staticConfig.DatabaseAutoMigrate, staticConfig.DatabaseLogQueries)
if errConnect := dbConn.Connect(ctx); errConnect != nil {
slog.Error("Cannot initialize database", log.ErrAttr(errConnect))

return
}

defer func() {
if errClose := dbUsecase.Close(); errClose != nil {
if errClose := dbConn.Close(); errClose != nil {
slog.Error("Failed to close database cleanly")
}
}()

configUsecase := config.NewConfigUsecase(staticConfig, config.NewConfigRepository(dbUsecase))
configUsecase := config.NewConfigUsecase(staticConfig, config.NewConfigRepository(dbConn))
if err := configUsecase.Init(ctx); err != nil {
panic(fmt.Sprintf("Failed to init config: %v", err))
}
Expand All @@ -93,20 +92,18 @@ func serveCmd() *cobra.Command { //nolint:maintidx

conf := configUsecase.Config()

var sentryClient *sentry.Client
var errSentry error

sentryClient, errSentry = log.NewSentryClient(conf.Sentry.SentryDSN, conf.Sentry.SentryTrace, conf.Sentry.SentrySampleRate, app.BuildVersion)
if conf.Sentry.SentryDSN != "" {
sentryClient, errSentry := log.NewSentryClient(conf.Sentry.SentryDSN, conf.Sentry.SentryTrace, conf.Sentry.SentrySampleRate, app.BuildVersion)
if errSentry != nil {
slog.Error("Failed to setup sentry client")
} else {
defer sentryClient.Flush(2 * time.Second)
}
}

logCloser := log.MustCreateLogger(conf.Log.File, conf.Log.Level)
defer logCloser()

if errSentry != nil {
slog.Error("Failed to setup sentry client")
} else {
defer sentryClient.Flush(2 * time.Second)
}

eventBroadcaster := fp.NewBroadcaster[logparse.EventType, logparse.ServerEvent]()
weaponsMap := fp.NewMutexMap[logparse.Weapon, int]()

Expand All @@ -117,48 +114,48 @@ func serveCmd() *cobra.Command { //nolint:maintidx
return
}

wordFilterUsecase := wordfilter.NewWordFilterUsecase(wordfilter.NewWordFilterRepository(dbUsecase))
if err := wordFilterUsecase.Import(ctx); err != nil {
slog.Error("Failed to load word filters", log.ErrAttr(err))
discordUsecase := discord.NewDiscordUsecase(discordRepository, configUsecase)

if err := discordUsecase.Start(); err != nil {
slog.Error("Failed to start discord", log.ErrAttr(err))

return
}

discordUsecase := discord.NewDiscordUsecase(discordRepository, wordFilterUsecase)

if err := discordUsecase.Start(); err != nil {
slog.Error("Failed to start discord", log.ErrAttr(err))
wordFilterUsecase := wordfilter.NewWordFilterUsecase(wordfilter.NewWordFilterRepository(dbConn), discordUsecase)
if err := wordFilterUsecase.Import(ctx); err != nil {
slog.Error("Failed to load word filters", log.ErrAttr(err))

return
}

defer discordUsecase.Shutdown(conf.Discord.GuildID)

personUsecase := person.NewPersonUsecase(person.NewPersonRepository(conf, dbUsecase), configUsecase)
personUsecase := person.NewPersonUsecase(person.NewPersonRepository(conf, dbConn), configUsecase)

networkUsecase := network.NewNetworkUsecase(eventBroadcaster, network.NewNetworkRepository(dbUsecase), personUsecase, configUsecase)
networkUsecase := network.NewNetworkUsecase(eventBroadcaster, network.NewNetworkRepository(dbConn), personUsecase, configUsecase)
go networkUsecase.Start(ctx)

assetRepository := asset.NewLocalRepository(dbUsecase, configUsecase)
assetRepository := asset.NewLocalRepository(dbConn, configUsecase)
if errInitAssets := assetRepository.Init(ctx); errInitAssets != nil {
slog.Error("Failed to init local asset repo", log.ErrAttr(errInitAssets))

return
}

assetUsecase := asset.NewAssetUsecase(assetRepository)
serversUsecase := servers.NewServersUsecase(servers.NewServersRepository(dbUsecase))
demoUsecase := demo.NewDemoUsecase(domain.BucketDemo, demo.NewDemoRepository(dbUsecase), assetUsecase, configUsecase, serversUsecase)
serversUsecase := servers.NewServersUsecase(servers.NewServersRepository(dbConn))
demoUsecase := demo.NewDemoUsecase(domain.BucketDemo, demo.NewDemoRepository(dbConn), assetUsecase, configUsecase, serversUsecase)
go demoUsecase.Start(ctx)
reportUsecase := report.NewReportUsecase(report.NewReportRepository(dbUsecase), discordUsecase, configUsecase, personUsecase, demoUsecase)
reportUsecase := report.NewReportUsecase(report.NewReportRepository(dbConn), discordUsecase, configUsecase, personUsecase, demoUsecase)

stateUsecase := state.NewStateUsecase(eventBroadcaster,
state.NewStateRepository(state.NewCollector(serversUsecase)), configUsecase, serversUsecase)
banUsecase := ban.NewBanSteamUsecase(ban.NewBanSteamRepository(dbUsecase, personUsecase, networkUsecase), personUsecase, configUsecase, discordUsecase, reportUsecase, stateUsecase)
banUsecase := ban.NewBanSteamUsecase(ban.NewBanSteamRepository(dbConn, personUsecase, networkUsecase), personUsecase, configUsecase, discordUsecase, reportUsecase, stateUsecase)

banGroupUsecase := steamgroup.NewBanGroupUsecase(steamgroup.NewSteamGroupRepository(dbUsecase), personUsecase, discordUsecase, configUsecase)
banGroupUsecase := steamgroup.NewBanGroupUsecase(steamgroup.NewSteamGroupRepository(dbConn), personUsecase, discordUsecase, configUsecase)

blocklistUsecase := blocklist.NewBlocklistUsecase(blocklist.NewBlocklistRepository(dbUsecase), banUsecase, banGroupUsecase)
blocklistUsecase := blocklist.NewBlocklistUsecase(blocklist.NewBlocklistRepository(dbConn), banUsecase, banGroupUsecase)
go blocklistUsecase.Start(ctx)

go func() {
Expand All @@ -167,48 +164,48 @@ func serveCmd() *cobra.Command { //nolint:maintidx
}
}()

banASNUsecase := ban.NewBanASNUsecase(ban.NewBanASNRepository(dbUsecase), discordUsecase, networkUsecase, configUsecase, personUsecase)
banNetUsecase := ban.NewBanNetUsecase(ban.NewBanNetRepository(dbUsecase), personUsecase, configUsecase, discordUsecase, stateUsecase)
banASNUsecase := ban.NewBanASNUsecase(ban.NewBanASNRepository(dbConn), discordUsecase, networkUsecase, configUsecase, personUsecase)
banNetUsecase := ban.NewBanNetUsecase(ban.NewBanNetRepository(dbConn), personUsecase, configUsecase, discordUsecase, stateUsecase)

discordOAuthUsecase := discord.NewDiscordOAuthUsecase(discord.NewDiscordOAuthRepository(dbUsecase), configUsecase)
discordOAuthUsecase := discord.NewDiscordOAuthUsecase(discord.NewDiscordOAuthRepository(dbConn), configUsecase)
go discordOAuthUsecase.Start(ctx)

apu := appeal.NewAppealUsecase(appeal.NewAppealRepository(dbUsecase), banUsecase, personUsecase, discordUsecase, configUsecase)
apu := appeal.NewAppealUsecase(appeal.NewAppealRepository(dbConn), banUsecase, personUsecase, discordUsecase, configUsecase)

matchRepo := match.NewMatchRepository(eventBroadcaster, dbUsecase, personUsecase, serversUsecase, discordUsecase, stateUsecase, weaponsMap)
matchRepo := match.NewMatchRepository(eventBroadcaster, dbConn, personUsecase, serversUsecase, discordUsecase, stateUsecase, weaponsMap)
go matchRepo.Start(ctx)

matchUsecase := match.NewMatchUsecase(matchRepo, stateUsecase, serversUsecase, discordUsecase)

chatRepository := chat.NewChatRepository(dbUsecase, personUsecase, wordFilterUsecase, matchUsecase, eventBroadcaster)
chatRepository := chat.NewChatRepository(dbConn, personUsecase, wordFilterUsecase, matchUsecase, eventBroadcaster)
go chatRepository.Start(ctx)

chatUsecase := chat.NewChatUsecase(configUsecase, chatRepository, wordFilterUsecase, stateUsecase, banUsecase, personUsecase, discordUsecase)
go chatUsecase.Start(ctx)

forumUsecase := forum.NewForumUsecase(forum.NewForumRepository(dbUsecase), discordUsecase)
forumUsecase := forum.NewForumUsecase(forum.NewForumRepository(dbConn), discordUsecase)

metricsUsecase := metrics.NewMetricsUsecase(eventBroadcaster)
go metricsUsecase.Start(ctx)

go forumUsecase.Start(ctx)

newsUsecase := news.NewNewsUsecase(news.NewNewsRepository(dbUsecase))
notificationUsecase := notification.NewNotificationUsecase(notification.NewNotificationRepository(dbUsecase), personUsecase)
patreonUsecase := patreon.NewPatreonUsecase(patreon.NewPatreonRepository(dbUsecase), configUsecase)
newsUsecase := news.NewNewsUsecase(news.NewNewsRepository(dbConn))
notificationUsecase := notification.NewNotificationUsecase(notification.NewNotificationRepository(dbConn), personUsecase)
patreonUsecase := patreon.NewPatreonUsecase(patreon.NewPatreonRepository(dbConn), configUsecase)
go patreonUsecase.Start(ctx)

srcdsUsecase := srcds.NewSrcdsUsecase(srcds.NewRepository(dbUsecase), configUsecase, serversUsecase, personUsecase, reportUsecase, discordUsecase, banUsecase)
srcdsUsecase := srcds.NewSrcdsUsecase(srcds.NewRepository(dbConn), configUsecase, serversUsecase, personUsecase, reportUsecase, discordUsecase, banUsecase)

wikiUsecase := wiki.NewWikiUsecase(wiki.NewWikiRepository(dbUsecase))
wikiUsecase := wiki.NewWikiUsecase(wiki.NewWikiRepository(dbConn))

authUsecase := auth.NewAuthUsecase(auth.NewAuthRepository(dbUsecase), configUsecase, personUsecase, banUsecase, serversUsecase)
authUsecase := auth.NewAuthUsecase(auth.NewAuthRepository(dbConn), configUsecase, personUsecase, banUsecase, serversUsecase)
go authUsecase.Start(ctx)

voteUsecase := votes.NewVoteUsecase(votes.NewVoteRepository(dbUsecase), personUsecase, matchUsecase, discordUsecase, eventBroadcaster)
voteUsecase := votes.NewVoteUsecase(votes.NewVoteRepository(dbConn), personUsecase, matchUsecase, discordUsecase, configUsecase, eventBroadcaster)
go voteUsecase.Start(ctx)

contestUsecase := contest.NewContestUsecase(contest.NewContestRepository(dbUsecase))
contestUsecase := contest.NewContestUsecase(contest.NewContestRepository(dbConn))

// start workers
if conf.General.Mode == domain.ReleaseMode {
Expand Down Expand Up @@ -265,7 +262,7 @@ func serveCmd() *cobra.Command { //nolint:maintidx
}

if conf.SSH.Enabled {
demoFetcher := demo.NewFetcher(dbUsecase, configUsecase, serversUsecase, assetUsecase, demoUsecase)
demoFetcher := demo.NewFetcher(dbConn, configUsecase, serversUsecase, assetUsecase, demoUsecase)
go demoFetcher.Start(ctx)
}

Expand Down
26 changes: 2 additions & 24 deletions internal/discord/discord_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (bot *nullDiscordRepository) Start() error {
return nil
}

func (bot *nullDiscordRepository) SendPayload(_ domain.DiscordChannel, _ *discordgo.MessageEmbed) {
func (bot *nullDiscordRepository) SendPayload(_ string, _ *discordgo.MessageEmbed) {
}

type discordRepository struct {
Expand Down Expand Up @@ -231,33 +231,11 @@ func (bot *discordRepository) sendInteractionResponse(session *discordgo.Session
return nil
}

func (bot *discordRepository) SendPayload(channel domain.DiscordChannel, payload *discordgo.MessageEmbed) {
func (bot *discordRepository) SendPayload(channelID string, payload *discordgo.MessageEmbed) {
if !bot.isReady.Load() {
return
}

var channelID string

switch channel {
case domain.ChannelMod:
channelID = bot.conf.Discord.LogChannelID
case domain.ChannelModLog:
channelID = bot.conf.Discord.LogChannelID
case domain.ChannelPublicLog:
channelID = bot.conf.Discord.PublicLogChannelID
case domain.ChannelPublicMatchLog:
channelID = bot.conf.Discord.PublicMatchLogChannelID
case domain.ChannelModAppealLog:
case domain.ChannelModVoteLog:
case domain.ChannelBanLog:
case domain.ChannelForumLog:
case domain.ChannelWordFilterLog:
}

if channelID == "" {
channelID = bot.conf.Discord.LogChannelID
}

if _, errSend := bot.session.ChannelMessageSendEmbed(channelID, payload); errSend != nil {
slog.Error("Failed to send discord payload", log.ErrAttr(errSend))
}
Expand Down
38 changes: 0 additions & 38 deletions internal/discord/discord_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ func (h discordService) makeOnUnban() func(_ context.Context, _ *discordgo.Sessi
func (h discordService) makeOnFilter() func(_ context.Context, _ *discordgo.Session, _ *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) { //nolint:maintidx
return func(ctx context.Context, session *discordgo.Session, interaction *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) {
switch interaction.ApplicationCommandData().Options[0].Name {
case "add":
return h.onFilterAdd(ctx, session, interaction)
case "del":
return h.onFilterDel(ctx, session, interaction)
case "check":
return h.onFilterCheck(ctx, session, interaction)
default:
Expand Down Expand Up @@ -501,40 +497,6 @@ func (h discordService) makeOnPlayers() func(context.Context, *discordgo.Session
}
}

func (h discordService) onFilterAdd(ctx context.Context, _ *discordgo.Session, interaction *discordgo.InteractionCreate,
) (*discordgo.MessageEmbed, error) {
opts := domain.OptionMap(interaction.ApplicationCommandData().Options[0].Options)
pattern := opts[domain.OptPattern].StringValue()
isRegex := opts[domain.OptIsRegex].BoolValue()

author, errAuthor := h.getDiscordAuthor(ctx, interaction)
if errAuthor != nil {
return nil, errAuthor
}

return h.discord.FilterAdd(ctx, author, pattern, isRegex)
}

func (h discordService) onFilterDel(ctx context.Context, _ *discordgo.Session, interaction *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) {
opts := domain.OptionMap(interaction.ApplicationCommandData().Options[0].Options)
wordID := opts["filter"].IntValue()

if wordID <= 0 {
return nil, domain.ErrInvalidFilterID
}

filter, errGetFilter := h.wordFilters.GetFilterByID(ctx, wordID)
if errGetFilter != nil {
return nil, domain.ErrCommandFailed
}

if errDropFilter := h.wordFilters.DropFilter(ctx, filter); errDropFilter != nil {
return nil, domain.ErrCommandFailed
}

return FilterDelMessage(filter), nil
}

func (h discordService) onFilterCheck(_ context.Context, _ *discordgo.Session, interaction *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) {
opts := domain.OptionMap(interaction.ApplicationCommandData().Options[0].Options)
message := opts[domain.OptMessage].StringValue()
Expand Down
Loading
Loading