diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-01-15 18:04:38 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-01-15 18:04:38 -0500 |
commit | 8ef5ea8282ba210ec04fecae1de1b9b7cb48452b (patch) | |
tree | c063213fd52b99235d03f9d11df4562c793d87a0 /src | |
parent | 323f604f45778f4620d5de7558d828349811aca7 (diff) | |
download | tanzanite-8ef5ea8282ba210ec04fecae1de1b9b7cb48452b.tar.gz tanzanite-8ef5ea8282ba210ec04fecae1de1b9b7cb48452b.tar.bz2 tanzanite-8ef5ea8282ba210ec04fecae1de1b9b7cb48452b.zip |
make join roles only trigger after a member is no longer pending (accepted welcome screen)
Diffstat (limited to 'src')
-rw-r--r-- | src/listeners/guild/guildMemberAdd.ts | 80 | ||||
-rw-r--r-- | src/listeners/guild/guildMemberRemove.ts | 4 | ||||
-rw-r--r-- | src/listeners/guild/joinRoles.ts | 119 |
3 files changed, 124 insertions, 79 deletions
diff --git a/src/listeners/guild/guildMemberAdd.ts b/src/listeners/guild/guildMemberAdd.ts index 4c7d498..22961e9 100644 --- a/src/listeners/guild/guildMemberAdd.ts +++ b/src/listeners/guild/guildMemberAdd.ts @@ -1,5 +1,5 @@ -import { BushListener, StickyRole, type BushClientEvents, type BushGuildMember, type BushTextChannel } from '#lib'; -import { MessageEmbed, type Snowflake } from 'discord.js'; +import { BushListener, type BushClientEvents, type BushGuildMember, type BushTextChannel } from '#lib'; +import { MessageEmbed } from 'discord.js'; export default class GuildMemberAddListener extends BushListener { public constructor() { @@ -12,10 +12,9 @@ export default class GuildMemberAddListener extends BushListener { public override async exec(...[member]: BushClientEvents['guildMemberAdd']) { void this.sendWelcomeMessage(member); - void this.joinAndStickyRoles(member); } - public async sendWelcomeMessage(member: BushGuildMember) { + private async sendWelcomeMessage(member: BushGuildMember) { if (client.config.isDevelopment) return; const welcomeChannel = await member.guild.getSetting('welcomeChannel'); if (!welcomeChannel) return; @@ -44,77 +43,4 @@ export default class GuildMemberAddListener extends BushListener { ) ); } - - public async joinAndStickyRoles(member: BushGuildMember) { - if (client.config.isDevelopment) return; - if (await member.guild.hasFeature('stickyRoles')) { - const hadRoles = await StickyRole.findOne({ where: { guild: member.guild.id, user: member.id } }); - if (hadRoles?.roles?.length) { - const rolesArray = hadRoles.roles - .map((roleID: Snowflake) => { - const role = member.guild.roles.cache.get(roleID); - if (role && !member.roles.cache.has(roleID)) { - if (role.name !== '@everyone' || !role.managed) return role.id; - } - }) - .filter((role) => role) as Snowflake[]; - if (hadRoles.nickname && member.manageable) { - void member.setNickname(hadRoles.nickname).catch(() => {}); - } - if (rolesArray?.length) { - const addedRoles = await member.roles.add(rolesArray, "Returning member's previous roles.").catch(() => - member.guild.sendLogChannel('error', { - embeds: [ - { - title: 'Sticky Roles Error', - description: `There was an error returning ${util.format.input(member.user.tag)}'s roles.`, - color: util.colors.error - } - ] - }) - ); - if (addedRoles) { - void client.console.info( - 'guildMemberAdd', - `Assigned sticky roles to ${util.format.inputLog(member.user.tag)} in ${util.format.inputLog(member.guild.name)}.` - ); - } else if (!addedRoles) { - const failedRoles: string[] = []; - for (let i = 0; i < rolesArray.length; i++) { - await member.roles - .add(rolesArray[i], "[Fallback] Returning member's previous roles.") - .catch(() => failedRoles.push(rolesArray[i])); - } - if (failedRoles.length) { - void client.console.warn('guildMemberAdd', `Failed assigning the following roles on Fallback: ${failedRoles}`); - } else { - void client.console.info( - 'guildMemberAdd', - `[Fallback] Assigned sticky roles to ${util.format.inputLog(member.user.tag)} in ${util.format.inputLog( - member.guild.name - )}.` - ); - } - } - } - } else { - const joinRoles = await member.guild.getSetting('joinRoles'); - if (!joinRoles || !joinRoles.length) return; - await member.roles - .add(joinRoles, 'Join roles.') - .then(() => - client.console.info( - 'guildMemberAdd', - `Assigned join roles to ${util.format.inputLog(member.user.tag)} in ${util.format.inputLog(member.guild.name)}.` - ) - ) - .catch(() => - member.guild.error( - 'Join Roles Error', - `Failed to assign join roles to ${util.format.input(member.user.tag)}, in ${util.format.input(member.guild.name)}.` - ) - ); - } - } - } } diff --git a/src/listeners/guild/guildMemberRemove.ts b/src/listeners/guild/guildMemberRemove.ts index ae4c4df..323bd24 100644 --- a/src/listeners/guild/guildMemberRemove.ts +++ b/src/listeners/guild/guildMemberRemove.ts @@ -22,7 +22,7 @@ export default class GuildMemberRemoveListener extends BushListener { void this.stickyRoles(member); } - public async sendWelcomeMessage(member: BushGuildMember | PartialBushGuildMember) { + private async sendWelcomeMessage(member: BushGuildMember | PartialBushGuildMember) { if (client.config.isDevelopment) return; const user = member.partial ? await client.users.fetch(member.id) : member.user; await util.sleep(0.05); // ban usually triggers after member leave @@ -54,7 +54,7 @@ export default class GuildMemberRemoveListener extends BushListener { ); } - public async stickyRoles(member: BushGuildMember | PartialBushGuildMember) { + private async stickyRoles(member: BushGuildMember | PartialBushGuildMember) { if (!(await member.guild.hasFeature('stickyRoles'))) return; if (member.partial) { await member.guild.members.fetch(); // try to prevent in the future diff --git a/src/listeners/guild/joinRoles.ts b/src/listeners/guild/joinRoles.ts new file mode 100644 index 0000000..a50f7ed --- /dev/null +++ b/src/listeners/guild/joinRoles.ts @@ -0,0 +1,119 @@ +import { BushListener, StickyRole, type BushClientEvents, type BushGuildMember } from '#lib'; +import { type Snowflake } from 'discord.js'; + +export default class JoinRolesListener extends BushListener { + public constructor() { + super('joinRoles', { + emitter: 'client', + event: 'guildMemberUpdate', // listens to guildMemberUpdate so that the role's aren't given before the member accepts the welcome screen + category: 'guild' + }); + } + + public override async exec(...[oldMember, newMember]: BushClientEvents['guildMemberUpdate']) { + if (client.config.isDevelopment) return; + if (oldMember.pending === false && newMember.pending === true) { + const feat = { + stickyRoles: await newMember.guild.hasFeature('stickyRoles'), + joinRoles: (await newMember.guild.getSetting('joinRoles')).length > 0 + }; + + if (!feat.stickyRoles && !feat.joinRoles) return; + + let addJoinRoles = true; + if (feat.stickyRoles) { + const addedStickyRoles = await this.stickyRoles(newMember); + if (addedStickyRoles) addJoinRoles = false; + } + + if (feat.joinRoles && addJoinRoles) { + void this.joinRoles(newMember); + } + } + } + + /** + * Adds sticky roles to a user. + * @param member The member to add sticky roles to. + * @returns Whether or not sticky roles were added. + */ + private async stickyRoles(member: BushGuildMember): Promise<boolean> { + const hadRoles = await StickyRole.findOne({ where: { guild: member.guild.id, user: member.id } }); + + if (hadRoles?.roles?.length) { + const rolesArray = hadRoles.roles + .map((roleID: Snowflake) => { + const role = member.guild.roles.cache.get(roleID); + if (role && !member.roles.cache.has(roleID)) { + if (role.name !== '@everyone' || !role.managed) return role.id; + } + }) + .filter((role) => role) as Snowflake[]; + if (hadRoles.nickname && member.manageable) { + void member.setNickname(hadRoles.nickname).catch(() => {}); + } + if (rolesArray?.length) { + const addedRoles = await member.roles.add(rolesArray, "Returning member's previous roles.").catch(() => { + void member.guild.sendLogChannel('error', { + embeds: [ + { + title: 'Sticky Roles Error', + description: `There was an error returning ${util.format.input(member.user.tag)}'s roles.`, + color: util.colors.error + } + ] + }); + return false as const; + }); + if (addedRoles) { + void client.console.info( + 'guildMemberAdd', + `Assigned sticky roles to ${util.format.inputLog(member.user.tag)} in ${util.format.inputLog(member.guild.name)}.` + ); + } else if (!addedRoles) { + const failedRoles: string[] = []; + for (let i = 0; i < rolesArray.length; i++) { + await member.roles + .add(rolesArray[i], "[Fallback] Returning member's previous roles.") + .catch(() => failedRoles.push(rolesArray[i])); + } + if (failedRoles.length) { + void client.console.warn('guildMemberAdd', `Failed assigning the following roles on Fallback: ${failedRoles}`); + } else { + void client.console.info( + 'guildMemberAdd', + `[Fallback] Assigned sticky roles to ${util.format.inputLog(member.user.tag)} in ${util.format.inputLog( + member.guild.name + )}.` + ); + } + } + return true; + } + } + return false; + } + + /** + * Add the guild's join roles to the member. + * @param member The member to add the join roles to. + */ + private async joinRoles(member: BushGuildMember): Promise<void> { + const joinRoles = await member.guild.getSetting('joinRoles'); + if (!joinRoles || !joinRoles.length) return; + await member.roles + .add(joinRoles, 'Join roles.') + .then(() => + client.console.info( + 'guildMemberAdd', + `Assigned join roles to ${util.format.inputLog(member.user.tag)} in ${util.format.inputLog(member.guild.name)}.` + ) + ) + .catch(() => + member.guild.error( + 'Join Roles Error', + `Failed to assign join roles to ${util.format.input(member.user.tag)}, in ${util.format.input(member.guild.name)}.` + ) + ); + } +} |