2025-04-27 11:06:41 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2025-04-27 14:13:23 +02:00
|
|
|
"github.com/gorcon/rcon"
|
2025-04-27 13:10:44 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"slices"
|
|
|
|
"syscall"
|
2025-04-27 11:06:41 +02:00
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Variables used for command line parameters
|
|
|
|
var (
|
2025-04-27 14:13:23 +02:00
|
|
|
Token string
|
|
|
|
MemberRole string
|
|
|
|
GuildID string
|
|
|
|
RCONServer string
|
|
|
|
RCONPassword string
|
2025-04-27 13:10:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Command vairables
|
|
|
|
var (
|
|
|
|
commands = []*discordgo.ApplicationCommand{
|
|
|
|
{
|
|
|
|
Name: "welcome",
|
|
|
|
Description: "Welcome a new member to the Server, giving them full membership permission.",
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Name: "user",
|
|
|
|
Description: "User to welcome as a new member.",
|
|
|
|
Type: discordgo.ApplicationCommandOptionUser,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2025-04-27 14:13:23 +02:00
|
|
|
{
|
|
|
|
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",
|
2025-04-27 14:21:53 +02:00
|
|
|
Description: "Is this a bedrock user?",
|
2025-04-27 14:13:23 +02:00
|
|
|
Type: discordgo.ApplicationCommandOptionBoolean,
|
|
|
|
Required: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2025-04-27 13:10:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
|
|
|
"welcome": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
2025-04-27 14:21:53 +02:00
|
|
|
caller, _ := s.GuildMember(i.GuildID, i.Member.User.ID)
|
|
|
|
|
|
|
|
// check if the user is a member
|
2025-04-27 13:10:44 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
options := i.ApplicationCommandData().Options
|
|
|
|
new := options[0].UserValue(s)
|
|
|
|
|
|
|
|
s.GuildMemberRoleAdd(i.GuildID, new.ID, MemberRole)
|
|
|
|
|
|
|
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: fmt.Sprintf("Welcome %s, you are now a member!", new.Mention()),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2025-04-27 14:13:23 +02:00
|
|
|
|
|
|
|
"whitelist": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
caller, err := s.GuildMember(i.GuildID, i.Member.User.ID)
|
2025-04-27 14:21:53 +02:00
|
|
|
|
|
|
|
// check if the user is a member
|
2025-04-27 14:13:23 +02:00
|
|
|
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
|
|
|
|
}
|
2025-04-27 14:21:53 +02:00
|
|
|
|
2025-04-27 14:13:23 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2025-04-27 14:21:53 +02:00
|
|
|
con.Close()
|
|
|
|
|
2025-04-27 14:13:23 +02:00
|
|
|
if err != nil {
|
|
|
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: "Ups! Something went wrong!",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-04-27 14:21:53 +02:00
|
|
|
log.Printf("Whitelisted %s, on behalf of %s.", name, i.Member.User.ID)
|
|
|
|
|
2025-04-27 14:13:23 +02:00
|
|
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: fmt.Sprintf("`%s` should now be whitelisted!", name),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2025-04-27 13:10:44 +02:00
|
|
|
}
|
2025-04-27 11:06:41 +02:00
|
|
|
)
|
|
|
|
|
2025-04-27 14:21:53 +02:00
|
|
|
// parse CLI arguments
|
2025-04-27 11:06:41 +02:00
|
|
|
func init() {
|
|
|
|
flag.StringVar(&Token, "t", "", "Bot Token")
|
2025-04-27 13:10:44 +02:00
|
|
|
flag.StringVar(&MemberRole, "r", "", "ID of the member role")
|
|
|
|
flag.StringVar(&GuildID, "g", "", "Id of the Guild")
|
2025-04-27 14:13:23 +02:00
|
|
|
flag.StringVar(&RCONServer, "rs", "", "RCON server to connect to")
|
|
|
|
flag.StringVar(&RCONPassword, "rp", "", "Password for the RCON server")
|
2025-04-27 13:10:44 +02:00
|
|
|
|
2025-04-27 11:06:41 +02:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2025-04-27 13:10:44 +02:00
|
|
|
bot, err := discordgo.New("Bot " + Token)
|
2025-04-27 11:06:41 +02:00
|
|
|
if err != nil {
|
2025-04-27 14:21:53 +02:00
|
|
|
log.Panicf("Could not create bot!")
|
2025-04-27 11:06:41 +02:00
|
|
|
}
|
|
|
|
|
2025-04-27 13:10:44 +02:00
|
|
|
bot.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
|
|
|
h(s, i)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2025-04-27 11:06:41 +02:00
|
|
|
// Open a websocket connection to Discord and begin listening.
|
2025-04-27 13:10:44 +02:00
|
|
|
err = bot.Open()
|
2025-04-27 11:06:41 +02:00
|
|
|
if err != nil {
|
2025-04-27 14:21:53 +02:00
|
|
|
log.Panicf("Error connecting to discord: %s", err)
|
2025-04-27 11:06:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-04-27 13:10:44 +02:00
|
|
|
log.Println("Adding commands...")
|
|
|
|
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
|
|
|
|
for i, v := range commands {
|
|
|
|
cmd, err := bot.ApplicationCommandCreate(bot.State.User.ID, GuildID, v)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
|
|
|
}
|
|
|
|
registeredCommands[i] = cmd
|
|
|
|
}
|
|
|
|
|
2025-04-27 14:21:53 +02:00
|
|
|
log.Println("Bot is now running. Press CTRL-C to exit.")
|
2025-04-27 13:10:44 +02:00
|
|
|
sc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
|
|
<-sc
|
|
|
|
|
2025-04-27 14:21:53 +02:00
|
|
|
log.Println("Exiting")
|
|
|
|
err = bot.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to shut down gracefully! %s", err)
|
|
|
|
}
|
2025-04-27 11:06:41 +02:00
|
|
|
}
|