diff --git a/commands/commands.go b/commands/commands.go index 4bdc7f6..f0d0b84 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -1,6 +1,8 @@ package commands import ( + "fmt" + "github.com/bwmarrin/discordgo" ) @@ -8,6 +10,16 @@ var Commands []*discordgo.ApplicationCommand var CommandHandlers = map[string]func(client *discordgo.Session, interaction *discordgo.InteractionCreate){} func RegisterCommands(client *discordgo.Session, TestGuildId string) { + botID := client.State.User.ID + if botID == "" { + me, err := client.User("@me") + if err != nil { + fmt.Println("Unable to determine bot user for command registration:", err) + return + } + botID = me.ID + } + Commands = []*discordgo.ApplicationCommand{ { Name: "ping", @@ -103,13 +115,13 @@ func RegisterCommands(client *discordgo.Session, TestGuildId string) { registeredCommands := make([]*discordgo.ApplicationCommand, len(Commands)) for i, command := range Commands { if TestGuildId == "" { - registeredCommand, err := client.ApplicationCommandCreate(client.State.User.ID, "", command) + registeredCommand, err := client.ApplicationCommandCreate(botID, "", command) if err != nil { println("Error adding command:", err) } registeredCommands[i] = registeredCommand } else { - registeredCommand, err := client.ApplicationCommandCreate(client.State.User.ID, TestGuildId, command) + registeredCommand, err := client.ApplicationCommandCreate(botID, TestGuildId, command) if err != nil { println("Error adding command:", err) } diff --git a/main.go b/main.go index d966ded..49ce03c 100644 --- a/main.go +++ b/main.go @@ -14,23 +14,29 @@ import ( var Client *discordgo.Session func main() { - ConfigErr, config := utils.LoadConfig() - if ConfigErr != nil { + config, err := utils.LoadConfig() + if err != nil { return } - Client, err := discordgo.New("Bot " + config.BotToken) + Client, err = discordgo.New("Bot " + config.BotToken) if err != nil { fmt.Println("error starting Discord bot,", err) return } + Client.Identify.Intents = discordgo.IntentsGuilds | + discordgo.IntentsGuildMessages | + discordgo.IntentsGuildMessageReactions | + discordgo.IntentsMessageContent + + events.RegisterEvents(Client) + err = Client.Open() if err != nil { fmt.Println("error opening connection to Discord,", err) return } - events.RegisterEvents(Client) commands.RegisterCommands(Client, config.TestGuildId) fmt.Println("Bot is now running. Press CTRL-C to exit.") diff --git a/utils/config.go b/utils/config.go index 490f36d..7d89356 100644 --- a/utils/config.go +++ b/utils/config.go @@ -24,10 +24,14 @@ var Config ConfigType var RedisCtx = context.Background() var RedisClient *redis.Client -func LoadConfig() (*error, *ConfigType) { +func LoadConfig() (*ConfigType, error) { // Get from config.json configFile, err := os.Open("config.json") + if err != nil { + fmt.Println(err.Error() + " - Please create a config.json file or repair it.") + return nil, err + } defer func(configFile *os.File) { err := configFile.Close() if err != nil { @@ -35,15 +39,12 @@ func LoadConfig() (*error, *ConfigType) { fmt.Println("error closing config file") } }(configFile) - if err != nil { - fmt.Println(err.Error() + " - Please create a config.json file or repair it.") - return &err, nil - } + jsonParser := json.NewDecoder(configFile) err = jsonParser.Decode(&Config) if err != nil { fmt.Println(err.Error() + " - Please create a config.json file or repair it.") - return &err, nil + return nil, err } fmt.Println("Loaded config.json") @@ -54,7 +55,7 @@ func LoadConfig() (*error, *ConfigType) { DB: Config.RedisDb, // use default DB }) - return nil, &Config + return &Config, nil } func ReadJsonFile(path string, target interface{}) error {