minecraft whitelist command
This commit is contained in:
parent
a3c55c8be8
commit
179f2d8402
3 changed files with 92 additions and 3 deletions
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ go 1.23
|
|||
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.28.1 // indirect
|
||||
github.com/gorcon/rcon v1.4.0 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,7 @@
|
|||
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
|
||||
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/gorcon/rcon v1.4.0 h1:pYwZ8Rhcgfh/LhdPBncecuEo5thoFvPIuMSWovz1FME=
|
||||
github.com/gorcon/rcon v1.4.0/go.mod h1:M6v6sNmr/NET9YIf+2rq+cIjTBridoy62uzQ58WgC1I=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||
|
|
86
micobot.go
86
micobot.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/gorcon/rcon"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
@ -17,6 +18,8 @@ var (
|
|||
Token string
|
||||
MemberRole string
|
||||
GuildID string
|
||||
RCONServer string
|
||||
RCONPassword string
|
||||
)
|
||||
|
||||
// Command vairables
|
||||
|
@ -34,6 +37,24 @@ var (
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "whitelist",
|
||||
Description: "Whitelist a user on the minecraft server.",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Name: "username",
|
||||
Description: "Username to whitelist",
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "bedrock",
|
||||
Description: "is this a bedrock user?",
|
||||
Type: discordgo.ApplicationCommandOptionBoolean,
|
||||
Required: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
||||
|
@ -64,6 +85,69 @@ 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)
|
||||
}
|
||||
if !slices.Contains(caller.Roles, MemberRole) {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Only Members are allowed to run this command",
|
||||
},
|
||||
})
|
||||
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)
|
||||
if err != nil {
|
||||
log.Printf("Could not connect to RCON server")
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Failed to whitelist, server may be down?",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err = nil
|
||||
|
||||
if bedrock {
|
||||
_, err = con.Execute(fmt.Sprintf("fwhitelist add %s", name))
|
||||
} else {
|
||||
_, err = con.Execute(fmt.Sprintf("whitelist add %s", name))
|
||||
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Ups! Something went wrong!",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("`%s` should now be whitelisted!", name),
|
||||
},
|
||||
})
|
||||
con.Close()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -72,6 +156,8 @@ 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")
|
||||
flag.StringVar(&RCONServer, "rs", "", "RCON server to connect to")
|
||||
flag.StringVar(&RCONPassword, "rp", "", "Password for the RCON server")
|
||||
|
||||
flag.Parse()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue