2023-12-26 18:39:21 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-21 00:27:52 -07:00
|
|
|
"himbot/command"
|
2023-12-26 18:39:21 -07:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-10-21 23:50:17 -06:00
|
|
|
"syscall"
|
2023-12-26 18:39:21 -07:00
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
"github.com/bwmarrin/discordgo"
|
2023-12-26 18:39:21 -07:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
var (
|
|
|
|
commands = []*discordgo.ApplicationCommand{
|
|
|
|
{
|
|
|
|
Name: "ping",
|
|
|
|
Description: "ping pong!",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hs",
|
|
|
|
Description: "This command was your nickname in highschool!",
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
Name: "nickname",
|
|
|
|
Description: "Your nickname in highschool.",
|
|
|
|
Required: true,
|
|
|
|
},
|
2023-12-26 18:39:21 -07:00
|
|
|
},
|
|
|
|
},
|
2024-10-22 00:01:19 -06:00
|
|
|
{
|
|
|
|
Name: "markov",
|
|
|
|
Description: "Testing markov chains.",
|
|
|
|
},
|
2024-10-21 23:50:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
2024-10-22 00:01:19 -06:00
|
|
|
"ping": command.PingCommand,
|
|
|
|
"hs": command.HsCommand,
|
|
|
|
"markov": command.MarkovCommand,
|
2024-10-21 23:50:17 -06:00
|
|
|
}
|
|
|
|
)
|
2023-12-26 18:39:21 -07:00
|
|
|
|
|
|
|
func main() {
|
2024-10-20 22:45:16 -06:00
|
|
|
godotenv.Load(".env")
|
2023-12-26 18:39:21 -07:00
|
|
|
|
|
|
|
token := os.Getenv("DISCORD_TOKEN")
|
2024-03-31 13:44:49 -06:00
|
|
|
|
2024-10-20 22:45:16 -06:00
|
|
|
if token == "" {
|
|
|
|
log.Fatalln("No $DISCORD_TOKEN given.")
|
2023-12-26 18:39:21 -07:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
dg, err := discordgo.New("Bot " + token)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error creating Discord session: %v", err)
|
2023-12-26 18:39:21 -07:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
dg.AddHandler(ready)
|
|
|
|
dg.AddHandler(interactionCreate)
|
|
|
|
|
|
|
|
dg.Identify.Intents = discordgo.IntentsGuilds
|
2023-12-26 18:39:21 -07:00
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
err = dg.Open()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error opening connection: %v", err)
|
2023-12-26 18:39:21 -07:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
log.Println("Bot is now running. Press CTRL-C to exit.")
|
|
|
|
registerCommands(dg)
|
|
|
|
|
|
|
|
sc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
|
|
<-sc
|
|
|
|
|
|
|
|
dg.Close()
|
2023-12-26 18:39:21 -07:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
|
|
|
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
|
|
|
|
}
|
2023-12-26 18:39:21 -07:00
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
|
|
|
h(s, i)
|
|
|
|
}
|
|
|
|
}
|
2023-12-26 18:39:21 -07:00
|
|
|
|
2024-10-21 23:50:17 -06:00
|
|
|
func registerCommands(s *discordgo.Session) {
|
|
|
|
log.Println("Registering commands...")
|
|
|
|
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
|
|
|
|
for i, v := range commands {
|
|
|
|
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "", v)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
|
|
|
}
|
|
|
|
registeredCommands[i] = cmd
|
|
|
|
}
|
2023-12-26 18:39:21 -07:00
|
|
|
}
|