From 299b0303a53f94e3f2a319ab7d308b6bfab450c7 Mon Sep 17 00:00:00 2001 From: Daniel Hawton Date: Wed, 6 Sep 2023 00:09:00 -0600 Subject: [PATCH] fixes --- cmd/bot/app/start.go | 25 ---------- commands.go | 99 --------------------------------------- pkg/config/configtypes.go | 30 +----------- pkg/database/bot.go | 49 ------------------- 4 files changed, 1 insertion(+), 202 deletions(-) delete mode 100644 commands.go delete mode 100644 pkg/database/bot.go diff --git a/cmd/bot/app/start.go b/cmd/bot/app/start.go index c59d20e..c3cc67b 100644 --- a/cmd/bot/app/start.go +++ b/cmd/bot/app/start.go @@ -24,7 +24,6 @@ import ( "strings" "syscall" - "github.com/adh-partnership/api/pkg/database" "github.com/adh-partnership/api/pkg/logger" "github.com/urfave/cli/v2" @@ -33,7 +32,6 @@ import ( "github.com/vpaza/bot/internal/facility" "github.com/vpaza/bot/pkg/cache" "github.com/vpaza/bot/pkg/config" - botdatabase "github.com/vpaza/bot/pkg/database" "github.com/vpaza/bot/pkg/jobs" ) @@ -92,29 +90,6 @@ func newStartCommand() *cli.Command { log.Debugf("FacCfg=%+v", facility.FacCfg) - log.Infof("Building database connection...") - err = database.Connect(database.DBOptions{ - Host: config.Cfg.Database.Host, - Port: config.Cfg.Database.Port, - User: config.Cfg.Database.Username, - Password: config.Cfg.Database.Password, - Database: config.Cfg.Database.Database, - CACert: config.Cfg.Database.CACert, - Driver: "mysql", - Logger: logger.Logger, - }) - if err != nil { - return err - } - - log.Infof("Running migrations...") - err = database.DB.AutoMigrate( - &botdatabase.Bot{}, - ) - if err != nil { - return err - } - log.Infof("Configuring cache") err = cache.Setup() if err != nil { diff --git a/commands.go b/commands.go deleted file mode 100644 index 10dd127..0000000 --- a/commands.go +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright Daniel Hawton - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package commands - -import ( - "github.com/adh-partnership/api/pkg/logger" - "github.com/bwmarrin/discordgo" - - "github.com/vpaza/bot/internal/commands/ping" -) - -type AppCommand struct { - Name string - ID string - Handler func(*discordgo.Session, *discordgo.InteractionCreate) - AppCommand *discordgo.ApplicationCommand -} - -var ( - commands map[string]*AppCommand - log = logger.Logger.WithField("component", "commands") -) - -func init() { - commands = make(map[string]*AppCommand) -} - -func SetupCommands() { - AddCommand(ping.Register()) -} - -func AddCommand(name string, handler func(*discordgo.Session, *discordgo.InteractionCreate), appCommand *discordgo.ApplicationCommand) { - commands[name] = &AppCommand{ - Name: name, - Handler: handler, - AppCommand: appCommand, - } -} - -func FindHandler(cmd string) (func(*discordgo.Session, *discordgo.InteractionCreate), bool) { - if command, ok := commands[cmd]; ok { - return command.Handler, true - } - return nil, false -} - -func RegisterCommands(s *discordgo.Session, guildid string) error { - // Clean all commands associated with us - registeredCommands, err := s.ApplicationCommands(s.State.User.ID, guildid) - if err != nil { - return err - } - for _, registeredCommand := range registeredCommands { - log.Warnf("Unregistering command %s, we didn't register", registeredCommand.Name) - err := s.ApplicationCommandDelete(s.State.User.ID, guildid, registeredCommand.ID) - if err != nil { - return err - } - } - - for _, command := range commands { - log.Infof("Registering command %s", command.Name) - appCommand, err := s.ApplicationCommandCreate(s.State.User.ID, guildid, command.AppCommand) - if err != nil { - return err - } - command.ID = appCommand.ID - } - - return nil -} - -func Unregister(s *discordgo.Session, guildid string) error { - for _, command := range commands { - if command.ID != "" { - log.Infof("Unregistering command %s", command.Name) - err := s.ApplicationCommandDelete(s.State.User.ID, guildid, command.ID) - if err != nil { - return err - } - } - } - - return nil -} diff --git a/pkg/config/configtypes.go b/pkg/config/configtypes.go index c9aaa05..38012be 100644 --- a/pkg/config/configtypes.go +++ b/pkg/config/configtypes.go @@ -17,38 +17,10 @@ package config type Config struct { - Database ConfigDatabase `json:"database"` - Discord ConfigDiscord `json:"discord"` -} - -type ConfigDatabase struct { - Host string `json:"host"` - Port string `json:"port"` - Username string `json:"user"` - Password string `json:"password"` - Database string `json:"database"` - AutoMigrate bool `json:"auto_migrate"` - CACert string `json:"ca_cert"` + Discord ConfigDiscord `json:"discord"` } type ConfigDiscord struct { AppID string `json:"app_id"` Token string `json:"token"` } - -type FacilityConfig struct { - Facility string `json:"facility"` - DiscordID string `json:"discord_id"` - Description string `json:"description"` - NameFormat string `json:"name_format"` - RosterAPI string `json:"roster_api"` - API string `json:"api"` - Roles []struct { - ID string `json:"id"` - Name string `json:"name"` - If []struct { - Condition string `json:"condition"` - Value string `json:"value"` - } `json:"if"` - } `json:"roles"` -} diff --git a/pkg/database/bot.go b/pkg/database/bot.go deleted file mode 100644 index f72e4e6..0000000 --- a/pkg/database/bot.go +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright Daniel Hawton - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package models - -import "github.com/adh-partnership/api/pkg/database" - -type Bot struct { - ID int64 - Key string - Value string -} - -func Get(key string) (string, error) { - var bot Bot - err := database.DB.Where(&Bot{Key: key}).First(&bot).Error - if err != nil { - return "", err - } - - return bot.Value, nil -} - -func Set(key, value string) error { - bot := Bot{ - Key: key, - Value: value, - } - - err := database.DB.Save(&bot).Error - if err != nil { - return err - } - - return nil -}