From 18fc298194cf92a3fb48c0cf14c86164cba556d5 Mon Sep 17 00:00:00 2001 From: theimperious1 Date: Mon, 30 Sep 2024 13:29:34 -0400 Subject: [PATCH 1/2] Can remove helper role, no intros needed on reapplying it --- src/discord/commands/global/d.setup.ts | 56 ++++++++++++++++--- .../migration.sql | 2 + src/prisma/tripbot/schema.prisma | 1 + 3 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 src/prisma/tripbot/migrations/20240929004151_add_last_was_helper/migration.sql diff --git a/src/discord/commands/global/d.setup.ts b/src/discord/commands/global/d.setup.ts index eb9d04441..328659826 100644 --- a/src/discord/commands/global/d.setup.ts +++ b/src/discord/commands/global/d.setup.ts @@ -652,8 +652,22 @@ export async function helperButton( } // Do everything else + const role = await interaction.guild?.roles.fetch(guildData.role_helper); + const user = await db.users.findUnique({ + where: { + discord_id: target.user.id, + }, + }); + + const userHasBeenAHelper = user?.last_was_helper !== null; + + if (!user) { + log.error(F, `No user found for discord_id: ${target.user.id}`); + return false; + } + if (!role) { await interaction.reply({ content: stripIndents`It looks like the guilds helper role was deleted, talk to the server admin about this! They may need to re-run \`/setup tripsit\``, @@ -662,17 +676,30 @@ export async function helperButton( return; } + // If the role being requested is the Helper or Contributor role, check if they have been banned first + if (role.id === guildData.role_helper && userData.helper_role_ban) { + await interaction.editReply({ content: 'Unable to add this role. If you feel this is an error, please talk to the team!' }); + return; + } + if (target.roles.cache.has(role.id)) { + await target.roles.remove(role); await interaction.reply({ - content: stripIndents`You already have the helper role!`, + content: stripIndents`Your helper role has been removed. If you ever want to re-apply it, just click the button again!`, ephemeral: true, }); return; - } - - // If the role being requested is the Helper or Contributor role, check if they have been banned first - if (role.id === guildData.role_helper && userData.helper_role_ban) { - await interaction.editReply({ content: 'Unable to add this role. If you feel this is an error, please talk to the team!' }); + } else if (userHasBeenAHelper && !target.roles.cache.has(role.id)) { + await target.roles.add(role); + if (interaction.guild.id === env.DISCORD_GUILD_ID) { + const channelTripsitters = await interaction.guild?.channels.fetch(env.CHANNEL_TRIPSITTERS) as TextChannel; + await channelTripsitters.send(stripIndents`${target.displayName} has re-joined as a ${role.name}.`); + } + const metaChannel = await interaction.guild?.channels.fetch(guildData.channel_tripsitmeta) as TextChannel; + await interaction.reply({ + content: stripIndents`Welcome back, go check out ${metaChannel}!`, + ephemeral: true, + }); return; } @@ -746,8 +773,22 @@ export async function helperButton( // log.debug(F, `introMessage: ${introMessage}`); await target.roles.add(role); + + // Update the last date when they were given the helper role + await db.users.upsert({ + where: { + discord_id: interaction.user.id, + }, + create: { + discord_id: interaction.user.id, + }, + update: { + last_was_helper: new Date() + }, + }); + const metaChannel = await i.guild?.channels.fetch(guildData.channel_tripsitmeta) as TextChannel; - await i.editReply({ content: `Added role ${role.name}, go check out ${metaChannel}!` }); + await i.editReply({ content: `Added role ${role.name}, go check out ${metaChannel}! If you ever want to remove it, just click the button again.` }); if (metaChannel.id === guildData.channel_tripsitmeta) { const introString = ` @@ -801,7 +842,6 @@ export async function helperButton( **If you have any questions, please reach out!** `); } - if (i.guild.id === env.DISCORD_GUILD_ID) { const channelTripsitters = await i.guild?.channels.fetch(env.CHANNEL_TRIPSITTERS) as TextChannel; const roleTripsitter = await i.guild?.roles.fetch(guildData.role_tripsitter) as Role; diff --git a/src/prisma/tripbot/migrations/20240929004151_add_last_was_helper/migration.sql b/src/prisma/tripbot/migrations/20240929004151_add_last_was_helper/migration.sql new file mode 100644 index 000000000..cb0d1a5d6 --- /dev/null +++ b/src/prisma/tripbot/migrations/20240929004151_add_last_was_helper/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "users" ADD COLUMN "last_was_helper" TIMESTAMP(3); diff --git a/src/prisma/tripbot/schema.prisma b/src/prisma/tripbot/schema.prisma index a815453dd..02764e69a 100644 --- a/src/prisma/tripbot/schema.prisma +++ b/src/prisma/tripbot/schema.prisma @@ -174,6 +174,7 @@ model users { wordle_scores wordle_scores[] connections_scores connections_scores[] mini_scores mini_scores[] + last_was_helper DateTime? } model wordle_scores { From 3e289cd3a533591b19f74870df5273e0c8ee616e Mon Sep 17 00:00:00 2001 From: theimperious1 Date: Mon, 30 Sep 2024 13:37:31 -0400 Subject: [PATCH 2/2] Fix lint and broken if else, whoops lol --- src/discord/commands/global/d.setup.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/discord/commands/global/d.setup.ts b/src/discord/commands/global/d.setup.ts index 328659826..ef70716e2 100644 --- a/src/discord/commands/global/d.setup.ts +++ b/src/discord/commands/global/d.setup.ts @@ -660,12 +660,12 @@ export async function helperButton( discord_id: target.user.id, }, }); - + const userHasBeenAHelper = user?.last_was_helper !== null; if (!user) { log.error(F, `No user found for discord_id: ${target.user.id}`); - return false; + return; } if (!role) { @@ -689,7 +689,9 @@ export async function helperButton( ephemeral: true, }); return; - } else if (userHasBeenAHelper && !target.roles.cache.has(role.id)) { + } + + if (userHasBeenAHelper && !target.roles.cache.has(role.id)) { await target.roles.add(role); if (interaction.guild.id === env.DISCORD_GUILD_ID) { const channelTripsitters = await interaction.guild?.channels.fetch(env.CHANNEL_TRIPSITTERS) as TextChannel; @@ -783,7 +785,7 @@ export async function helperButton( discord_id: interaction.user.id, }, update: { - last_was_helper: new Date() + last_was_helper: new Date(), }, });