forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.go
83 lines (76 loc) · 2.36 KB
/
poll.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
package poll
import (
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"github.com/pkg/errors"
)
var (
pollReactions = [...]string{"1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣", "🔟"}
Command = &commands.YAGCommand{
CmdCategory: commands.CategoryTool,
Name: "Poll",
Description: "Create a reaction poll.",
RequiredArgs: 3,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{
Name: "Topic",
Type: dcmd.String,
Help: "Description of the poll",
},
&dcmd.ArgDef{Name: "Option1", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option2", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option3", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option4", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option5", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option6", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option7", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option8", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option9", Type: dcmd.String},
&dcmd.ArgDef{Name: "Option10", Type: dcmd.String},
},
RunFunc: createPoll,
}
)
func createPoll(data *dcmd.Data) (interface{}, error) {
topic := data.Args[0].Str()
options := data.Args[1:]
for i, option := range options {
if option.Str() == "" || i >= len(pollReactions) {
options = options[:i]
break
}
}
var description string
for i, option := range options {
if i != 0 {
description += "\n"
}
description += pollReactions[i] + " " + option.Str()
}
author := data.Msg.Author
authorName := author.Username
if member, err := bot.GetMember(data.GS.ID, author.ID); err == nil && member.Nick != "" {
authorName = member.Nick
}
response := discordgo.MessageEmbed{
Title: topic,
Description: description,
Color: 0x65f442,
Author: &discordgo.MessageEmbedAuthor{
Name: authorName,
IconURL: discordgo.EndpointUserAvatar(author.ID, author.Avatar),
},
}
common.BotSession.ChannelMessageDelete(data.Msg.ChannelID, data.Msg.ID)
pollMsg, err := common.BotSession.ChannelMessageSendEmbed(data.Msg.ChannelID, &response)
if err != nil {
return nil, errors.Wrap(err, "failed to add poll description")
}
for i, _ := range options {
common.BotSession.MessageReactionAdd(pollMsg.ChannelID, pollMsg.ID, pollReactions[i])
}
return nil, nil
}