little clean up

This commit is contained in:
Morrígan 2025-04-27 14:21:53 +02:00
parent 179f2d8402
commit e864baea31
Signed by: morrigan
GPG key ID: CACB010F463A77D0

View file

@ -49,7 +49,7 @@ var (
}, },
{ {
Name: "bedrock", Name: "bedrock",
Description: "is this a bedrock user?", Description: "Is this a bedrock user?",
Type: discordgo.ApplicationCommandOptionBoolean, Type: discordgo.ApplicationCommandOptionBoolean,
Required: false, Required: false,
}, },
@ -59,10 +59,9 @@ var (
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"welcome": 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) caller, _ := 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) { if !slices.Contains(caller.Roles, MemberRole) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -88,9 +87,8 @@ var (
"whitelist": func(s *discordgo.Session, i *discordgo.InteractionCreate) { "whitelist": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
caller, err := s.GuildMember(i.GuildID, i.Member.User.ID) 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) { if !slices.Contains(caller.Roles, MemberRole) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -100,13 +98,12 @@ var (
}) })
return return
} }
opts := i.ApplicationCommandData().Options opts := i.ApplicationCommandData().Options
name := opts[0].StringValue() name := opts[0].StringValue()
bedrock := false bedrock := false
if len(opts) > 1 { if len(opts) > 1 {
bedrock = opts[1].BoolValue() bedrock = opts[1].BoolValue()
} }
con, err := rcon.Dial(RCONServer, RCONPassword) con, err := rcon.Dial(RCONServer, RCONPassword)
@ -127,9 +124,10 @@ var (
_, err = con.Execute(fmt.Sprintf("fwhitelist add %s", name)) _, err = con.Execute(fmt.Sprintf("fwhitelist add %s", name))
} else { } else {
_, err = con.Execute(fmt.Sprintf("whitelist add %s", name)) _, err = con.Execute(fmt.Sprintf("whitelist add %s", name))
} }
con.Close()
if err != nil { if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -140,19 +138,20 @@ var (
return return
} }
log.Printf("Whitelisted %s, on behalf of %s.", name, i.Member.User.ID)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("`%s` should now be whitelisted!", name), Content: fmt.Sprintf("`%s` should now be whitelisted!", name),
}, },
}) })
con.Close()
}, },
} }
) )
// parse CLI arguments
func init() { func init() {
flag.StringVar(&Token, "t", "", "Bot Token") flag.StringVar(&Token, "t", "", "Bot Token")
flag.StringVar(&MemberRole, "r", "", "ID of the member role") flag.StringVar(&MemberRole, "r", "", "ID of the member role")
flag.StringVar(&GuildID, "g", "", "Id of the Guild") flag.StringVar(&GuildID, "g", "", "Id of the Guild")
@ -165,7 +164,7 @@ func init() {
func main() { func main() {
bot, err := discordgo.New("Bot " + Token) bot, err := discordgo.New("Bot " + Token)
if err != nil { if err != nil {
fmt.Println("Thats not good!") log.Panicf("Could not create bot!")
} }
bot.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { bot.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
@ -177,7 +176,7 @@ func main() {
// Open a websocket connection to Discord and begin listening. // Open a websocket connection to Discord and begin listening.
err = bot.Open() err = bot.Open()
if err != nil { if err != nil {
fmt.Println("error opening connection,", err) log.Panicf("Error connecting to discord: %s", err)
return return
} }
@ -191,10 +190,14 @@ func main() {
registeredCommands[i] = cmd 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) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc <-sc
bot.Close() log.Println("Exiting")
err = bot.Close()
if err != nil {
log.Printf("Failed to shut down gracefully! %s", err)
}
} }