-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
MemberLogging.kt
158 lines (148 loc) · 5 KB
/
MemberLogging.kt
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package org.hyacinthbots.lilybot.extensions.events
import com.kotlindiscord.kord.extensions.DISCORD_GREEN
import com.kotlindiscord.kord.extensions.DISCORD_RED
import com.kotlindiscord.kord.extensions.checks.anyGuild
import com.kotlindiscord.kord.extensions.checks.guildFor
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.event
import com.kotlindiscord.kord.extensions.utils.botHasPermissions
import dev.kord.common.entity.Permission
import dev.kord.core.behavior.channel.createEmbed
import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.behavior.getChannelOfOrNull
import dev.kord.core.entity.channel.GuildMessageChannel
import dev.kord.core.event.guild.MemberJoinEvent
import dev.kord.core.event.guild.MemberLeaveEvent
import dev.kord.rest.builder.message.create.embed
import kotlinx.datetime.Clock
import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection
import org.hyacinthbots.lilybot.extensions.config.ConfigOptions
import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms
import org.hyacinthbots.lilybot.utils.getMemberCount
import org.hyacinthbots.lilybot.utils.requiredConfigs
/**
* Logs members joining and leaving a guild to the member log channel designated in the config for that guild.
* @author NoComment1105
* @author tempest15
* @since 2.0
*/
class MemberLogging : Extension() {
override val name = "member-logging"
override suspend fun setup() {
/** Create an embed in the join channel on user join */
event<MemberJoinEvent> {
check {
anyGuild()
requiredConfigs(ConfigOptions.MEMBER_LOGGING_ENABLED, ConfigOptions.MEMBER_LOG)
failIf { event.member.id == kord.selfId }
}
action {
val memberLog = getLoggingChannelWithPerms(ConfigOptions.MEMBER_LOG, event.guild)
val config = LoggingConfigCollection().getConfig(event.guildId)
memberLog?.createEmbed {
author {
name = "User joined the server!"
icon = event.member.avatar?.cdnUrl?.toUrl()
}
field {
name = "Welcome:"
value = "${event.member.mention} (${event.member.tag})"
inline = true
}
field {
name = "ID:"
value = event.member.id.toString()
inline = false
}
timestamp = Clock.System.now()
color = DISCORD_GREEN
footer {
text = "Member count: ${event.guild.getMemberCount()}"
}
}
if (config != null && config.enablePublicMemberLogs) {
var publicLog = guildFor(event)?.getChannelOfOrNull<GuildMessageChannel>(config.publicMemberLog!!)
val permissions = publicLog?.botHasPermissions(Permission.SendMessages, Permission.EmbedLinks)
if (permissions == false || permissions == null) {
publicLog = null
}
publicLog?.createMessage {
if (config.publicMemberLogData?.pingNewUsers == true) content = event.member.mention
embed {
author {
name = "Welcome ${event.member.username}"
icon = event.member.avatar?.cdnUrl?.toUrl()
}
description = if (config.publicMemberLogData?.joinMessage != null) {
config.publicMemberLogData.joinMessage
} else {
"Welcome to the server!"
}
timestamp = Clock.System.now()
color = DISCORD_GREEN
footer {
text =
"Member count: ${event.guild.getMemberCount()}"
}
}
}
}
}
}
/** Create an embed in the join channel on user leave */
event<MemberLeaveEvent> {
check {
anyGuild()
requiredConfigs(ConfigOptions.MEMBER_LOGGING_ENABLED, ConfigOptions.MEMBER_LOG)
failIf { event.user.id == kord.selfId }
}
action {
val memberLog = getLoggingChannelWithPerms(ConfigOptions.MEMBER_LOG, event.guild)
val config = LoggingConfigCollection().getConfig(event.guildId)
memberLog?.createEmbed {
author {
name = "User left the server!"
icon = event.user.avatar?.cdnUrl?.toUrl()
}
field {
name = "Goodbye:"
value = event.user.tag
inline = true
}
field {
name = "ID:"
value = event.user.id.toString()
}
timestamp = Clock.System.now()
color = DISCORD_RED
footer {
text = "Member count: ${event.guild.getMemberCount()}"
}
}
if (config != null && config.enablePublicMemberLogs) {
var publicLog = guildFor(event)?.getChannelOfOrNull<GuildMessageChannel>(config.publicMemberLog!!)
val permissions = publicLog?.botHasPermissions(Permission.SendMessages, Permission.EmbedLinks)
if (permissions == false || permissions == null) {
publicLog = null
}
publicLog?.createEmbed {
author {
name = "Goodbye ${event.user.username}"
icon = event.user.avatar?.cdnUrl?.toUrl()
}
description = if (config.publicMemberLogData?.leaveMessage != null) {
config.publicMemberLogData.leaveMessage
} else {
"Farewell!"
}
timestamp = Clock.System.now()
color = DISCORD_RED
footer {
text = "Member count: ${event.guild.getMemberCount()}"
}
}
}
}
}
}
}