package main import ( "io" "log" "net/http" "github.com/bwmarrin/discordgo" ) var client = &http.Client{} type CommandFunction func(s *discordgo.Session, m *discordgo.MessageCreate) func InitCommands(s *discordgo.Session) { s.AddHandler(commandHandler("greet", greetCommand)) s.AddHandler(commandHandler("dadjoke", dadJokeCommand)) s.AddHandler(commandHandler("hibub", hiBubCommand)) } func commandHandler(commandName string, handler CommandFunction) func(s *discordgo.Session, m *discordgo.MessageCreate) { return func(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Content != "!"+commandName { return } handler(s, m) } } func greetCommand(s *discordgo.Session, m *discordgo.MessageCreate) { greeting := `Hello! I'm Bub! I am a bot created for the Hubbub. I was created by RageCage. Ask him for more info if you're interested in looking at my source code!` s.ChannelMessageSend(m.ChannelID, greeting) } func dadJokeCommand(s *discordgo.Session, m *discordgo.MessageCreate) { errorMessage := "I couldn't get a dad joke! Tell RageCage to check on me. :)" req, err := http.NewRequest("GET", "https://icanhazdadjoke.com/", nil) if err != nil { log.Println(err) s.ChannelMessageSend(m.ChannelID, errorMessage) return } req.Header.Add("Accept", "text/plain") jokeResponse, err := client.Do(req) if err != nil { log.Println(err) s.ChannelMessageSend(m.ChannelID, errorMessage) return } defer jokeResponse.Body.Close() jokeBytes, err := io.ReadAll(jokeResponse.Body) if err != nil { log.Println(err) s.ChannelMessageSend(m.ChannelID, errorMessage) return } joke := string(jokeBytes) log.Println(joke) s.ChannelMessageSend(m.ChannelID, joke) } func hiBubCommand(s *discordgo.Session, m *discordgo.MessageCreate) { user := m.Author hibub := "Hi " + user.Username + "it's Bub from the Hubbub, how are you?" s.ChannelMessageSend(m.ChannelID, hibub) }