Passed
Push — master ( 8f4eac...14a602 )
by Viktor
01:56
created

bot.*NewEmbedStruct.Footer   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
}
14
15
// NewEmbed creates new embed
16
func NewEmbed(title string) *NewEmbedStruct {
17
	return &NewEmbedStruct{&discordgo.MessageSend{Embed: &discordgo.MessageEmbed{Title: title}}}
18
}
19
20
// Field adds field to embed
21
func (emb *NewEmbedStruct) Field(name, value string, inline bool) *NewEmbedStruct {
22
	emb.Embed.Fields = append(emb.Embed.Fields, &discordgo.MessageEmbedField{Name: name, Value: value, Inline: inline})
23
	return emb
24
}
25
26
func (emb *NewEmbedStruct) TimeStamp(ts string) *NewEmbedStruct {
0 ignored issues
show
introduced by
exported method NewEmbedStruct.TimeStamp should have comment or be unexported
Loading history...
27
	emb.Embed.Timestamp = ts
28
	return emb
29
}
30
31
// Author adds author to embed
32
func (emb *NewEmbedStruct) Author(name, url, iconURL string) *NewEmbedStruct {
33
	emb.Embed.Author = &discordgo.MessageEmbedAuthor{URL: url, Name: name, IconURL: iconURL}
34
	return emb
35
}
36
37
// Desc adds description to embed
38
func (emb *NewEmbedStruct) Desc(desc string) *NewEmbedStruct {
39
	emb.Embed.Description = desc
40
	return emb
41
}
42
43
// URL adds url to embed description
44
func (emb *NewEmbedStruct) URL(url string) *NewEmbedStruct {
45
	emb.Embed.URL = url
46
	return emb
47
}
48
49
// Footer adds footer text
50
func (emb *NewEmbedStruct) Footer(text string) *NewEmbedStruct {
51
	emb.Embed.Footer = &discordgo.MessageEmbedFooter{Text: text}
52
	return emb
53
}
54
55
// Color adds color to embed
56
func (emb *NewEmbedStruct) Color(color int) *NewEmbedStruct {
57
	emb.Embed.Color = color
58
	return emb
59
}
60
61
// AttachImg adds attached image to embed from io.Reader
62
func (emb *NewEmbedStruct) AttachImg(name string, file io.Reader) *NewEmbedStruct {
63
	emb.Embed.Image = &discordgo.MessageEmbedImage{URL: "attachment://" + name}
64
	emb.Files = append(emb.Files, &discordgo.File{Name: name, Reader: file})
65
	return emb
66
}
67
68
// AttachImgURL adds attached image to embed from url
69
func (emb *NewEmbedStruct) AttachImgURL(url string) *NewEmbedStruct {
70
	emb.Embed.Image = &discordgo.MessageEmbedImage{URL: url}
71
	return emb
72
}
73
74
// AttachThumbURL adds attached thumbnail to embed from url
75
func (emb *NewEmbedStruct) AttachThumbURL(url string) *NewEmbedStruct {
76
	emb.Embed.Thumbnail = &discordgo.MessageEmbedThumbnail{URL: url}
77
	return emb
78
}
79
80
// Send send embed message to Discord
81
func (emb *NewEmbedStruct) Send(ctx *Context) *discordgo.Message {
82
	msg, err := ctx.Discord.ChannelMessageSendComplex(ctx.TextChannel.ID, emb.MessageSend)
83
	if err != nil {
84
		fmt.Println("Error whilst sending embed message, ", err)
85
		return nil
86
	}
87
	ctx.BotMsg.Add(ctx, msg.ID)
88
	return msg
89
}
90
91
// SendPM send embed personal message to Discord
92
func (emb *NewEmbedStruct) SendPM(ctx *Context) *discordgo.Message {
93
	ch, err := ctx.Discord.UserChannelCreate(ctx.User.ID)
94
	if err != nil {
95
		fmt.Println("Error whilst creating private channel, ", err)
96
		return nil
97
	}
98
	msg, err := ctx.Discord.ChannelMessageSendComplex(ch.ID, emb.MessageSend)
99
	if err != nil {
100
		fmt.Println("Error whilst sending embed message, ", err)
101
		return nil
102
	}
103
	return msg
104
}
105
106
// GetEmbed returns discords embed
107
func (emb *NewEmbedStruct) GetEmbed() *discordgo.MessageEmbed {
108
	return emb.Embed
109
}
110
111
// Reply reply on massage
112
func (ctx *Context) Reply(content string) *discordgo.Message {
113
	msg, err := ctx.Discord.ChannelMessageSend(ctx.TextChannel.ID, content)
114
	if err != nil {
115
		fmt.Println("Error whilst sending message,", err)
116
		return nil
117
	}
118
	ctx.BotMsg.Add(ctx, msg.ID)
119
	return msg
120
}
121
122
// ReplyFile reply on massage with file
123
func (ctx *Context) ReplyFile(name string, r io.Reader) *discordgo.Message {
124
	msg, err := ctx.Discord.ChannelFileSend(ctx.TextChannel.ID, name, r)
125
	if err != nil {
126
		fmt.Println("Error whilst sending file,", err)
127
		return nil
128
	}
129
	ctx.BotMsg.Add(ctx, msg.ID)
130
	return msg
131
}
132
133
// EditEmbed edits embed message by id
134
func (ctx *Context) EditEmbed(ID, name, value string, inline bool) {
135
	_, err := ctx.Discord.ChannelMessageEditEmbed(ctx.TextChannel.ID, ID, NewEmbed("").
136
		Color(ctx.GetGuild().EmbedColor).
137
		Footer(fmt.Sprintf("%v %v", ctx.Loc("requested_by"), ctx.User.Username)).
138
		Field(name, value, inline).
139
		GetEmbed())
140
	if err != nil {
141
		ctx.Log("Message", ctx.Guild.ID, err.Error())
142
	}
143
}
144
145
// ReplyEmbed reply on message with embed message
146
func (ctx *Context) ReplyEmbed(name, content string) *discordgo.Message {
147
	return NewEmbed("").
148
		Field(name, content, false).
149
		Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username).
