This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
response.go
142 lines (121 loc) · 3.62 KB
/
response.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package dcmd
import (
"errors"
"fmt"
"github.com/jonas747/discordgo"
"github.com/jonas747/dutil"
"reflect"
"time"
)
type Response interface {
// Channel, session, command etc can all be found in this context
Send(data *Data) ([]*discordgo.Message, error)
}
func SendResponseInterface(data *Data, reply interface{}, escapeEveryoneMention bool) ([]*discordgo.Message, error) {
switch t := reply.(type) {
case Response:
return t.Send(data)
case string:
if t != "" {
if escapeEveryoneMention {
t = dutil.EscapeEveryoneMention(t)
}
return dutil.SplitSendMessage(data.Session, data.Msg.ChannelID, t)
}
return []*discordgo.Message{}, nil
case error:
if t != nil {
m := t.Error()
if escapeEveryoneMention {
m = dutil.EscapeEveryoneMention(m)
}
return dutil.SplitSendMessage(data.Session, data.Msg.ChannelID, m)
}
return []*discordgo.Message{}, nil
case *discordgo.MessageEmbed:
m, err := data.Session.ChannelMessageSendEmbed(data.Msg.ChannelID, t)
return []*discordgo.Message{m}, err
case []*discordgo.MessageEmbed:
msgs := make([]*discordgo.Message, len(t))
for i, embed := range t {
m, err := data.Session.ChannelMessageSendEmbed(data.Msg.ChannelID, embed)
if err != nil {
return msgs, err
}
msgs[i] = m
}
return msgs, nil
}
return nil, errors.New("Unknown reply type: " + reflect.TypeOf(reply).String() + " (Does not implement Response)")
}
// Temporary response deletes the inner response after Duration
type TemporaryResponse struct {
Response interface{}
Duration time.Duration
EscapeEveryone bool
}
func NewTemporaryResponse(d time.Duration, inner interface{}, escapeEveryoneMention bool) *TemporaryResponse {
return &TemporaryResponse{
Duration: d, Response: inner,
EscapeEveryone: escapeEveryoneMention,
}
}
func (t *TemporaryResponse) Send(data *Data) ([]*discordgo.Message, error) {
msgs, err := SendResponseInterface(data, t.Response, t.EscapeEveryone)
if err != nil {
return nil, err
}
time.AfterFunc(t.Duration, func() {
// do a bulk if 2 or more
if len(msgs) > 1 {
ids := make([]int64, len(msgs))
for i, m := range msgs {
ids[i] = m.ID
}
data.Session.ChannelMessagesBulkDelete(data.Msg.ChannelID, ids)
} else {
data.Session.ChannelMessageDelete(data.Msg.ChannelID, msgs[0].ID)
}
})
return msgs, nil
}
// The FallbackEmbed reponse type will turn the embed into a normal mesasge if there is not enough permissions
// This requires state member tracking enabled
type FallbackEmebd struct {
*discordgo.MessageEmbed
}
func (fe *FallbackEmebd) Send(data *Data) ([]*discordgo.Message, error) {
channelPerms, err := data.Session.State.UserChannelPermissions(data.Session.State.User.ID, data.Msg.ChannelID)
if err != nil {
return nil, err
}
if channelPerms&discordgo.PermissionEmbedLinks != 0 {
m, err := data.Session.ChannelMessageSendEmbed(data.Msg.ChannelID, fe.MessageEmbed)
if err != nil {
return nil, err
}
return []*discordgo.Message{m}, nil
}
content := StringEmbed(fe.MessageEmbed) + "\n*I have no 'embed links' permissions here, this is a fallback. it looks prettier if i have that perm :)*"
return dutil.SplitSendMessage(data.Session, data.Msg.ChannelID, content)
}
func StringEmbed(embed *discordgo.MessageEmbed) string {
body := ""
if embed.Author != nil {
body += embed.Author.Name + "\n"
body += embed.Author.URL + "\n"
}
if embed.Title != "" {
body += "**" + embed.Title + "**\n"
}
if embed.Description != "" {
body += embed.Description + "\n"
}
if body != "" {
body += "\n"
}
for _, v := range embed.Fields {
body += fmt.Sprintf("**%s**\n%s\n\n", v.Name, v.Value)
}
return body
}