mirror of
https://github.com/Xevion/banner.git
synced 2025-12-13 00:11:01 -06:00
Fix main setup, use package main, working bot run
This commit is contained in:
105
main.go
105
main.go
@@ -1,4 +1,4 @@
|
|||||||
package banner
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
@@ -15,58 +15,17 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
// Base URL for all requests to the banner system
|
// Base URL for all requests to the banner system
|
||||||
baseURL string
|
baseURL string
|
||||||
client http.Client
|
client http.Client
|
||||||
cookies http.CookieJar
|
cookies http.CookieJar
|
||||||
s *discordgo.Session
|
session *discordgo.Session
|
||||||
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
|
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
|
||||||
commands = []*discordgo.ApplicationCommand{
|
integerOptionMinValue = 0.0
|
||||||
{
|
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
||||||
Name: "time",
|
"time": TimeCommandHandler,
|
||||||
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 {
|
type MeetingTimeFaculty struct {
|
||||||
bannerId int
|
bannerId int
|
||||||
category string
|
category string
|
||||||
@@ -107,32 +66,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client = http.Client{Jar: cookies}
|
client = http.Client{Jar: cookies}
|
||||||
setup()
|
setup(cookies)
|
||||||
|
|
||||||
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
session, err = discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
||||||
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 {
|
if err != nil {
|
||||||
log.Fatalf("Invalid bot parameters: %v", err)
|
log.Fatalf("Invalid bot parameters: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
||||||
|
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
|
||||||
|
})
|
||||||
|
err = session.Open()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Cannot open the session: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
session.AddHandler(func(internalSession *discordgo.Session, interaction *discordgo.InteractionCreate) {
|
||||||
|
if handler, ok := commandHandlers[interaction.ApplicationCommandData().Name]; ok {
|
||||||
|
handler(internalSession, interaction)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
log.Println("Adding commands...")
|
log.Println("Adding commands...")
|
||||||
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
|
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
|
||||||
for i, v := range commands {
|
for i, v := range commandDefinitions {
|
||||||
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v)
|
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
||||||
}
|
}
|
||||||
registeredCommands[i] = cmd
|
registeredCommands[i] = cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
defer s.Close()
|
defer session.Close()
|
||||||
|
|
||||||
stop := make(chan os.Signal, 1)
|
stop := make(chan os.Signal, 1)
|
||||||
signal.Notify(stop, os.Interrupt)
|
signal.Notify(stop, os.Interrupt)
|
||||||
@@ -140,18 +105,10 @@ func main() {
|
|||||||
<-stop
|
<-stop
|
||||||
|
|
||||||
if *RemoveCommands {
|
if *RemoveCommands {
|
||||||
log.Println("Removing commands...")
|
log.Printf("Removing %d command%s...\n", len(registeredCommands), Plural(len(registeredCommands)))
|
||||||
// // 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 {
|
for _, v := range registeredCommands {
|
||||||
err := s.ApplicationCommandDelete(s.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v.ID)
|
err := session.ApplicationCommandDelete(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
|
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user