TerraOceanPlugin/GoBot/discord/message/embed.go

88 lines
1.9 KiB
Go

package message
2019-10-10 10:51:53 +07:00
2019-10-12 15:33:06 +07:00
import (
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/config"
)
2019-10-10 10:51:53 +07:00
type Embed struct {
2019-10-10 10:51:53 +07:00
*discordgo.MessageEmbed
}
func NewEmbed() *Embed {
return &Embed{&discordgo.MessageEmbed{}}
2019-10-10 10:51:53 +07:00
}
func InitNewEmbed(title, description string, colour int) *Embed {
return NewEmbed().SetTitle(title).SetDescription(description).SetColour(colour)
2019-10-12 11:29:33 +07:00
}
func InitNewErrorEmbed(description string) *Embed {
e := NewEmbed()
2019-10-10 10:51:53 +07:00
e.Color = 0xE00000
e.Title = "发生错误An error has occurred!"
2019-10-12 11:29:33 +07:00
e.Description = description
2019-10-10 10:51:53 +07:00
return e
}
func (e *Embed) SetTitle(title string) *Embed {
2019-10-10 10:51:53 +07:00
e.Title = title
return e
}
func (e *Embed) SetColour(colour int) *Embed {
2019-10-10 10:51:53 +07:00
e.Color = colour
return e
}
func (e *Embed) SetDescription(description string) *Embed {
2019-10-10 10:51:53 +07:00
e.Description = description
return e
}
func (e *Embed) AddField(name, value string) *Embed {
2019-10-10 10:51:53 +07:00
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
Name: name,
Value: value,
})
return e
}
func (e *Embed) AddInlineField(name, value string) *Embed {
2019-10-12 11:29:33 +07:00
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
Name: name,
Value: value,
Inline: true,
})
return e
}
func (e *Embed) UpdateVoteStatus(isPositive bool) *Embed {
2019-10-12 15:33:06 +07:00
if isPositive {
return e.SetColour(config.VotePassColour).SetTitle(config.VotePassTitle)
}
return e.SetColour(config.VoteRejectColour).SetTitle(config.VoteRejectTitle)
}
func (e *Embed) Build() *discordgo.MessageEmbed {
2019-10-10 10:51:53 +07:00
return e.MessageEmbed
}
2019-10-12 11:29:33 +07:00
func (e *Embed) Send(s *discordgo.Session, channelID string) (*discordgo.Message, error) {
2019-10-12 14:44:05 +07:00
return s.ChannelMessageSendEmbed(channelID, e.Build())
2019-10-12 11:29:33 +07:00
}
2019-10-12 13:49:41 +07:00
func (e *Embed) SendPM(s *discordgo.Session, userID string) (*discordgo.Message, error) {
2019-10-12 13:49:41 +07:00
channel, err := s.UserChannelCreate(userID)
if err != nil {
AuditError(s, "", err)
2019-10-13 09:59:35 +07:00
return nil, err
2019-10-12 13:49:41 +07:00
}
2019-10-13 09:59:35 +07:00
return e.Send(s, channel.ID)
2019-10-12 13:49:41 +07:00
}
2019-10-12 15:33:06 +07:00
func (e *Embed) Edit(s *discordgo.Session, channelID, messageID string) {
2019-10-12 15:33:06 +07:00
s.ChannelMessageEditEmbed(channelID, messageID, e.Build())
}