Skip to content

Commit

Permalink
Add basic discord log query
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jan 30, 2025
1 parent f574f5a commit 58767c6
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ func serveCmd() *cobra.Command { //nolint:maintidx
}

discordHandler := discord.NewDiscordHandler(discordUsecase, personUsecase, banUsecase,
stateUsecase, serversUC, configUsecase, networkUsecase, wordFilterUsecase, matchUsecase, banNetUsecase, banASNUsecase)
stateUsecase, serversUC, configUsecase, networkUsecase, wordFilterUsecase, matchUsecase, banNetUsecase,
banASNUsecase, anticheatUsecase)
discordHandler.Start(ctx)

anticheat.NewHandler(router, authUsecase, anticheatUsecase)
Expand Down
16 changes: 16 additions & 0 deletions internal/discord/discord_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,22 @@ func (bot *discordRepository) botRegisterSlashCommands(appID string) error {
// },
},
},
{
ApplicationID: appID,
Name: string(domain.CmdAC),
Description: "Query Anticheat Logs",
DefaultMemberPermissions: &modPerms,
Options: []*discordgo.ApplicationCommandOption{
{
Name: string(domain.CmdACPlayer),
Description: "Query a players anticheat logs by steam id",
Options: []*discordgo.ApplicationCommandOption{
optUserID,
},
},
},
},

{
ApplicationID: appID,
Name: string(domain.CmdFilter),
Expand Down
41 changes: 41 additions & 0 deletions internal/discord/discord_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

type discordService struct {
anticheat domain.AntiCheatUsecase
discord domain.DiscordUsecase
persons domain.PersonUsecase
bansSteam domain.BanSteamUsecase
Expand All @@ -39,6 +40,7 @@ func NewDiscordHandler(discordUsecase domain.DiscordUsecase, persons domain.Pers
bansSteam domain.BanSteamUsecase, state domain.StateUsecase, servers domain.ServersUsecase,
config domain.ConfigUsecase, network domain.NetworkUsecase, wordFilters domain.WordFilterUsecase,
matches domain.MatchUsecase, bansNet domain.BanNetUsecase, bansASN domain.BanASNUsecase,
anticheat domain.AntiCheatUsecase,
) domain.ServiceStarter {
handler := &discordService{
discord: discordUsecase,
Expand All @@ -52,6 +54,7 @@ func NewDiscordHandler(discordUsecase domain.DiscordUsecase, persons domain.Pers
wordFilters: wordFilters,
bansNet: bansNet,
bansASN: bansASN,
anticheat: anticheat,
}

return handler
Expand All @@ -76,6 +79,7 @@ func (h discordService) Start(_ context.Context) {
domain.CmdServers: h.makeOnServers(),
domain.CmdUnban: h.makeOnUnban(),
domain.CmdStats: h.makeOnStats(),
domain.CmdAC: h.makeOnAC(),
}

for k, v := range cmdMap {
Expand Down Expand Up @@ -504,6 +508,43 @@ func (h discordService) makeOnStats() func(context.Context, *discordgo.Session,
}
}

func (h discordService) makeOnAC() func(context.Context, *discordgo.Session, *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) {
return func(ctx context.Context, session *discordgo.Session, interaction *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) {
name := interaction.ApplicationCommandData().Options[0].Name
switch name {
case "player":
return h.onACPlayer(ctx, session, interaction)
// case string(cmdStatsGlobal):
// return discord.onStatsGlobal(ctx, session, interaction, response)
// case string(cmdStatsServer):
// return discord.onStatsServer(ctx, session, interaction, response)
default:
return nil, domain.ErrCommandFailed
}
}
}

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

steamID, errResolveSID := steamid.Resolve(ctx, opts[domain.OptUserIdentifier].StringValue())
if errResolveSID != nil || !steamID.Valid() {
return nil, domain.ErrInvalidSID
}

person, errAuthor := h.persons.GetPersonBySteamID(ctx, nil, steamID)
if errAuthor != nil {
return nil, errAuthor
}

logs, errQuery := h.anticheat.Query(ctx, domain.AnticheatQuery{SteamID: steamID.String()})
if errQuery != nil {
return nil, errQuery
}

return ACPlayerLogs(person, logs), nil
}

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

Expand Down
34 changes: 34 additions & 0 deletions internal/discord/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,40 @@ func FilterCheckMessage(matches []domain.Filter) *discordgo.MessageEmbed {
return msgEmbed.Embed().Truncate().MessageEmbed
}

func ACPlayerLogs(person domain.PersonInfo, entries []domain.AnticheatEntry) *discordgo.MessageEmbed {
sid := person.GetSteamID()
emb := NewEmbed()
emb.Embed().
SetTitle(fmt.Sprintf("Anticheat DetectionsStats: %s (%s)", person.GetName(), sid.String())).
SetColor(ColourSuccess)

emb.Embed().SetDescription(fmt.Sprintf("Logs ```%s```",
makeACLogTable(entries),
))

return emb.Embed().MessageEmbed
}

func makeACLogTable(entries []domain.AnticheatEntry) string {
writer := &strings.Builder{}
table := defaultTable(writer)
table.SetHeader([]string{"Srv", "Time", "Name", "Cnt", "Summary"})

for _, player := range entries {
table.Append([]string{
player.ServerName,
player.CreatedOn.Format(time.DateTime),
player.Name,
string(player.Detection),
player.Summary,
})
}

table.Render()

return strings.Trim(writer.String(), "\n")
}

func StatsPlayerMessage(person domain.PersonInfo, url string, classStats domain.PlayerClassStatsCollection,
medicStats []domain.PlayerMedicStats, weaponStats []domain.PlayerWeaponStats, killstreakStats []domain.PlayerKillstreakStats,
) *discordgo.MessageEmbed {
Expand Down
2 changes: 2 additions & 0 deletions internal/domain/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type FoundPlayer struct {
type Cmd string

const (
CmdAC Cmd = "ac"
CmdACPlayer Cmd = "player"
CmdBan Cmd = "ban"
CmdFind Cmd = "find"
CmdMute Cmd = "mute"
Expand Down

0 comments on commit 58767c6

Please sign in to comment.