1
|
|
|
package bot |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"io" |
6
|
|
|
|
7
|
|
|
"github.com/bwmarrin/discordgo" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// NewEmbedStruct generated embed |
11
|
|
|
type NewEmbedStruct struct { |
12
|
|
|
*discordgo.MessageSend |
13
|
|
|
embLength int |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
func truncateText(text string, length int) string { |
17
|
|
|
if length > 3 && len(text) > length && len(text) > 3 { |
18
|
|
|
text = text[:length-3] + "..." |
19
|
|
|
} |
20
|
|
|
return text |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// NewEmbed creates new embed |
24
|
|
|
func NewEmbed(title string) *NewEmbedStruct { |
25
|
|
|
title = truncateText(title, 256) |
26
|
|
|
return &NewEmbedStruct{&discordgo.MessageSend{Embed: &discordgo.MessageEmbed{Title: title}}, len(title)} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// CheckLength returns true if length of embed chars less then 6000 |
30
|
|
|
func (emb *NewEmbedStruct) CheckLength(newLength int) bool { |
31
|
|
|
if emb.embLength + newLength <= 6000 { |
32
|
|
|
return true |
33
|
|
|
} |
34
|
|
|
return false |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Field adds field to embed |
38
|
|
|
func (emb *NewEmbedStruct) Field(name, value string, inline bool) *NewEmbedStruct { |
39
|
|
|
if len(name) > 0 && len(value) > 0 { |
40
|
|
|
name = truncateText(name, 256) |
41
|
|
|
value = truncateText(value, 1024) |
42
|
|
|
newLength := len(name + value) |
43
|
|
|
if emb.CheckLength(newLength) { |
44
|
|
|
emb.Embed.Fields = append(emb.Embed.Fields, |
45
|
|
|
&discordgo.MessageEmbedField{ |
46
|
|
|
Name: name, |
47
|
|
|
Value: value, |
48
|
|
|
Inline: inline}) |
49
|
|
|
emb.embLength += newLength |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return emb |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// TimeStamp adds timestamp to footer of embed |
56
|
|
|
func (emb *NewEmbedStruct) TimeStamp(ts string) *NewEmbedStruct { |
57
|
|
|
emb.Embed.Timestamp = ts |
58
|
|
|
return emb |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Author adds author to embed |
62
|
|
|
func (emb *NewEmbedStruct) Author(name, url, iconURL string) *NewEmbedStruct { |
63
|
|
|
name = truncateText(name, 256) |
64
|
|
|
newLength := len(name) |
65
|
|
|
if emb.CheckLength(newLength) { |
66
|
|
|
emb.Embed.Author = &discordgo.MessageEmbedAuthor{URL: url, Name: name, IconURL: iconURL} |
67
|
|
|
emb.embLength += newLength |
68
|
|
|
} |
69
|
|
|
return emb |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Desc adds description to embed |
73
|
|
|
func (emb *NewEmbedStruct) Desc(desc string) *NewEmbedStruct { |
74
|
|
|
if len(desc) > 0 { |
75
|
|
|
desc = truncateText(desc, 2048) |
76
|
|
|
newLength := len(desc) |
77
|
|
|
if emb.CheckLength(newLength) { |
78
|
|
|
emb.Embed.Description = desc |
79
|
|
|
emb.embLength += newLength |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
return emb |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// URL adds url to embed description |
86
|
|
|
func (emb *NewEmbedStruct) URL(url string) *NewEmbedStruct { |
87
|
|
|
emb.Embed.URL = url |
88
|
|
|
return emb |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Footer adds footer text |
92
|
|
|
func (emb *NewEmbedStruct) Footer(text string) *NewEmbedStruct { |
93
|
|
|
text = truncateText(text, 2048) |
94
|
|
|
newLength := len(text) |
95
|
|
|
if emb.CheckLength(newLength) { |
96
|
|
|
emb.Embed.Footer = &discordgo.MessageEmbedFooter{Text: text} |
97
|
|
|
emb.embLength += newLength |
98
|
|
|
} |
99
|
|
|
return emb |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Color adds color to embed |
103
|
|
|
func (emb *NewEmbedStruct) Color(color int) *NewEmbedStruct { |
104
|
|
|
emb.Embed.Color = color |
105
|
|
|
return emb |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// AttachImg adds attached image to embed from io.Reader |
109
|
|
|
func (emb *NewEmbedStruct) AttachImg(name string, file io.Reader) *NewEmbedStruct { |
110
|
|
|
emb.Embed.Image = &discordgo.MessageEmbedImage{URL: "attachment://" + name} |
111
|
|
|
emb.Files = append(emb.Files, &discordgo.File{Name: name, Reader: file}) |
112
|
|
|
return emb |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// AttachImgURL adds attached image to embed from url |
116
|
|
|
func (emb *NewEmbedStruct) AttachImgURL(url string) *NewEmbedStruct { |
117
|
|
|
emb.Embed.Image = &discordgo.MessageEmbedImage{URL: url} |
118
|
|
|
return emb |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// AttachThumbURL adds attached thumbnail to embed from url |
122
|
|
|
func (emb *NewEmbedStruct) AttachThumbURL(url string) *NewEmbedStruct { |
123
|
|
|
emb.Embed.Thumbnail = &discordgo.MessageEmbedThumbnail{URL: url} |
124
|
|
|
return emb |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Send send embed message to Discord |
128
|
|
|
func (emb *NewEmbedStruct) Send(ctx *Context) *discordgo.Message { |
129
|
|
|
msg, err := ctx.Discord.ChannelMessageSendComplex(ctx.TextChannel.ID, emb.MessageSend) |
130
|
|
|
if err != nil { |
131
|
|
|
fmt.Println("Error whilst sending embed message, ", err) |
132
|
|
|
return nil |
133
|
|
|
} |
134
|
|
|
ctx.BotMsg.Add(ctx, msg.ID) |
135
|
|
|
return msg |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// SendPM send embed personal message to Discord |
139
|
|
|
func (emb *NewEmbedStruct) SendPM(ctx *Context) *discordgo.Message { |
140
|
|
|
ch, err := ctx.Discord.UserChannelCreate(ctx.User.ID) |
141
|
|
|
if err != nil { |
142
|
|
|
fmt.Println("Error whilst creating private channel, ", err) |
143
|
|
|
return nil |
144
|
|
|
} |
145
|
|
|
msg, err := ctx.Discord.ChannelMessageSendComplex(ch.ID, emb.MessageSend) |
146
|
|
|
if err != nil { |
147
|
|
|
fmt.Println("Error whilst sending embed message, ", err) |
148
|
|
|
return nil |
149
|
|
|
} |
150
|
|
|
return msg |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// GetEmbed returns discords embed |
154
|
|
|
func (emb *NewEmbedStruct) GetEmbed() *discordgo.MessageEmbed { |
155
|
|
|
return emb.Embed |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Reply reply on massage |
159
|
|
|
func (ctx *Context) Reply(content string) *discordgo.Message { |
160
|
|
|
msg, err := ctx.Discord.ChannelMessageSend(ctx.TextChannel.ID, content) |
161
|
|
|
if err != nil { |
162
|
|
|
fmt.Println("Error whilst sending message,", err) |
163
|
|
|
return nil |
164
|
|
|
} |
165
|
|
|
ctx.BotMsg.Add(ctx, msg.ID) |
166
|
|
|
return msg |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// ReplyFile reply on massage with file |
170
|
|
|
func (ctx *Context) ReplyFile(name string, r io.Reader) *discordgo.Message { |
171
|
|
|
msg, err := ctx.Discord.ChannelFileSend(ctx.TextChannel.ID, name, r) |
172
|
|
|
if err != nil { |
173
|
|
|
fmt.Println("Error whilst sending file,", err) |
174
|
|
|
return nil |
175
|
|
|
} |
176
|
|
|
ctx.BotMsg.Add(ctx, msg.ID) |
177
|
|
|
return msg |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// EditEmbed edits embed message by id |
181
|
|
|
func (ctx *Context) EditEmbed(ID, name, value string, inline bool) { |
182
|
|
|
_, err := ctx.Discord.ChannelMessageEditEmbed(ctx.TextChannel.ID, ID, NewEmbed(""). |
183
|
|
|
Color(ctx.GetGuild().EmbedColor). |
184
|
|
|
Footer(fmt.Sprintf("%v %v", ctx.Loc("requested_by"), ctx.User.Username)). |
185
|
|
|
Field(name, value, inline). |
186
|
|
|
GetEmbed()) |
187
|
|
|
if err != nil { |
188
|
|
|
ctx.Log("Message", ctx.Guild.ID, err.Error()) |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// ReplyEmbed reply on message with embed message |
193
|
|
|
func (ctx *Context) ReplyEmbed(name, content string) *discordgo.Message { |
194
|
|
|
return NewEmbed(""). |
195
|
|
|
Field(name, content, false). |
196
|
|
|
Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username). |
197
|
|
|
Color(ctx.GetGuild().EmbedColor). |
198
|
|
|
Send(ctx) |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// ReplyEmbedPM sends embed in personal channel |
202
|
|
|
func (ctx *Context) ReplyEmbedPM(name, content string) *discordgo.Message { |
203
|
|
|
return NewEmbed(""). |
204
|
|
|
Field(name, content, false). |
205
|
|
|
Footer(ctx.Loc("requested_from") + ": " + ctx.Guild.Name). |
206
|
|
|
Color(ctx.GetGuild().EmbedColor). |
207
|
|
|
SendPM(ctx) |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// ReplyPM sends reply message to user personal channel |
211
|
|
|
func (ctx *Context) ReplyPM(content string) *discordgo.Message { |
212
|
|
|
ch, err := ctx.Discord.UserChannelCreate(ctx.User.ID) |
213
|
|
|
if err != nil { |
214
|
|
|
ctx.Log("reply_pm", "", fmt.Sprintf("Error whilst creating private channel: %v", err.Error())) |
215
|
|
|
return nil |
216
|
|
|
} |
217
|
|
|
msg, err := ctx.Discord.ChannelMessageSend(ch.ID, content) |
218
|
|
|
if err != nil { |
219
|
|
|
ctx.Log("reply_pm", "", fmt.Sprintf("ReplyPM error: %v", err.Error())) |
220
|
|
|
} |
221
|
|
|
return msg |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// ReplyEmbedAttachment reply on message with embed message with attachment |
225
|
|
|
func (ctx *Context) ReplyEmbedAttachment(name, content, fileName string, file io.Reader) *discordgo.Message { |
226
|
|
|
return NewEmbed(""). |
227
|
|
|
Field(name, content, false). |
228
|
|
|
AttachImg(fileName, file). |
229
|
|
|
Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username). |
230
|
|
|
Color(ctx.GetGuild().EmbedColor). |
231
|
|
|
Send(ctx) |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// ReplyEmbedAttachment reply on message with embed message with attachment |
|
|
|
|
235
|
|
|
func (ctx *Context) ReplyEmbedAttachmentImageURL(title, desc, imageUrl string) *discordgo.Message { |
|
|
|
|
236
|
|
|
return NewEmbed(title). |
237
|
|
|
Desc(desc). |
238
|
|
|
AttachImgURL(imageUrl). |
239
|
|
|
Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username). |
240
|
|
|
Color(ctx.GetGuild().EmbedColor). |
241
|
|
|
Send(ctx) |
242
|
|
|
} |