Skip to content

Commit

Permalink
complete part 5
Browse files Browse the repository at this point in the history
  • Loading branch information
notunderctrl committed Dec 30, 2022
1 parent d2a98e3 commit f83279a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 05 - Button Roles/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,29 @@ client.on('ready', (c) => {
console.log(`✅ ${c.user.tag} is online.`);
});

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;
}

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

client.login(process.env.TOKEN);
60 changes: 60 additions & 0 deletions 05 - Button Roles/src/send-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require('dotenv').config();
const {
Client,
IntentsBitField,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
} = require('discord.js');

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});

const roles = [
{
id: '1058030952707784714',
label: 'Red',
},
{
id: '1058031008655609856',
label: 'Green',
},
{
id: '1058031040461013093',
label: 'Blue',
},
];

client.on('ready', async (c) => {
try {
const channel = await client.channels.cache.get('1049345076188422226');
if (!channel) return;

const row = new ActionRowBuilder();
roles.forEach((role) => {
row.components.push(
new ButtonBuilder()
.setCustomId(role.id)
.setLabel(role.label)
.setStyle(ButtonStyle.Primary)
);
});

await channel.send({
content: 'Claim or remove a role below.',
components: [row],
});

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

client.login(process.env.TOKEN);

0 comments on commit f83279a

Please sign in to comment.