canopeas/discord/history.go

87 lines
2.1 KiB
Go

package discord
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"go.jolheiser.com/beaver"
"go.jolheiser.com/gojang"
"go.jolheiser.com/gojang/rate"
)
func init() {
cmd := command{
validate: func(cmd commandInit) bool {
return true
},
run: func(cmd commandInit) (string, error) {
args := strings.Fields(cmd.message.Content)
if len(args) < 2 {
return "You must give this command a Minecraft username", nil
}
sendTyping(cmd.session, cmd.message.ChannelID)
at := time.Now()
if len(args) > 2 {
t, err := time.Parse("01/02/2006", args[2])
if err != nil {
return `Could not parse the time. Use the format 01/30/2006`, nil
}
at = t
}
client := gojang.New(time.Second * 5)
profile, err := client.Profile(args[1], at)
if err != nil {
if gojang.IsPlayerNotFoundError(err) {
return "Could not find a player with that username at that time.", nil
}
if rate.IsRateLimitExceededError(err) {
return "Rate limited by Mojang, slow down!", nil
}
beaver.Errorf("Profile: %v", err)
return "Could not contact the Mojang API.", nil
}
names, err := client.UUIDToNameHistory(profile.UUID)
if err != nil {
beaver.Errorf("UUIDToNameHistory: %v", err)
return "Could not contact the Mojang API.", nil
}
var history string
for _, name := range names {
cleaned := strings.NewReplacer("_", "\\_", "*", "\\*").Replace(name.Name)
history += fmt.Sprintf("\n%s", cleaned)
if name.ChangedToAt == 0 {
history += " (original)"
} else {
history += fmt.Sprintf(" (%s)", name.ChangedToAtTime().Format("01/02/2006"))
}
}
embed := &discordgo.MessageEmbed{
Color: 0x7ed321,
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: fmt.Sprintf("https://minotar.net/helm/%s/100.png", names.Current().Name),
},
Fields: []*discordgo.MessageEmbedField{
{
Name: fmt.Sprintf("%s's Name History", names.Current().Name),
Value: history,
},
},
}
sendEmbed(cmd.session, cmd.message.ChannelID, embed)
return "", nil
},
help: "Minecraft name history",
}
commands["history"] = cmd
commands["names"] = cmd
}