Add command aliases, cleanup types and order

This commit is contained in:
Michał Gdula 2025-04-02 19:23:17 +01:00
parent a70ca23e41
commit 10acb07216
4 changed files with 58 additions and 83 deletions

View file

@ -21,14 +21,16 @@ type Config struct {
}
type App struct {
Config Config
Commands map[string]Callback
Config Config
Commands map[string]Callback
CommandAliases map[string]string
}
func NewApp(config Config) *App {
return &App{
Config: config,
Commands: make(map[string]Callback),
Config: config,
Commands: make(map[string]Callback),
CommandAliases: make(map[string]string),
}
}
@ -36,6 +38,10 @@ func (a *App) RegisterCommand(cmd string, f Callback) {
a.Commands[cmd] = f
}
func (a *App) RegisterCommandAlias(alias, cmd string) {
a.CommandAliases[alias] = cmd
}
func (a *App) Run() {
dg, err := discordgo.New("Bot " + a.Config.Token)
if err != nil {
@ -92,6 +98,11 @@ func (a *App) handler(session *discordgo.Session, message *discordgo.MessageCrea
cmd = strings.TrimPrefix(cmd, a.Config.Prefix)
cmd, args, _ = strings.Cut(cmd, " ")
alias, ok := a.CommandAliases[cmd]
if ok {
cmd = alias
}
callback, ok := a.Commands[cmd]
if !ok {
// Falling back to default help command

View file

@ -21,9 +21,46 @@ var client = http.Client{
var username = os.Getenv("E621_USERNAME")
var password = os.Getenv("E621_PASSWORD")
type post struct {
Id int `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
File struct {
Width int `json:"width"`
Height int `json:"height"`
Ext string `json:"ext"`
Size int `json:"size"`
Md5 string `json:"md5"`
Url string `json:"url"`
} `json:"file"`
Score struct {
Up int `json:"up"`
Down int `json:"down"`
Total int `json:"total"`
} `json:"score"`
Tags struct {
General []string `json:"general"`
Artist []string `json:"artist"`
Contributor []interface{} `json:"contributor"`
Copyright []string `json:"copyright"`
Character []interface{} `json:"character"`
Species []string `json:"species"`
Invalid []interface{} `json:"invalid"`
Meta []string `json:"meta"`
Lore []interface{} `json:"lore"`
} `json:"tags"`
Rating string `json:"rating"`
FavCount int `json:"fav_count"`
Sources []string `json:"sources"`
Description string `json:"description"`
CommentCount int `json:"comment_count"`
}
func RegisterPorbCommands(a *app.App) {
if username != "" && password != "" {
a.RegisterCommand("e621", registerE621(a))
a.RegisterCommandAlias("porb", "e621")
} else {
log.Println("Not registering e621 command...")
}

View file

@ -1,75 +0,0 @@
package porb
import (
"time"
)
type post struct {
Id int `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
File struct {
Width int `json:"width"`
Height int `json:"height"`
Ext string `json:"ext"`
Size int `json:"size"`
Md5 string `json:"md5"`
Url string `json:"url"`
} `json:"file"`
Preview struct {
Width int `json:"width"`
Height int `json:"height"`
Url string `json:"url"`
} `json:"preview"`
Sample struct {
Has bool `json:"has"`
Height int `json:"height"`
Width int `json:"width"`
Url string `json:"url"`
Alternates struct {
} `json:"alternates"`
} `json:"sample"`
Score struct {
Up int `json:"up"`
Down int `json:"down"`
Total int `json:"total"`
} `json:"score"`
Tags struct {
General []string `json:"general"`
Artist []string `json:"artist"`
Contributor []interface{} `json:"contributor"`
Copyright []string `json:"copyright"`
Character []interface{} `json:"character"`
Species []string `json:"species"`
Invalid []interface{} `json:"invalid"`
Meta []string `json:"meta"`
Lore []interface{} `json:"lore"`
} `json:"tags"`
LockedTags []interface{} `json:"locked_tags"`
ChangeSeq int `json:"change_seq"`
Flags struct {
Pending bool `json:"pending"`
Flagged bool `json:"flagged"`
NoteLocked bool `json:"note_locked"`
StatusLocked bool `json:"status_locked"`
RatingLocked bool `json:"rating_locked"`
Deleted bool `json:"deleted"`
} `json:"flags"`
Rating string `json:"rating"`
FavCount int `json:"fav_count"`
Sources []string `json:"sources"`
Pools []int `json:"pools"`
Relationships struct {
ParentId interface{} `json:"parent_id"`
HasChildren bool `json:"has_children"`
HasActiveChildren bool `json:"has_active_children"`
Children []interface{} `json:"children"`
} `json:"relationships"`
ApproverId interface{} `json:"approver_id"`
UploaderId int `json:"uploader_id"`
Description string `json:"description"`
CommentCount int `json:"comment_count"`
IsFavorited bool `json:"is_favorited"`
HasNotes bool `json:"has_notes"`
Duration interface{} `json:"duration"`
}

View file

@ -18,10 +18,6 @@ var client = http.Client{
Timeout: 10 * time.Second,
}
func RegisterTinyfoxCommands(a *app.App) {
a.RegisterCommand("animal", registerAnimal(a))
}
var animals = []string{
"fox",
"yeen",
@ -85,6 +81,12 @@ var animalAliases = map[string]string{
"opossum": "poss",
}
func RegisterTinyfoxCommands(a *app.App) {
a.RegisterCommand("animal", registerAnimal(a))
a.RegisterCommandAlias("a", "animal")
}
func registerAnimal(a *app.App) app.Callback {
return func(h *app.Handler, args []string) app.Error {
var options struct {