Skip to content

Commit

Permalink
Update part 5
Browse files Browse the repository at this point in the history
  • Loading branch information
notunderctrl committed Dec 30, 2022
1 parent f83279a commit df1d4f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
45 changes: 24 additions & 21 deletions 05 - Button Roles/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,31 @@ client.on('ready', (c) => {
});

client.on('interactionCreate', async (interaction) => {
if (!interaction.isButton()) return;

await interaction.deferReply({ ephemeral: true });

const role = interaction.guild.roles.cache.get(interaction.customId);
if (!role) {
await interaction.deferReply({
content: "I couldnt' find that role",
});
return;
}

const hasRole = await interaction.member.roles.cache.has(role.id);

if (hasRole) {
await interaction.member.roles.remove(role);
await interaction.editReply(`The role ${role} has been removed.`);
return;
try {
if (!interaction.isButton()) return;
await interaction.deferReply({ ephemeral: true });

const role = interaction.guild.roles.cache.get(interaction.customId);
if (!role) {
interaction.editReply({
content: "I couldn't find that role",
});
return;
}

const hasRole = interaction.member.roles.cache.has(role.id);

if (hasRole) {
await interaction.member.roles.remove(role);
await interaction.editReply(`The role ${role} has been removed.`);
return;
}

await interaction.member.roles.add(role);
await interaction.editReply(`The role ${role} has been added.`);
} catch (error) {
console.log(error);
}

await interaction.member.roles.add(role);
await interaction.editReply(`The role ${role} has been added.`);
});

client.login(process.env.TOKEN);
2 changes: 1 addition & 1 deletion 05 - Button Roles/src/send-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ client.on('ready', async (c) => {
if (!channel) return;

const row = new ActionRowBuilder();

roles.forEach((role) => {
row.components.push(
new ButtonBuilder()
Expand All @@ -50,7 +51,6 @@ client.on('ready', async (c) => {
content: 'Claim or remove a role below.',
components: [row],
});

process.exit();
} catch (error) {
console.log(error);
Expand Down

0 comments on commit df1d4f7

Please sign in to comment.