Passed
Push — master ( a266ab...6dd3ac )
by Viktor
01:47
created

bot/reply.go   A

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 42
eloc 147
dl 0
loc 238
rs 9.0399
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A bot.truncateText 0 5 4
A bot.NewEmbed 0 3 1
A bot.*NewEmbedStruct.Send 0 8 2
A bot.*Context.EditEmbed 0 8 2
A bot.*NewEmbedStruct.Desc 0 8 2
A bot.*Context.ReplyPM 0 11 3
A bot.*NewEmbedStruct.AttachImgURL 0 3 1
A bot.*NewEmbedStruct.Author 0 8 2
A bot.*NewEmbedStruct.AttachImg 0 4 1
A bot.*NewEmbedStruct.SendPM 0 12 3
A bot.*Context.Reply 0 8 2
A bot.*Context.ReplyEmbedAttachment 0 7 1
A bot.*NewEmbedStruct.Field 0 15 4
A bot.*NewEmbedStruct.AttachThumbURL 0 3 1
A bot.*NewEmbedStruct.URL 0 3 1
A bot.*NewEmbedStruct.Footer 0 8 2
A bot.*NewEmbedStruct.Color 0 3 1
A bot.*NewEmbedStruct.GetEmbed 0 2 1
A bot.*Context.ReplyFile 0 8 2
A bot.*Context.ReplyEmbed 0 6 1
A bot.*Context.ReplyEmbedPM 0 6 1
A bot.*NewEmbedStruct.TimeStamp 0 3 1
A bot.*NewEmbedStruct.CheckLength 0 5 2
A bot.*Context.ReplyEmbedAttachmentImageURL 0 6 1
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
	desc = truncateText(desc, 2048)
75
	newLength := len(desc)
76
	if emb.CheckLength(newLength) {
77
		emb.Embed.Description = desc
78
		emb.embLength += newLength
79
	}
80
	return emb
81
}
82
83
// URL adds url to embed description
84
func (emb *NewEmbedStruct) URL(url string) *NewEmbedStruct {
85
	emb.Embed.URL = url
86
	return emb
87
}
88
89
// Footer adds footer text
90
func (emb *NewEmbedStruct) Footer(text string) *NewEmbedStruct {
91
	text = truncateText(text, 2048)
92
	newLength := len(text)
93
	if emb.CheckLength(newLength) {
94
		emb.Embed.Footer = &discordgo.MessageEmbedFooter{Text: text}
95
		emb.embLength += newLength
96
	}
97
	return emb
98
}
99
100
// Color adds color to embed
101
func (emb *NewEmbedStruct) Color(color int) *NewEmbedStruct {
102
	emb.Embed.Color = color
103
	return emb
104
}
105
106
// AttachImg adds attached image to embed from io.Reader
107
func (emb *NewEmbedStruct) AttachImg(name string, file io.Reader) *NewEmbedStruct {
108
	emb.Embed.Image = &discordgo.MessageEmbedImage{URL: "attachment://" + name}
109
	emb.Files = append(emb.Files, &discordgo.File{Name: name, Reader: file})
110
	return emb
111
}
112
113
// AttachImgURL adds attached image to embed from url
114
func (emb *NewEmbedStruct) AttachImgURL(url string) *NewEmbedStruct {
115
	emb.Embed.Image = &discordgo.MessageEmbedImage{URL: url}
116
	return emb
117
}
118
119
// AttachThumbURL adds attached thumbnail to embed from url
120
func (emb *NewEmbedStruct) AttachThumbURL(url string) *NewEmbedStruct {
121
	emb.Embed.Thumbnail = &discordgo.MessageEmbedThumbnail{URL: url}
122
	return emb
123
}
124
125
// Send send embed message to Discord
126
func (emb *NewEmbedStruct) Send(ctx *Context) *discordgo.Message {
127
	msg, err := ctx.Discord.ChannelMessageSendComplex(ctx.TextChannel.ID, emb.MessageSend)
128
	if err != nil {
129
		fmt.Println("Error whilst sending embed message, ", err)
130
		return nil
131
	}
132
	ctx.BotMsg.Add(ctx, msg.ID)
133
	return msg
134
}
135
136
// SendPM send embed personal message to Discord
137
func (emb *NewEmbedStruct) SendPM(ctx *Context) *discordgo.Message {
138
	ch, err := ctx.Discord.UserChannelCreate(ctx.User.ID)
139
	if err != nil {
140
		fmt.Println("Error whilst creating private channel, ", err)
141
		return nil
142
	}
143
	msg, err := ctx.Discord.ChannelMessageSendComplex(ch.ID, emb.MessageSend)
144
	if err != nil {
145
		fmt.Println("Error whilst sending embed message, ", err)
146
		return nil
147
	}
148
	return msg
149
}
150
151
// GetEmbed returns discords embed
152
func (emb *NewEmbedStruct) GetEmbed() *discordgo.MessageEmbed {
153
	return emb.Embed
154
}
155
156
// Reply reply on massage
157
func (ctx *Context) Reply(content string) *discordgo.Message {
158
	msg, err := ctx.Discord.ChannelMessageSend(ctx.TextChannel.ID, content)
159
	if err != nil {
160
		fmt.Println("Error whilst sending message,", err)
161
		return nil
162
	}
163
	ctx.BotMsg.Add(ctx, msg.ID)
164
	return msg
165
}
166
167
// ReplyFile reply on massage with file
168
func (ctx *Context) ReplyFile(name string, r io.Reader) *discordgo.Message {
169
	msg, err := ctx.Discord.ChannelFileSend(ctx.TextChannel.ID, name, r)
170
	if err != nil {
171
		fmt.Println("Error whilst sending file,", err)
172
		return nil
173
	}
174
	ctx.BotMsg.Add(ctx, msg.ID)
175
	return msg
176
}
177
178
// EditEmbed edits embed message by id
179
func (ctx *Context) EditEmbed(ID, name, value string, inline bool) {
180
	_, err := ctx.Discord.ChannelMessageEditEmbed(ctx.TextChannel.ID, ID, NewEmbed("").
181
		Color(ctx.GetGuild().EmbedColor).
182
		Footer(fmt.Sprintf("%v %v", ctx.Loc("requested_by"), ctx.User.Username)).
183
		Field(name, value, inline).
184
		GetEmbed())
185
	if err != nil {
186
		ctx.Log("Message", ctx.Guild.ID, err.Error())
187
	}
188
}
189
190
// ReplyEmbed reply on message with embed message
191
func (ctx *Context) ReplyEmbed(name, content string) *discordgo.Message {
192
	return NewEmbed("").
193
		Field(name, content, false).
194
		Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username).
