diff --git a/cmd_autojoinroles.go b/cmd_autojoinroles.go new file mode 100644 index 0000000..56e8a14 --- /dev/null +++ b/cmd_autojoinroles.go @@ -0,0 +1,54 @@ +package main + +import "github.com/bwmarrin/discordgo" + +var autojoinroles_command Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "autojoinroles", + Description: "Give users a role when they join", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: "bot", + Description: "Give bots a role when they join (Leave empty to remove current)", + Options: []*discordgo.ApplicationCommandOption{ + { + + Type: discordgo.ApplicationCommandOptionRole, + Name: "role", + Description: "The role bots should get when they join the server", + }, + }, + }, + { + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: "user", + Description: "Give users a role when they join (Leave empty to remove current)", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionRole, + Name: "role", + Description: "The role users should get when they join the server", + }}, + }, + }, + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + option := i.ApplicationCommandData().Options[0].Name + role := i.ApplicationCommandData().Options[0].Options[0].RoleValue(s, i.GuildID).ID + var content string + if setAutoJoinRole(i.GuildID, option, role) { + content = "Setup auto join role for " + option + "s as <@&" + role + ">" + } else { + content = "Updated auto join role for " + option + "s as <@&" + role + ">" + } + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: content, + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + + }, +} diff --git a/cmd_form.go b/cmd_form.go index a147b11..6f9d64c 100644 --- a/cmd_form.go +++ b/cmd_form.go @@ -133,7 +133,7 @@ var form_command Command = Command{ for _, opt := range options.Options { switch opt.Name { case "type": - formID = options.Options[1].StringValue() + formID = opt.StringValue() case "title": overwriteTitle = opt.StringValue() title = overwriteTitle diff --git a/manage_data.go b/manage_data.go index 55ec605..e9049d8 100644 --- a/manage_data.go +++ b/manage_data.go @@ -40,6 +40,12 @@ func initTables() { mods_can_comment BOOL, PRIMARY KEY (form_manage_id, form_type) ); + CREATE TABLE IF NOT EXISTS autojoinroles ( + guild_id TEXT NOT NULL, + bot_role TEXT, + user_role TEXT, + PRIMARY KEY (guild_id) + ) ` _, err := db.Exec(createTableQuery) @@ -229,3 +235,23 @@ func getFormType(formManageID string) string { } return formType } + +func setAutoJoinRole(guildID string, option string, roleID string) bool { + var exists bool + err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM autojoinroles WHERE guild_id = $1)", guildID).Scan(&exists) + if err != nil { + log.Println(err) + } + if exists { + _, err = db.Exec("UPDATE autojoinroles SET "+option+"_role = $1 WHERE guild_id = $2", roleID, guildID) + if err != nil { + log.Println(err) + } + } else { + _, err = db.Exec("INSERT INTO autojoinroles (guild_id, "+option+"_role) VALUES ($1, $2)", guildID, roleID) + if err != nil { + log.Println(err) + } + } + return exists +} diff --git a/register_commands.go b/register_commands.go index 8d17280..a4d3e41 100644 --- a/register_commands.go +++ b/register_commands.go @@ -19,7 +19,7 @@ type Command struct { ModalIDs []string } -var commands []Command = []Command{form_command, tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command, cat_command} +var commands []Command = []Command{form_command, tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command, cat_command, autojoinroles_command} func ready(s *discordgo.Session, event *discordgo.Ready) { fmt.Print("\nStarting up...") diff --git a/tool.go b/tool.go new file mode 100644 index 0000000..cb202b4 --- /dev/null +++ b/tool.go @@ -0,0 +1,24 @@ +package main + +import "encoding/json" + +type ModalJsonField struct { + Label string `json:"label"` + IsParagraph bool `json:"is_paragraph"` + Value string `json:"value"` + Required bool `json:"required"` + MinLength int `json:"min_length"` + MaxLength int `json:"max_length"` +} + +type ModalJson struct { + FormType string `json:"form_type"` + Title string `json:"title"` + Form []ModalJsonField `json:"form"` +} + +func jsonStringShowModal(jsonString string, id string) { + var modal ModalJson + json.Unmarshal([]byte(jsonString), &modal) + +}