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