HwidBot/main.go

290 lines
7.5 KiB
Go

package main
import (
"database/sql"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
_ "github.com/lib/pq"
)
var (
config Config
client *discordgo.Session
db *sql.DB
globalCategoryID string
)
type Config struct {
Discord Discord `toml:"discord"`
Database Database `toml:"database"`
}
type Discord struct {
Token string `toml:"token"`
AppID string `toml:"appid"`
GuildID string `toml:"guildid"`
}
type Database struct {
Host string `toml:"host"`
Port int `toml:"port"`
Name string `toml:"name"`
Username string `toml:"username"`
Password string `toml:"password"`
}
func loadConfig(filename string) (Config, error) {
var config Config
_, err := toml.DecodeFile(filename, &config)
return config, err
}
func connectDb(config Config) (*sql.DB, error) {
connectionString := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
config.Database.Host,
config.Database.Port,
config.Database.Username,
config.Database.Password,
config.Database.Name,
)
db, err := sql.Open("postgres", connectionString)
if err != nil {
return nil, fmt.Errorf("error connecting to the database: %v", err)
}
err = db.Ping()
if err != nil {
db.Close()
return nil, fmt.Errorf("error pinging the database: %v", err)
}
log.Println("Successfully connected to the database.")
return db, nil
}
func init() {
var err error
config, err = loadConfig("config.toml")
if err != nil {
log.Println("Error occurred whilst trying to load config:", err)
return
}
client, err = discordgo.New("Bot " + config.Discord.Token)
if err != nil {
log.Println("Error initializing bot:", err)
return
}
db, err = connectDb(config)
if err != nil {
log.Println("Error initializing db connection:", err)
return
}
}
var (
commands = []discordgo.ApplicationCommand{
{
Name: "setup",
Description: "Setup a channel for ticket creation",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Channel for ticket creation",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "category",
Description: "Category for tickets",
Required: true,
},
},
},
}
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
"setup": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
channelOption := i.ApplicationCommandData().Options[0].ChannelValue(client)
categoryOption := i.ApplicationCommandData().Options[1].ChannelValue(client)
globalCategoryID = categoryOption.ID
_, err := client.ChannelMessageSendComplex(channelOption.ID, &discordgo.MessageSend{
Content: "Click the button below to request a HWID reset:",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Request Reset",
Style: discordgo.PrimaryButton,
CustomID: "create_ticket",
},
},
},
},
})
if err != nil {
log.Println("Error sending message to the specified channel:", err)
}
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Setup completed! Request creation button has been added to %s", channelOption.Mention()),
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
},
}
componentsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
"create_ticket": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "For what would u like a HWID reset:",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.SelectMenu{
CustomID: "select_software_type",
Placeholder: "Choose which software",
Options: []discordgo.SelectMenuOption{
{
Label: "Vanity",
Value: "vanity",
Description: "Request a vanity reset",
},
{
Label: "Mesa",
Value: "mesa",
Description: "Request a mesa reset",
},
},
},
},
},
},
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
},
"select_software_type": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
selectedOption := i.MessageComponentData().Values[0]
var softwareType string
if selectedOption == "vanity" {
softwareType = "Vanity"
} else if selectedOption == "mesa" {
softwareType = "Mesa"
}
guildID := i.GuildID
userID := i.Member.User.ID
userName := i.Member.User.Username
channel, err := client.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{
Name: fmt.Sprintf("reset-%s-%s", softwareType, userName),
Type: discordgo.ChannelTypeGuildText,
ParentID: globalCategoryID,
PermissionOverwrites: []*discordgo.PermissionOverwrite{
{
ID: guildID,
Type: discordgo.PermissionOverwriteTypeRole,
Deny: discordgo.PermissionViewChannel,
},
{
ID: userID,
Type: discordgo.PermissionOverwriteTypeMember,
Allow: discordgo.PermissionViewChannel,
},
},
})
if err != nil {
log.Println("Error creating hwid request channel:", err)
return
}
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Request created: %s", channel.Mention()),
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
},
}
)
func main() {
if client == nil {
log.Println("Bot client is not initialized")
return
}
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is online")
cmdIDs := make(map[string]string, len(commands))
for _, cmd := range commands {
rcmd, err := client.ApplicationCommandCreate(client.State.User.ID, config.Discord.GuildID, &cmd)
if err != nil {
log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
}
cmdIDs[rcmd.ID] = rcmd.Name
}
})
client.AddHandler(func(client *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type == discordgo.InteractionApplicationCommand {
if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
h(client, i)
}
} else if i.Type == discordgo.InteractionMessageComponent {
if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
h(client, i)
}
}
})
err := client.Open()
if err != nil {
log.Println("Error opening connection:", err)
return
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
log.Println("Gracefully shutting down.")
client.Close()
if db != nil {
db.Close()
}
}