Passed
Push — master ( d5c9bc...e94d82 )
by Viktor
01:33
created

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