-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve member logs and add flag checking
- Loading branch information
Showing
8 changed files
with
181 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import { FT, T } from '#lib/types'; | ||
|
||
export const GuildMemberAdd = T<string>('events/guilds-members:guildMemberAdd'); | ||
export const GuildMemberAddDescription = FT<{ mention: string; time: number }, string>('events/guilds-members:guildMemberAddDescription'); | ||
export const GuildMemberAddedRoles = FT<{ addedRoles: string; count: number }, string>('events/guilds-members:guildMemberAddedRoles'); | ||
export const GuildMemberAddMute = T<string>('events/guilds-members:guildMemberAddMute'); | ||
export const GuildMemberBanned = T<string>('events/guilds-members:guildMemberBanned'); | ||
export const GuildMemberKicked = T<string>('events/guilds-members:guildMemberKicked'); | ||
export const GuildMemberNoUpdate = T<string>('events/guilds-members:guildMemberNoUpdate'); | ||
export const GuildMemberRemove = T<string>('events/guilds-members:guildMemberRemove'); | ||
export const GuildMemberRemoveDescription = FT<{ mention: string; time: number }, string>('events/guilds-members:guildMemberRemoveDescription'); | ||
export const GuildMemberRemoveDescriptionWithJoinedAt = FT<{ mention: string; time: number }, string>( | ||
export const GuildMemberAdd = T('events/guilds-members:guildMemberAdd'); | ||
export const GuildMemberAddDescription = FT<{ user: string; relativeTime: string }>('events/guilds-members:guildMemberAddDescription'); | ||
export const GuildMemberAddedRoles = FT<{ addedRoles: string; count: number }>('events/guilds-members:guildMemberAddedRoles'); | ||
export const GuildMemberAddMute = T('events/guilds-members:guildMemberAddMute'); | ||
export const GuildMemberBanned = T('events/guilds-members:guildMemberBanned'); | ||
export const GuildMemberKicked = T('events/guilds-members:guildMemberKicked'); | ||
export const GuildMemberNoUpdate = T('events/guilds-members:guildMemberNoUpdate'); | ||
export const GuildMemberRemove = T('events/guilds-members:guildMemberRemove'); | ||
export const GuildMemberRemoveDescription = FT<{ user: string; relativeTime: string }>('events/guilds-members:guildMemberRemoveDescription'); | ||
export const GuildMemberRemoveDescriptionWithJoinedAt = FT<{ user: string; relativeTime: string }>( | ||
'events/guilds-members:guildMemberRemoveDescriptionWithJoinedAt' | ||
); | ||
export const GuildMemberRemovedRoles = FT<{ removedRoles: string; count: number }, string>('events/guilds-members:guildMemberRemovedRoles'); | ||
export const GuildMemberSoftBanned = T<string>('events/guilds-members:guildMemberSoftBanned'); | ||
export const NameUpdateNextWasNotSet = FT<{ nextName: string | null }, string>('events/guilds-members:nameUpdateNextWasNotSet'); | ||
export const NameUpdateNextWasSet = FT<{ nextName: string | null }, string>('events/guilds-members:nameUpdateNextWasSet'); | ||
export const NameUpdatePreviousWasNotSet = FT<{ previousName: string | null }, string>('events/guilds-members:nameUpdatePreviousWasNotSet'); | ||
export const NameUpdatePreviousWasSet = FT<{ previousName: string | null }, string>('events/guilds-members:nameUpdatePreviousWasSet'); | ||
export const NicknameUpdate = T<string>('events/guilds-members:nicknameUpdate'); | ||
export const RoleUpdate = T<string>('events/guilds-members:roleUpdate'); | ||
export const UsernameUpdate = T<string>('events/guilds-members:usernameUpdate'); | ||
export const GuildMemberRemovedRoles = FT<{ removedRoles: string; count: number }>('events/guilds-members:guildMemberRemovedRoles'); | ||
export const GuildMemberSoftBanned = T('events/guilds-members:guildMemberSoftBanned'); | ||
export const NameUpdateNextWasNotSet = FT<{ nextName: string | null }>('events/guilds-members:nameUpdateNextWasNotSet'); | ||
export const NameUpdateNextWasSet = FT<{ nextName: string | null }>('events/guilds-members:nameUpdateNextWasSet'); | ||
export const NameUpdatePreviousWasNotSet = FT<{ previousName: string | null }>('events/guilds-members:nameUpdatePreviousWasNotSet'); | ||
export const NameUpdatePreviousWasSet = FT<{ previousName: string | null }>('events/guilds-members:nameUpdatePreviousWasSet'); | ||
export const NicknameUpdate = T('events/guilds-members:nicknameUpdate'); | ||
export const RoleUpdate = T('events/guilds-members:roleUpdate'); | ||
export const UsernameUpdate = T('events/guilds-members:usernameUpdate'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Emojis } from '#utils/constants'; | ||
import { userMention } from '@discordjs/builders'; | ||
import { BitField } from '@sapphire/bitfield'; | ||
import { UserFlags, type Snowflake } from 'discord.js'; | ||
|
||
const ExtendedUserFlagBits = new BitField({ | ||
Quarantined: getExtendedBits(UserFlags.Quarantined), | ||
Collaborator: getExtendedBits(UserFlags.Collaborator), | ||
RestrictedCollaborator: getExtendedBits(UserFlags.RestrictedCollaborator) | ||
}); | ||
|
||
export function getModerationFlags(bitfield: number) { | ||
return { | ||
spammer: (bitfield & UserFlags.Spammer) === UserFlags.Spammer, | ||
quarantined: ExtendedUserFlagBits.has(getExtendedBits(bitfield), ExtendedUserFlagBits.flags.Quarantined) | ||
}; | ||
} | ||
|
||
export function getModerationFlagsString(bitfield: number) { | ||
const { spammer, quarantined } = getModerationFlags(bitfield); | ||
if (spammer && quarantined) return Emojis.SpammerIcon + Emojis.QuarantinedIcon; | ||
if (spammer) return Emojis.SpammerIcon; | ||
if (quarantined) return Emojis.QuarantinedIcon; | ||
return ''; | ||
} | ||
|
||
export function getUserMentionWithFlagsString(bitfield: number, userId: Snowflake) { | ||
const flags = getModerationFlagsString(bitfield); | ||
const mention = userMention(userId); | ||
return flags ? `${mention} ${flags}` : mention; | ||
} | ||
|
||
function getExtendedBits(bitfield: number) { | ||
return (bitfield / (1 << 30)) | 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
import { GuildSettings, readSettings } from '#lib/database'; | ||
import { LanguageKeys } from '#lib/i18n/languageKeys'; | ||
import { Events } from '#lib/types'; | ||
import { seconds } from '#utils/common'; | ||
import { Colors } from '#utils/constants'; | ||
import { getLogger, getUserMentionWithFlagsString } from '#utils/functions'; | ||
import { getFullEmbedAuthor } from '#utils/util'; | ||
import { EmbedBuilder } from '@discordjs/builders'; | ||
import { EmbedBuilder, TimestampStyles, time } from '@discordjs/builders'; | ||
import { ApplyOptions } from '@sapphire/decorators'; | ||
import { Listener } from '@sapphire/framework'; | ||
import { isNullish } from '@sapphire/utilities'; | ||
import type { GuildMember } from 'discord.js'; | ||
|
||
const Root = LanguageKeys.Events.Guilds.Members; | ||
const ChannelSettingsKey = GuildSettings.Channels.Logs.MemberAdd; | ||
|
||
@ApplyOptions<Listener.Options>({ event: Events.NotMutedMemberAdd }) | ||
export class UserListener extends Listener { | ||
public async run(member: GuildMember) { | ||
const key = GuildSettings.Channels.Logs.MemberAdd; | ||
const [logChannelId, t] = await readSettings(member, (settings) => [settings[key], settings.getLanguage()]); | ||
if (isNullish(logChannelId)) return; | ||
|
||
this.container.client.emit(Events.GuildMessageLog, member.guild, logChannelId, key, () => | ||
new EmbedBuilder() | ||
.setColor(Colors.Green) | ||
.setAuthor(getFullEmbedAuthor(member.user)) | ||
.setDescription( | ||
t(LanguageKeys.Events.Guilds.Members.GuildMemberAddDescription, { | ||
mention: member.toString(), | ||
time: Date.now() - member.user.createdTimestamp | ||
}) | ||
) | ||
.setFooter({ text: t(LanguageKeys.Events.Guilds.Members.GuildMemberAdd) }) | ||
.setTimestamp() | ||
); | ||
const [t, logChannelId] = await readSettings(member, (settings) => [settings.getLanguage(), settings[ChannelSettingsKey]]); | ||
await getLogger(member.guild).send({ | ||
key: ChannelSettingsKey, | ||
channelId: logChannelId, | ||
makeMessage: () => { | ||
const { user } = member; | ||
const description = t(Root.GuildMemberAddDescription, { | ||
user: getUserMentionWithFlagsString(user.flags?.bitfield ?? 0, user.id), | ||
relativeTime: time(seconds.fromMilliseconds(user.createdTimestamp), TimestampStyles.RelativeTime) | ||
}); | ||
return new EmbedBuilder() | ||
.setColor(Colors.Green) | ||
.setAuthor(getFullEmbedAuthor(member.user)) | ||
.setDescription(description) | ||
.setFooter({ text: t(Root.GuildMemberAdd) }) | ||
.setTimestamp(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters