forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modlog.go
118 lines (97 loc) · 3.14 KB
/
modlog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package moderation
import (
"fmt"
"regexp"
"strings"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
)
type ModlogAction struct {
Prefix string
Emoji string
Color int
Footer string
}
func (m ModlogAction) String() string {
str := m.Emoji + m.Prefix
if m.Footer != "" {
str += " (" + m.Footer + ")"
}
return str
}
var (
MAMute = ModlogAction{Prefix: "Muted", Emoji: "🔇", Color: 0x57728e}
MAUnmute = ModlogAction{Prefix: "Unmuted", Emoji: "🔊", Color: 0x62c65f}
MAKick = ModlogAction{Prefix: "Kicked", Emoji: "👢", Color: 0xf2a013}
MABanned = ModlogAction{Prefix: "Banned", Emoji: "🔨", Color: 0xd64848}
MAUnbanned = ModlogAction{Prefix: "Unbanned", Emoji: "🔓", Color: 0x62c65f}
MAWarned = ModlogAction{Prefix: "Warned", Emoji: "⚠", Color: 0xfca253}
MAGiveRole = ModlogAction{Prefix: "", Emoji: "➕", Color: 0x53fcf9}
MARemoveRole = ModlogAction{Prefix: "", Emoji: "➖", Color: 0x53fcf9}
)
func CreateModlogEmbed(channelID int64, author *discordgo.User, action ModlogAction, target *discordgo.User, reason, logLink string) error {
if channelID == 0 {
return nil
}
emptyAuthor := false
if author == nil {
emptyAuthor = true
author = &discordgo.User{
ID: 0,
Username: "Unknown",
Discriminator: "????",
}
}
if reason == "" {
reason = "(no reason specified)"
}
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
Name: fmt.Sprintf("%s#%s (ID %d)", author.Username, author.Discriminator, author.ID),
IconURL: discordgo.EndpointUserAvatar(author.ID, author.Avatar),
},
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: discordgo.EndpointUserAvatar(target.ID, target.Avatar),
},
Color: action.Color,
Description: fmt.Sprintf("**%s%s %s**#%s *(ID %d)*\n📄**Reason:** %s",
action.Emoji, action.Prefix, target.Username, target.Discriminator, target.ID, reason),
}
if logLink != "" {
embed.Description += " ([Logs](" + logLink + "))"
}
if action.Footer != "" {
embed.Footer = &discordgo.MessageEmbedFooter{
Text: action.Footer,
}
}
m, err := common.BotSession.ChannelMessageSendEmbed(channelID, embed)
if err != nil {
return err
}
if emptyAuthor {
placeholder := fmt.Sprintf("Asssign an author and reason to this using **'reason %d your-reason-here`**", m.ID)
updateEmbedReason(nil, placeholder, embed)
_, err = common.BotSession.ChannelMessageEditEmbed(channelID, m.ID, embed)
}
return err
}
var (
logsRegex = regexp.MustCompile(`\(\[Logs\]\(.*\)\)`)
)
func updateEmbedReason(author *discordgo.User, reason string, embed *discordgo.MessageEmbed) {
const checkStr = "📄**Reason:**"
index := strings.Index(embed.Description, checkStr)
withoutReason := embed.Description[:index+len(checkStr)]
logsLink := logsRegex.FindString(embed.Description)
if logsLink != "" {
logsLink = " " + logsLink
}
embed.Description = withoutReason + " " + reason + logsLink
if author != nil {
embed.Author = &discordgo.MessageEmbedAuthor{
Name: fmt.Sprintf("%s#%s (ID %d)", author.Username, author.Discriminator, author.ID),
IconURL: discordgo.EndpointUserAvatar(author.ID, author.Avatar),
}
}
}