continued form command
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
[
|
||||
{
|
||||
"label": "This is a simple textline",
|
||||
"is_paragraph": false,
|
||||
"placeholder": "...and it is required!",
|
||||
"value": "",
|
||||
"required": true,
|
||||
"min_length": 0,
|
||||
"max_length": 20
|
||||
},
|
||||
{
|
||||
"label": "This is a paragraph",
|
||||
"is_paragraph": true,
|
||||
"placeholder": "...and it is not required!",
|
||||
"value": "We already have some input here",
|
||||
"required": false,
|
||||
"min_length": 0,
|
||||
"max_length": 2000
|
||||
}
|
||||
]
|
||||
{
|
||||
"form_id": "DO_NOT_USE_THE_SAME_ID_TWICE",
|
||||
"title": "Demo form",
|
||||
"form": [
|
||||
{
|
||||
"label": "This is a simple textline",
|
||||
"is_paragraph": false,
|
||||
"placeholder": "...and it is required!",
|
||||
"value": "",
|
||||
"required": true,
|
||||
"min_length": 0,
|
||||
"max_length": 20
|
||||
},
|
||||
{
|
||||
"label": "This is a paragraph",
|
||||
"is_paragraph": true,
|
||||
"placeholder": "...and it is not required!",
|
||||
"value": "We already have some input here",
|
||||
"required": false,
|
||||
"min_length": 0,
|
||||
"max_length": 2000
|
||||
}
|
||||
]
|
||||
}
|
||||
|
78
cmd_form.go
78
cmd_form.go
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var fileData []byte
|
||||
@@ -49,12 +50,35 @@ var form_command Command = Command{
|
||||
Name: "add",
|
||||
Description: "Adds existing forms to this channel",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionChannel,
|
||||
Name: "result_channel",
|
||||
Description: "Where the form results should appear",
|
||||
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText},
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "type",
|
||||
Description: "Which type of form you want to add",
|
||||
Autocomplete: true,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "title",
|
||||
Description: "The title the form should have",
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionChannel,
|
||||
Name: "accept_channel_id",
|
||||
Description: "Channel for results that need to be accepted by a moderator before sending it to the result channel",
|
||||
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText},
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionBoolean,
|
||||
Name: "mods_can_comment",
|
||||
Description: "Moderators can open a new channel on the form result, which then pings the user who submitted it",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -101,14 +125,16 @@ var form_command Command = Command{
|
||||
},
|
||||
})
|
||||
case "add":
|
||||
var title string
|
||||
var optionValue string
|
||||
if len(i.ApplicationCommandData().Options[0].Options) == 0 {
|
||||
optionValue = "template_general"
|
||||
var title, formID, overwriteTitle, acceptChannelID string
|
||||
var modsCanComment bool
|
||||
options := i.ApplicationCommandData().Options[0]
|
||||
|
||||
if len(options.Options) <= 1 {
|
||||
formID = "template_general"
|
||||
} else {
|
||||
optionValue = i.ApplicationCommandData().Options[0].Options[0].StringValue()
|
||||
formID = options.Options[1].StringValue()
|
||||
}
|
||||
switch optionValue {
|
||||
switch formID {
|
||||
case "template_feedback":
|
||||
title = "Submit Feedback"
|
||||
case "template_ticket":
|
||||
@@ -118,7 +144,26 @@ var form_command Command = Command{
|
||||
case "template_general":
|
||||
title = "Form"
|
||||
}
|
||||
s.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{
|
||||
|
||||
var exists bool = true
|
||||
formManageID := uuid.New()
|
||||
for exists {
|
||||
formManageID = uuid.New()
|
||||
exists = getFormManageIdExists(i.GuildID, formManageID)
|
||||
}
|
||||
|
||||
if !(len(options.Options) <= 2) {
|
||||
overwriteTitle = options.Options[2].StringValue()
|
||||
title = overwriteTitle
|
||||
}
|
||||
if !(len(options.Options) <= 3) {
|
||||
acceptChannelID = options.Options[3].ChannelValue(s).ID
|
||||
}
|
||||
if !(len(options.Options) <= 4) {
|
||||
modsCanComment = options.Options[4].BoolValue()
|
||||
}
|
||||
|
||||
message, _ := s.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{
|
||||
Embed: &discordgo.MessageEmbed{
|
||||
Color: hexToDecimal(color["primary"]),
|
||||
Title: title,
|
||||
@@ -128,7 +173,8 @@ var form_command Command = Command{
|
||||
discordgo.ActionsRow{
|
||||
Components: []discordgo.MessageComponent{
|
||||
discordgo.Button{
|
||||
CustomID: "form:", //add formID
|
||||
CustomID: "form:" + formManageID.String(),
|
||||
Style: discordgo.SuccessButton,
|
||||
Label: "Submit",
|
||||
Emoji: discordgo.ComponentEmoji{
|
||||
Name: "📥",
|
||||
@@ -138,6 +184,7 @@ var form_command Command = Command{
|
||||
},
|
||||
},
|
||||
})
|
||||
addFormButton(i.GuildID, i.ChannelID, message.ID, formManageID.String(), formID, options.Options[0].ChannelValue(s).ID, overwriteTitle, acceptChannelID, modsCanComment)
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
@@ -147,7 +194,7 @@ var form_command Command = Command{
|
||||
})
|
||||
}
|
||||
},
|
||||
ComponentIDs: getFormIDs(),
|
||||
ComponentIDs: getFormButtonIDs(),
|
||||
ComponentInteract: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if i.Interaction.MessageComponentData().CustomID == "form_demo" {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
@@ -228,6 +275,15 @@ var form_command Command = Command{
|
||||
}
|
||||
|
||||
func getFormIDs() []string {
|
||||
//needs databank support
|
||||
return []string{"form_demo"}
|
||||
//needs custom IDs from databank
|
||||
return []string{"form_demo", "template_feedback", "template_ticket", "template_url", "template_general"}
|
||||
}
|
||||
|
||||
func getFormButtonIDs() []string {
|
||||
var IDs []string = []string{"form_demo"}
|
||||
var formButtonIDs []string = getFormManageIDs()
|
||||
for _, buttonID := range formButtonIDs {
|
||||
IDs = append(IDs, "form:"+buttonID)
|
||||
}
|
||||
return IDs
|
||||
}
|
||||
|
15
main.go
15
main.go
@@ -25,12 +25,7 @@ func main() {
|
||||
godotenv.Load()
|
||||
|
||||
var err error
|
||||
connStr := "postgresql://" + os.Getenv("DB_USER") + ":" + url.QueryEscape(os.Getenv("DB_PASSWORD")) + "@" + os.Getenv("DB_SERVER") + ":" + string(os.Getenv("DB_PORT")) + "/" + os.Getenv("DB_NAME") + "?sslmode=disable"
|
||||
db, err = sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
initTables()
|
||||
|
||||
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
||||
if err != nil {
|
||||
fmt.Println("error creating Discord session,", err)
|
||||
@@ -47,7 +42,13 @@ func main() {
|
||||
fmt.Println("error opening connection,", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Bot is now running as \"%s\"!", discord.State.User.Username)
|
||||
connStr := "postgresql://" + os.Getenv("DB_USER") + ":" + url.QueryEscape(os.Getenv("DB_PASSWORD")) + "@" + os.Getenv("DB_SERVER") + ":" + string(os.Getenv("DB_PORT")) + "/" + os.Getenv("DB_NAME") + "?sslmode=disable&application_name=" + discord.State.User.Username
|
||||
db, err = sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
initTables()
|
||||
fmt.Printf("\nBot is now running as \"%s\"!", discord.State.User.Username)
|
||||
sc := make(chan os.Signal, 1)
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||
<-sc
|
||||
|
@@ -21,22 +21,23 @@ func initTables() {
|
||||
guild_id TEXT NOT NULL,
|
||||
PRIMARY KEY (channel_id, guild_id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS forms (
|
||||
CREATE TABLE IF NOT EXISTS custom_forms (
|
||||
form_id TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
json TEXT NOT NULL,
|
||||
json JSON NOT NULL,
|
||||
guild_id TEXT NOT NULL,
|
||||
PRIMARY KEY (form_id, guild_id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS formbuttons (
|
||||
CREATE TABLE IF NOT EXISTS form_buttons (
|
||||
form_manage_id TEXT NOT NULL,
|
||||
form_id TEXT NOT NULL,
|
||||
overwrite_title TEXT NOT NULL,
|
||||
overwrite_title TEXT,
|
||||
message_id TEXT NOT NULL,
|
||||
channel_id TEXT NOT NULL,
|
||||
guild_id TEXT NOT NULL,
|
||||
result_channel_id TEXT NOT NULL,
|
||||
accept_channel_id TEXT NOT NULL,
|
||||
accept_channel_id TEXT,
|
||||
mods_can_comment BOOL,
|
||||
PRIMARY KEY (form_manage_id, form_id)
|
||||
);
|
||||
`
|
||||
@@ -48,21 +49,17 @@ func initTables() {
|
||||
}
|
||||
|
||||
func addTag(guildID, tagName, tagContent string) bool {
|
||||
var exists bool
|
||||
var exists bool = true
|
||||
//TODO: add modify command
|
||||
id := uuid.New()
|
||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_id = $2)", guildID, id).Scan(&exists)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
// TODO: add modify command
|
||||
for exists {
|
||||
id = uuid.New()
|
||||
err = db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_id = $2)", guildID, id).Scan(&exists)
|
||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_id = $2)", guildID, id).Scan(&exists)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec("INSERT INTO tags (guild_id, tag_name, tag_content, tag_id) VALUES ($1, $2, $3, $4)", guildID, tagName, tagContent, id)
|
||||
_, err := db.Exec("INSERT INTO tags (guild_id, tag_name, tag_content, tag_id) VALUES ($1, $2, $3, $4)", guildID, tagName, tagContent, id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
@@ -180,6 +177,46 @@ func removeSticky(guildID string, channelID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func addTemplateForm(guildID string, channelID string, formID string, overwriteChannelID string, resultChannelID string, acceptChannelID string) {
|
||||
|
||||
func getFormManageIdExists(guildID string, id uuid.UUID) bool {
|
||||
var exists bool
|
||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM form_buttons WHERE guild_id = $1 AND form_manage_id = $2)", guildID, id).Scan(&exists)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
func addFormButton(guildID string, channelID string, messageID string, formManageID string, formID string, resultChannelID string, overwriteTitle string, acceptChannelID string, modsCanComment bool) {
|
||||
_, err := db.Exec("INSERT INTO form_buttons (guild_id, form_manage_id, channel_id, message_id, form_id, result_channel_id, overwrite_title, accept_channel_id, mods_can_comment) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", guildID, formManageID, channelID, messageID, formID, resultChannelID, overwriteTitle, acceptChannelID, modsCanComment)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getFormManageIDs() []string {
|
||||
if db == nil {
|
||||
return nil
|
||||
}
|
||||
var IDs []string
|
||||
rows, err := db.Query("SELECT form_manage_id FROM form_buttons")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
IDs = append(IDs, id)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
return IDs
|
||||
}
|
||||
|
Reference in New Issue
Block a user