|
|
@ -8,6 +8,7 @@ import (
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
|
|
|
@ -29,7 +30,7 @@ var (
|
|
|
|
store *themis.Store
|
|
|
|
store *themis.Store
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var ()
|
|
|
|
type Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)
|
|
|
@ -59,140 +60,120 @@ func main() {
|
|
|
|
|
|
|
|
|
|
|
|
commands := []*discordgo.ApplicationCommand{
|
|
|
|
commands := []*discordgo.ApplicationCommand{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Name: "themis",
|
|
|
|
Name: "ping",
|
|
|
|
Description: "Call dibs on EU4 provinces",
|
|
|
|
Description: "Ping Themis",
|
|
|
|
|
|
|
|
Type: discordgo.ChatApplicationCommand,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Name: "list-claims",
|
|
|
|
|
|
|
|
Description: "List current claims",
|
|
|
|
|
|
|
|
Type: discordgo.ChatApplicationCommand,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Name: "claim",
|
|
|
|
|
|
|
|
Description: "Take a claim on provinces",
|
|
|
|
Type: discordgo.ChatApplicationCommand,
|
|
|
|
Type: discordgo.ChatApplicationCommand,
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Name: "ping",
|
|
|
|
Name: "claim-type",
|
|
|
|
Description: "Ping Themis",
|
|
|
|
Description: "one of `area`, `region` or `trade`",
|
|
|
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Name: "list-claims",
|
|
|
|
|
|
|
|
Description: "List current claims",
|
|
|
|
|
|
|
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Name: "claim",
|
|
|
|
Name: "name",
|
|
|
|
Description: "Take a claim on provinces",
|
|
|
|
Description: "the name of zone claimed",
|
|
|
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Name: "claim-type",
|
|
|
|
|
|
|
|
Description: "one of `area`, `region` or `trade`",
|
|
|
|
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Name: "name",
|
|
|
|
|
|
|
|
Description: "the name of zone claimed",
|
|
|
|
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
|
|
|
handlers := map[string]Handler{
|
|
|
|
"themis": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
options := i.ApplicationCommandData().Options
|
|
|
|
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
switch options[0].Name {
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
case "ping":
|
|
|
|
Content: "Pong",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"list-claims": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
|
|
|
|
claims, err := store.ListClaims(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: "Pong",
|
|
|
|
Content: "Oops, something went wrong! :(",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "list-claims":
|
|
|
|
}
|
|
|
|
claims, err := store.ListClaims(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
|
|
|
|
Content: "Oops, something went wrong! :(",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sb := strings.Builder{}
|
|
|
|
sb := strings.Builder{}
|
|
|
|
sb.WriteString(fmt.Sprintf("There are currently %d claims:\n", len(claims)))
|
|
|
|
sb.WriteString(fmt.Sprintf("There are currently %d claims:\n", len(claims)))
|
|
|
|
for _, c := range claims {
|
|
|
|
sb.WriteString("```\n")
|
|
|
|
sb.WriteString(fmt.Sprintf("%s\n", c))
|
|
|
|
sb.WriteString(formatClaimsTable(claims))
|
|
|
|
}
|
|
|
|
sb.WriteString("```\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
|
|
|
|
Content: sb.String(),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"claim": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
|
|
|
|
opts := i.ApplicationCommandData().Options
|
|
|
|
|
|
|
|
claimType, err := themis.ClaimTypeFromString(opts[0].StringValue())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: sb.String(),
|
|
|
|
Content: "You can only take claims of types `area`, `region` or `trade`",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "claim":
|
|
|
|
return
|
|
|
|
opts := options[0].Options
|
|
|
|
}
|
|
|
|
claimType, err := themis.ClaimTypeFromString(opts[0].StringValue())
|
|
|
|
name := opts[1].StringValue()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
|
|
|
|
Content: "You can only take claims of types `area`, `region` or `trade`",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
name := opts[1].StringValue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
player := i.Member.Nick
|
|
|
|
|
|
|
|
if player == "" {
|
|
|
|
|
|
|
|
player = i.Member.User.Username
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = store.Claim(ctx, player, name, claimType)
|
|
|
|
player := i.Member.Nick
|
|
|
|
if err != nil {
|
|
|
|
if player == "" {
|
|
|
|
fmt.Printf("[error]: failed to acquire claim: %s\n", err)
|
|
|
|
player = i.Member.User.Username
|
|
|
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
}
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
|
|
|
|
Content: "failed to acquire claim :(",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = store.Claim(ctx, player, name, claimType)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Printf("[error]: failed to acquire claim: %s\n", err)
|
|
|
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: fmt.Sprintf("Claimed %s for %s!", name, player),
|
|
|
|
Content: "failed to acquire claim :(",
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
|
|
|
|
Content: fmt.Sprintf("Oops, I don't know any `%s` action", options[0].Name),
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
|
|
|
|
Content: fmt.Sprintf("Claimed %s for %s!", name, player),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("[error] failed to respond to command:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -245,7 +226,7 @@ func touchDbFile(path string) error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func registerHandlers(sess *discordgo.Session, handlers map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)) {
|
|
|
|
func registerHandlers(sess *discordgo.Session, handlers map[string]Handler) {
|
|
|
|
sess.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
|
|
|
sess.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
|
|
|
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
|
|
|
|
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -255,3 +236,32 @@ func registerHandlers(sess *discordgo.Session, handlers map[string]func(s *disco
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const TABLE_PATTERN = "| %-*s | %-*s | %-*s | %-*s |\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func formatClaimsTable(claims []themis.Claim) string {
|
|
|
|
|
|
|
|
sb := strings.Builder{}
|
|
|
|
|
|
|
|
maxLengths := []int{2, 6, 4, 4} // id, player, type, name
|
|
|
|
|
|
|
|
for _, c := range claims {
|
|
|
|
|
|
|
|
sid := strconv.Itoa(c.ID)
|
|
|
|
|
|
|
|
if len(sid) > maxLengths[0] {
|
|
|
|
|
|
|
|
maxLengths[0] = len(sid)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Player) > maxLengths[1] {
|
|
|
|
|
|
|
|
maxLengths[1] = len(c.Player)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Type) > maxLengths[2] {
|
|
|
|
|
|
|
|
maxLengths[2] = len(c.Type)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Name) > maxLengths[3] {
|
|
|
|
|
|
|
|
maxLengths[3] = len(c.Name)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sb.WriteString(fmt.Sprintf(TABLE_PATTERN, maxLengths[0], "ID", maxLengths[1], "Player", maxLengths[2], "Type", maxLengths[3], "Name"))
|
|
|
|
|
|
|
|
sb.WriteString(fmt.Sprintf(TABLE_PATTERN, maxLengths[0], strings.Repeat("-", maxLengths[0]), maxLengths[1], strings.Repeat("-", maxLengths[1]), maxLengths[2], strings.Repeat("-", maxLengths[2]), maxLengths[3], strings.Repeat("-", maxLengths[3])))
|
|
|
|
|
|
|
|
for _, c := range claims {
|
|
|
|
|
|
|
|
sb.WriteString(fmt.Sprintf(TABLE_PATTERN, maxLengths[0], strconv.Itoa(c.ID), maxLengths[1], c.Player, maxLengths[2], c.Type, maxLengths[3], c.Name))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
|
|
|
|
}
|
|
|
|