You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
themis/cmd/themis-server/main.go

306 lines
8.4 KiB

2 years ago
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"go.wperron.io/themis"
)
const (
DB_FILE = "prod.db"
CONN_STRING = "file:" + DB_FILE + "?cache=shared&mode=rw&_journal_mode=WAL"
DISCORD_APP_ID = "1014881815921705030"
DISCORD_GUILD_ID = "1014883118764806164"
)
var (
dbFile = flag.String("db", "", "SQlite database file path")
store *themis.Store
)
type Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
2 years ago
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)
defer cancel()
flag.Parse()
err := touchDbFile(*dbFile)
if err != nil {
log.Fatalln("fatal error: failed to touch database file:", err)
}
store, err = themis.NewStore(CONN_STRING)
if err != nil {
log.Fatalln("fatal error: failed to initialize database:", err)
}
authToken, ok := os.LookupEnv("DISCORD_TOKEN")
if !ok {
log.Fatalln("fatal error: no auth token found at DISCORD_TOKEN env var")
}
discord, err := discordgo.New(fmt.Sprintf("Bot %s", authToken))
if err != nil {
log.Fatalln("fatal error: failed to create discord app:", err)
}
commands := []*discordgo.ApplicationCommand{
{
Name: "ping",
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,
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]Handler{
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
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{
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.WriteString(fmt.Sprintf("There are currently %d claims:\n", len(claims)))
sb.WriteString("```\n")
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{
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)
if err != nil {
conflict, ok := err.(themis.ErrConflict)
if ok {
sb := strings.Builder{}
sb.WriteString("Some provinces are already claimed:\n```\n")
for _, c := range conflict.Conflicts {
sb.WriteString(fmt.Sprintf(" - %s\n", c))
}
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)
}
return
}
fmt.Printf("[error]: failed to acquire claim: %s\n", err)
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 = 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)
}
},
}
registerHandlers(discord, handlers)
err = discord.Open()
if err != nil {
log.Fatalln("fatal error: failed to open session:", err)
}
defer discord.Close()
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, c := range commands {
command, err := discord.ApplicationCommandCreate(DISCORD_APP_ID, DISCORD_GUILD_ID, c)
if err != nil {
log.Fatalln("fatal error: failed to register command:", err)
}
registeredCommands[i] = command
}
log.Printf("registered %d commands\n", len(registeredCommands))
go func() {
if err := serve(":8080"); err != nil {
log.Printf("[error]: %s\n", err)
}
cancel()
}()
<-ctx.Done()
for _, c := range registeredCommands {
err = discord.ApplicationCommandDelete(DISCORD_APP_ID, DISCORD_GUILD_ID, c.ID)
if err != nil {
log.Printf("[error]: failed to delete command: %s\n", err)
}
}
log.Println("deregistered commands, bye bye!")
os.Exit(0)
}
func touchDbFile(path string) error {
f, err := os.Open(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
f, err := os.Create(path)
if err != nil {
return err
}
f.Close()
} else {
return err
}
}
f.Close()
return nil
}
func registerHandlers(sess *discordgo.Session, handlers map[string]Handler) {
sess.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
sess.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := handlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
2 years ago
}
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()
}
func serve(address string) error {
http.Handle("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
w.WriteHeader(http.StatusOK)
}))
return http.ListenAndServe(address, nil)
}