Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions commands/commands.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package commands

import (
"fmt"

"github.com/bwmarrin/discordgo"
)

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",
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
15 changes: 8 additions & 7 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,27 @@ 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 {
fmt.Println(err)
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")
Expand All @@ -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 {
Expand Down