150
		Color(ctx.GetGuild().EmbedColor).
151
		Send(ctx)
152
}
153
154
// ReplyEmbedPM sends embed in personal channel
155
func (ctx *Context) ReplyEmbedPM(name, content string) *discordgo.Message {
156
	return NewEmbed("").
157
		Field(name, content, false).
158
		Footer(ctx.Loc("requested_from") + ": " + ctx.Guild.Name).
159
		Color(ctx.GetGuild().EmbedColor).
160
		SendPM(ctx)
161
}
162
163
// ReplyPM sends reply message to user personal channel
164
func (ctx *Context) ReplyPM(content string) *discordgo.Message {
165
	ch, err := ctx.Discord.UserChannelCreate(ctx.User.ID)
166
	if err != nil {
167
		ctx.Log("reply_pm", "", fmt.Sprintf("Error whilst creating private channel: %v", err.Error()))
168
		return nil
169
	}
170
	msg, err := ctx.Discord.ChannelMessageSend(ch.ID, content)
171
	if err != nil {
172
		ctx.Log("reply_pm", "", fmt.Sprintf("ReplyPM error: %v", err.Error()))
173
	}
174
	return msg
175
}
176
177
// ReplyEmbedAttachment reply on message with embed message with attachment
178
func (ctx *Context) ReplyEmbedAttachment(name, content, fileName string, file io.Reader) *discordgo.Message {
179
	return NewEmbed("").
180
		Field(name, content, false).
181
		AttachImg(fileName, file).
182
		Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username).
183
		Color(ctx.GetGuild().EmbedColor).
184
		Send(ctx)
185
}
186