195
		Color(ctx.GetGuild().EmbedColor).
196
		Send(ctx)
197
}
198
199
// ReplyEmbedPM sends embed in personal channel
200
func (ctx *Context) ReplyEmbedPM(name, content string) *discordgo.Message {
201
	return NewEmbed("").
202
		Field(name, content, false).
203
		Footer(ctx.Loc("requested_from") + ": " + ctx.Guild.Name).
204
		Color(ctx.GetGuild().EmbedColor).
205
		SendPM(ctx)
206
}
207
208
// ReplyPM sends reply message to user personal channel
209
func (ctx *Context) ReplyPM(content string) *discordgo.Message {
210
	ch, err := ctx.Discord.UserChannelCreate(ctx.User.ID)
211
	if err != nil {
212
		ctx.Log("reply_pm", "", fmt.Sprintf("Error whilst creating private channel: %v", err.Error()))
213
		return nil
214
	}
215
	msg, err := ctx.Discord.ChannelMessageSend(ch.ID, content)
216
	if err != nil {
217
		ctx.Log("reply_pm", "", fmt.Sprintf("ReplyPM error: %v", err.Error()))
218
	}
219
	return msg
220
}
221
222
// ReplyEmbedAttachment reply on message with embed message with attachment
223
func (ctx *Context) ReplyEmbedAttachment(name, content, fileName string, file io.Reader) *discordgo.Message {
224
	return NewEmbed("").
225
		Field(name, content, false).
226
		AttachImg(fileName, file).
227
		Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username).
228
		Color(ctx.GetGuild().EmbedColor).
229
		Send(ctx)
230
}
231
232
// ReplyEmbedAttachment reply on message with embed message with attachment
0 ignored issues
show
introduced by
comment on exported method Context.ReplyEmbedAttachmentImageURL should be of the form "ReplyEmbedAttachmentImageURL ..."
Loading history...
233
func (ctx *Context) ReplyEmbedAttachmentImageURL(title, imageUrl string) *discordgo.Message {
0 ignored issues
show
introduced by
method parameter imageUrl should be imageURL
Loading history...
234
	return NewEmbed(title).
235
		AttachImgURL(imageUrl).
236
		Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username).
237
		Color(ctx.GetGuild().EmbedColor).
238
		Send(ctx)
239
}