From e864baea31c494b7aa8d1580960c8acc84dcbf42 Mon Sep 17 00:00:00 2001 From: morrigan Date: Sun, 27 Apr 2025 14:21:53 +0200 Subject: [PATCH] little clean up --- micobot.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/micobot.go b/micobot.go index 04cdd7d..62c892a 100644 --- a/micobot.go +++ b/micobot.go @@ -49,7 +49,7 @@ var ( }, { Name: "bedrock", - Description: "is this a bedrock user?", + Description: "Is this a bedrock user?", Type: discordgo.ApplicationCommandOptionBoolean, Required: false, }, @@ -59,10 +59,9 @@ var ( commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ "welcome": func(s *discordgo.Session, i *discordgo.InteractionCreate) { - caller, err := s.GuildMember(i.GuildID, i.Member.User.ID) - if err != nil { - fmt.Println("Could not get Guild Member,", err) - } + caller, _ := s.GuildMember(i.GuildID, i.Member.User.ID) + + // check if the user is a member if !slices.Contains(caller.Roles, MemberRole) { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -88,9 +87,8 @@ var ( "whitelist": func(s *discordgo.Session, i *discordgo.InteractionCreate) { caller, err := s.GuildMember(i.GuildID, i.Member.User.ID) - if err != nil { - fmt.Println("Could not get Guild Member,", err) - } + + // check if the user is a member if !slices.Contains(caller.Roles, MemberRole) { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -100,13 +98,12 @@ var ( }) return } + opts := i.ApplicationCommandData().Options name := opts[0].StringValue() - bedrock := false if len(opts) > 1 { bedrock = opts[1].BoolValue() - } con, err := rcon.Dial(RCONServer, RCONPassword) @@ -127,9 +124,10 @@ var ( _, err = con.Execute(fmt.Sprintf("fwhitelist add %s", name)) } else { _, err = con.Execute(fmt.Sprintf("whitelist add %s", name)) - } + con.Close() + if err != nil { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -140,19 +138,20 @@ var ( return } + log.Printf("Whitelisted %s, on behalf of %s.", name, i.Member.User.ID) + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: fmt.Sprintf("`%s` should now be whitelisted!", name), }, }) - con.Close() }, } ) +// parse CLI arguments func init() { - flag.StringVar(&Token, "t", "", "Bot Token") flag.StringVar(&MemberRole, "r", "", "ID of the member role") flag.StringVar(&GuildID, "g", "", "Id of the Guild") @@ -165,7 +164,7 @@ func init() { func main() { bot, err := discordgo.New("Bot " + Token) if err != nil { - fmt.Println("Thats not good!") + log.Panicf("Could not create bot!") } bot.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { @@ -177,7 +176,7 @@ func main() { // Open a websocket connection to Discord and begin listening. err = bot.Open() if err != nil { - fmt.Println("error opening connection,", err) + log.Panicf("Error connecting to discord: %s", err) return } @@ -191,10 +190,14 @@ func main() { registeredCommands[i] = cmd } - fmt.Println("Bot is now running. Press CTRL-C to exit.") + log.Println("Bot is now running. Press CTRL-C to exit.") sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc - bot.Close() + log.Println("Exiting") + err = bot.Close() + if err != nil { + log.Printf("Failed to shut down gracefully! %s", err) + } }