/
updatepoll.go
128 lines (113 loc) · 3.37 KB
/
updatepoll.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package polls
import (
"errors"
"github.com/bwmarrin/discordgo"
"github.com/lordralex/absol/api/database"
"github.com/lordralex/absol/api/logger"
"gorm.io/gorm"
)
var updatePollOperation = &discordgo.ApplicationCommand{
Name: "updatepoll",
Description: "Update a poll with a bunch of options",
Type: discordgo.ChatApplicationCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "id",
Description: "ID for the poll",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
},
{
Name: "title",
Description: "Title for the poll",
Type: discordgo.ApplicationCommandOptionString,
Required: false,
},
{
Name: "description",
Description: "Full information about the poll",
Type: discordgo.ApplicationCommandOptionString,
Required: false,
},
{
Name: "description-file",
Description: "Full information about the poll (based from a file)",
Type: discordgo.ApplicationCommandOptionAttachment,
Required: false,
},
},
}
func runUpdateCommand(ds *discordgo.Session, i *discordgo.InteractionCreate) {
_ = ds.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{Flags: discordgo.MessageFlagsEphemeral},
})
commandData := i.ApplicationCommandData()
var messageId string
for _, v := range commandData.Options {
if v.Name == "id" {
messageId = v.StringValue()
break
}
}
msg := "Unable to get poll"
originalMessage, err := ds.ChannelMessage(i.ChannelID, messageId)
if err != nil {
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
return
}
db, err := database.Get()
if err != nil {
logger.Err().Println(err.Error())
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
return
}
poll := &Poll{MessageId: messageId}
err = db.Where(poll).First(poll).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logger.Err().Println(err.Error())
}
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
return
}
if poll.Closed {
msg = "Poll is already closed"
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
return
}
edit := discordgo.NewMessageEdit(originalMessage.ChannelID, originalMessage.ID)
edit.Components = originalMessage.Components
edit.Embeds = originalMessage.Embeds
title := edit.Embeds[0].Title
description := edit.Embeds[0].Description
for _, v := range commandData.Options {
switch v.Name {
case "title":
{
title = v.StringValue()
}
case "description":
{
description = v.StringValue()
}
case "description-file":
{
fileId := v.Value.(string)
description, err = downloadFile(commandData.Resolved.Attachments[fileId].URL)
if err != nil {
msg = "Error downloading file: " + err.Error()
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
return
}
}
}
}
edit.Embeds[0].Title = title
edit.Embeds[0].Description = description
poll.Title = title
_ = db.Save(poll).Error
_, _ = ds.ChannelMessageEditComplex(edit)
msg = "Poll updated"
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
}