2024-10-21 19:58:19 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
commands = []*discordgo.ApplicationCommand{
|
|
|
|
{
|
|
|
|
Name: "whitelist-add",
|
|
|
|
Description: "Add yourself to the whitelist",
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
Name: "minecraft-username",
|
|
|
|
Description: "Minecraft Username",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "whitelist-remove",
|
|
|
|
Description: "Remove yourself from the whitelist",
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "whitelist-show",
|
|
|
|
Description: "Display your whitelist link",
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
|
|
|
"whitelist-add": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
// Access options in the order provided by the user.
|
|
|
|
options := i.ApplicationCommandData().Options
|
|
|
|
|
|
|
|
// Convert the slice into a map
|
|
|
|
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
|
|
|
|
for _, opt := range options {
|
|
|
|
optionMap[opt.Name] = opt
|
|
|
|
}
|
|
|
|
|
|
|
|
requestedUsername := (optionMap["minecraft-username"]).StringValue()
|
|
|
|
invokingUserName := i.Member.User.Username
|
|
|
|
invokingUserID := i.Member.User.ID
|
|
|
|
|
|
|
|
// check requestedUsername validity
|
|
|
|
valid := checkMcUsernameValid(requestedUsername)
|
|
|
|
if !valid {
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("`%s` is not a valid minecraft username", requestedUsername))
|
2024-10-21 19:58:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
existingMCUsername, ok := db[invokingUserID]
|
|
|
|
if !ok { // invoking user does not currently have an mc username associated
|
|
|
|
// execute RCON
|
|
|
|
response, err := conn.Execute(fmt.Sprintf("whitelist add %s", requestedUsername))
|
|
|
|
if err != nil {
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("Failed to add `%s` to whitelist: %s", requestedUsername, err.Error()))
|
2024-10-21 19:58:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// this can happen if the username is not a real player, OR if the user is already on the whitelist
|
|
|
|
// In both cases we want to exit early to avoid adding an invalid username to the db
|
|
|
|
if !strings.EqualFold(response, fmt.Sprintf("Added %s to the whitelist", requestedUsername)) {
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("Failed to add `%s` to whitelist: %s", requestedUsername, response))
|
2024-10-21 19:58:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// save state to db
|
|
|
|
db[invokingUserID] = requestedUsername
|
|
|
|
SaveDB(dbPath, db)
|
|
|
|
|
|
|
|
// send response
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("`%s` linked minecraft username `%s`", invokingUserName, requestedUsername))
|
2024-10-21 19:58:19 +00:00
|
|
|
} else {
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("`%s` already has a linked minecraft username `%s`", invokingUserName, existingMCUsername))
|
2024-10-21 19:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
"whitelist-remove": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
invokingUserName := i.Member.User.Username
|
|
|
|
invokingUserID := i.Member.User.ID
|
|
|
|
|
|
|
|
existingMCUsername, ok := db[invokingUserID]
|
|
|
|
if !ok { // invoking user does not currently have an mc username associated
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("`%s` does not have a linked minecraft username", invokingUserName))
|
2024-10-21 19:58:19 +00:00
|
|
|
} else {
|
|
|
|
// execute RCON
|
|
|
|
response, err := conn.Execute(fmt.Sprintf("whitelist remove %s", existingMCUsername))
|
|
|
|
if err != nil {
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("Failed to remove `%s` from whitelist: %s", existingMCUsername, err.Error()))
|
2024-10-21 19:58:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// this can happen if the username is not a real player, OR if the user is already not on the whitelist
|
|
|
|
if !strings.EqualFold(response, fmt.Sprintf("Removed %s from the whitelist", existingMCUsername)) {
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("Failed to remove `%s` from whitelist: %s", existingMCUsername, response))
|
2024-10-21 19:58:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// save state to db
|
|
|
|
delete(db, invokingUserID)
|
|
|
|
SaveDB(dbPath, db)
|
|
|
|
|
|
|
|
// send response
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("`%s` unlinked minecraft username `%s`", invokingUserName, existingMCUsername))
|
2024-10-21 19:58:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"whitelist-show": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
invokingUserName := i.Member.User.Username
|
|
|
|
invokingUserID := i.Member.User.ID
|
|
|
|
|
|
|
|
existingMCUsername, ok := db[invokingUserID]
|
|
|
|
|
|
|
|
if ok {
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("`%s` is `%s`", invokingUserName, existingMCUsername))
|
2024-10-21 19:58:19 +00:00
|
|
|
} else {
|
2024-12-19 22:10:18 +00:00
|
|
|
simpleResponse(s, i, fmt.Sprintf("`%s` does not have a linked minecraft username", invokingUserName))
|
2024-10-21 19:58:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|