diff --git a/main.go b/main.go index af22fbf..2ce2ce0 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,72 @@ package banner import ( + "flag" "log" "net/http" "net/http/cookiejar" "os" + "os/signal" "time" + "github.com/bwmarrin/discordgo" "github.com/joho/godotenv" ) var ( // Base URL for all requests to the banner system - baseURL string - client http.Client - cookies http.CookieJar + baseURL string + client http.Client + cookies http.CookieJar + s *discordgo.Session + RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not") + commands = []*discordgo.ApplicationCommand{ + { + Name: "time", + Description: "Get Class Meeting Time", + }} + commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ + "time": func(s *discordgo.Session, i *discordgo.InteractionCreate) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + { + Title: "Permissions overview", + Description: "Overview of permissions for this command", + Fields: []*discordgo.MessageEmbedField{ + { + Name: "Users", + Value: "test", + }, + { + Name: "Channels", + Value: "test", + }, + { + Name: "Roles", + Value: "test", + }, + }, + }, + }, + AllowedMentions: &discordgo.MessageAllowedMentions{}, + }, + }) + }, + } ) +func init() { + + flag.Parse() + s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { + if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { + h(s, i) + } + }) +} + type MeetingTimeFaculty struct { bannerId int category string @@ -45,23 +95,69 @@ type MeetingTimeResponse struct { } func main() { - err := godotenv.Load() - if err != nil { + // Load environment variables + if err := godotenv.Load(); err != nil { log.Fatal("Error loading .env file") } - baseURL = os.Getenv("BANNER_BASE_URL") - cookies, err = cookiejar.New(nil) + + cookies, err := cookiejar.New(nil) if err != nil { log.Fatal(err) } - client = http.Client{ - Jar: cookies, - } - + client = http.Client{Jar: cookies} setup() - meetingTime := getCourseMeetingTime(202420, 44142) - log.Println(meetingTime) + s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { + log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator) + }) + err = s.Open() + if err != nil { + log.Fatalf("Cannot open the session: %v", err) + } + + s, err = discordgo.New("Bot " + os.Getenv("BOT_TOKEN")) + if err != nil { + log.Fatalf("Invalid bot parameters: %v", err) + } + + log.Println("Adding commands...") + registeredCommands := make([]*discordgo.ApplicationCommand, len(commands)) + for i, v := range commands { + cmd, err := s.ApplicationCommandCreate(s.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v) + if err != nil { + log.Panicf("Cannot create '%v' command: %v", v.Name, err) + } + registeredCommands[i] = cmd + } + + defer s.Close() + + stop := make(chan os.Signal, 1) + signal.Notify(stop, os.Interrupt) + log.Println("Press Ctrl+C to exit") + <-stop + + if *RemoveCommands { + log.Println("Removing commands...") + // // We need to fetch the commands, since deleting requires the command ID. + // // We are doing this from the returned commands on line 375, because using + // // this will delete all the commands, which might not be desirable, so we + // // are deleting only the commands that we added. + // registeredCommands, err := s.ApplicationCommands(s.State.User.ID, *GuildID) + // if err != nil { + // log.Fatalf("Could not fetch registered commands: %v", err) + // } + + for _, v := range registeredCommands { + err := s.ApplicationCommandDelete(s.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v.ID) + if err != nil { + log.Panicf("Cannot delete '%v' command: %v", v.Name, err) + } + } + } + + log.Println("Gracefully shutting down.") + }