initialize git
This commit is contained in:
commit
d17d31b293
6 changed files with 253 additions and 0 deletions
63
snake-go.go
Normal file
63
snake-go.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue