63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/devawaves/snake-go/commands"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func main() {
|
|
err := godotenv.Load()
|
|
errorCheck(err)
|
|
token := os.Getenv("DISCORD_TOKEN")
|
|
|
|
discord, err := discordgo.New("Bot " + token)
|
|
errorCheck(err)
|
|
|
|
discord.AddHandler(messageCreate)
|
|
|
|
discord.Identify.Intents = discordgo.IntentsGuildMessages
|
|
err = discord.Open()
|
|
errorCheck(err)
|
|
|
|
fmt.Println("Bot is running...")
|
|
sc := make(chan os.Signal, 1)
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
<-sc
|
|
|
|
discord.Close()
|
|
}
|
|
|
|
func errorCheck(err error) {
|
|
if err != nil {
|
|
fmt.Println("er! " + err.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func messageCreate(session *discordgo.Session, msg *discordgo.MessageCreate) {
|
|
if msg.Author.ID == session.State.User.ID {
|
|
return
|
|
}
|
|
|
|
for _, command := range commands.Commands {
|
|
potential_command := strings.ToLower(msg.Content)
|
|
last_command_index := strings.Index(potential_command, " ")
|
|
|
|
if last_command_index == -1 {
|
|
last_command_index = len(potential_command)
|
|
}
|
|
|
|
potential_command = potential_command[:last_command_index]
|
|
|
|
if potential_command == "$" + command.Name {
|
|
command.Handler(session, msg)
|
|
}
|
|
}
|
|
}
|