TerraOceanPlugin/GoBot/discord/modules/embed.go

88 lines
1.9 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
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 {
*discordgo.MessageEmbed
}
func newEmbed() *embed {
return &embed{&discordgo.MessageEmbed{}}
}
2019-10-12 11:29:33 +07:00
func initNewEmbed(title, description string, colour int) *embed {
return newEmbed().SetTitle(title).SetDescription(description).SetColour(colour)
}
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 {
e.Title = title
return e
}
func (e *embed) SetColour(colour int) *embed {
e.Color = colour
return e
}
func (e *embed) SetDescription(description string) *embed {
e.Description = description
return e
}
func (e *embed) AddField(name, value string) *embed {
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
Name: name,
Value: value,
})
return e
}
2019-10-12 11:29:33 +07:00
func (e *embed) AddInlineField(name, value string) *embed {
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
Name: name,
Value: value,
Inline: true,
})
return e
}
2019-10-12 15:33:06 +07:00
func (e *embed) UpdateVoteStatus(isPositive bool) *embed {
if isPositive {
return e.SetColour(config.VotePassColour).SetTitle(config.VotePassTitle)
}
return e.SetColour(config.VoteRejectColour).SetTitle(config.VoteRejectTitle)
}
2019-10-10 10:51:53 +07:00
func (e *embed) Build() *discordgo.MessageEmbed {
return e.MessageEmbed
}
2019-10-12 11:29:33 +07:00
2019-10-12 14:44:05 +07:00
func (e *embed) Send(s *discordgo.Session, channelID string) (*discordgo.Message, error) {
return s.ChannelMessageSendEmbed(channelID, e.Build())
2019-10-12 11:29:33 +07:00
}
2019-10-12 13:49:41 +07:00
2019-10-13 09:59:35 +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) {
s.ChannelMessageEditEmbed(channelID, messageID, e.Build())
}