Skip to content
Open
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
3 changes: 3 additions & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ moderation:
## Mod role to be pinged in report posts
#mod_role_id: ""

## Guild ID to scope bot commands to (optional, needed for prompt updates)
#guild_id: ""

## Logging settings
logging:
## Size of log file (MB)
Expand Down
3 changes: 3 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Config struct {

moderation struct {
botToken string
guildId string
channelId string
modRoleId string
}
Expand Down Expand Up @@ -88,6 +89,7 @@ type ConfigFile struct {
Moderation *struct {
BotToken string `yaml:"bot_token"`
ChannelID string `yaml:"channel_id"`
GuildID string `yaml:"guild_id"`
ModRoleID string `yaml:"mod_role_id"`
} `yaml:"moderation"`

Expand Down Expand Up @@ -182,6 +184,7 @@ func parseConfigFile(filename string) *Config {
config.moderation.botToken = mod.BotToken
config.moderation.channelId = mod.ChannelID
config.moderation.modRoleId = mod.ModRoleID
config.moderation.guildId = mod.GuildID
}

if ipc := configFile.Ipc; ipc != nil {
Expand Down
94 changes: 94 additions & 0 deletions server/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func initModBot() {
case discordgo.InteractionModalSubmit:
botHandleModalResponse(&resp, action.ModalSubmitData(), action.Interaction)
return
case discordgo.InteractionApplicationCommand:
botHandleCommandResponse(&resp, action.ApplicationCommandData())
return
}

if action.Type != discordgo.InteractionMessageComponent {
Expand Down Expand Up @@ -255,10 +258,17 @@ func initModBot() {
}
})

bot.Identify.Intents = discordgo.IntentsGuilds
if err = bot.Open(); err != nil {
log.Printf("bot/open: %s", err)
return
}

if err = registerBotCommands(); err != nil {
log.Printf("bot/registerBotCommands: %s", err)
return
}

}

func parseMsgIdFromComponent(msgObj *discordgo.Message) string {
Expand Down Expand Up @@ -335,6 +345,90 @@ func botHandleModalResponse(resp *discordgo.InteractionResponse, data discordgo.
}
}

func botHandleCommandResponse(resp *discordgo.InteractionResponse, data discordgo.ApplicationCommandInteractionData) {
args := data.Options
switch data.Name {
case "pinfo":
if len(args) != 1 {
setResponse(resp, "Usage: /pinfo <NAME|UUID>")
return
}
playerid := args[0].StringValue()
var (
onlineGames []string
name, uuid string
banned, muted bool
err error
)
rows, err := db.Query(`
SELECT
pgd.name, pgd.uuid, pgd.game, pgd.online, players.banned, players.muted
FROM playerGameData pgd
JOIN players ON players.uuid = pgd.uuid
WHERE pgd.name = ? OR pgd.uuid = ?`, playerid, playerid)
if err != nil {
setResponse(resp, fmt.Sprintf("pinfo: sql error: %s", err))
return
}

defer rows.Close()
for rows.Next() {
var (
game string
online bool
)
err = rows.Scan(&name, &uuid, &game, &online, &banned, &muted)
if err != nil {
setResponse(resp, fmt.Sprintf("pinfo: sql error: %s", err))
return
}
if online {
onlineGames = append(onlineGames, game)
}
}
msg := fmt.Sprintf(`##### Player Info
name=%s uuid=%s
banned=%t muted=%t
online in: %s`, name, uuid, banned, muted, strings.Join(onlineGames, ", "))
setResponse(resp, msg)
default:
setResponse(resp, "Unknown command")
}
}

func registerBotCommands() (err error) {
if bot == nil {
return
}

_, err = bot.ApplicationCommandCreate(
bot.State.User.ID,
config.moderation.guildId,
&discordgo.ApplicationCommand{
Name: "pinfo",
Description: "Show player info",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "name",
Description: "player name or uuid",
Required: true,
},
},
},
)

return
}

func setResponse(resp *discordgo.InteractionResponse, err string) {
resp.Type = discordgo.InteractionResponseChannelMessageWithSource
resp.Data = &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: err,
}
}

func initModActionExpirations() {
modActionExpirations = make(map[ModAction]oneshotJob)

Expand Down