feat: add role menu
This commit is contained in:
parent
3367443e32
commit
8bcdddccae
3 changed files with 131 additions and 26 deletions
|
@ -51,6 +51,11 @@ export const reuploadCommands = async () => {
|
||||||
)
|
)
|
||||||
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers)
|
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers)
|
||||||
.setDMPermission(false),
|
.setDMPermission(false),
|
||||||
|
new SlashCommandBuilder()
|
||||||
|
.setName('rolemenu')
|
||||||
|
.setDescription('Make a role menu')
|
||||||
|
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers)
|
||||||
|
.setDMPermission(false),
|
||||||
new SlashCommandBuilder().setName('joke').setDescription("it's a joke"),
|
new SlashCommandBuilder().setName('joke').setDescription("it's a joke"),
|
||||||
].map((command) => command.toJSON());
|
].map((command) => command.toJSON());
|
||||||
|
|
||||||
|
|
22
src/commands/rolemenu.ts
Normal file
22
src/commands/rolemenu.ts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import {
|
||||||
|
ActionRowBuilder,
|
||||||
|
ButtonBuilder,
|
||||||
|
ButtonStyle,
|
||||||
|
CacheType,
|
||||||
|
ChatInputCommandInteraction,
|
||||||
|
} from 'discord.js';
|
||||||
|
|
||||||
|
export const roleMenuCommand = async (
|
||||||
|
i: ChatInputCommandInteraction<CacheType>
|
||||||
|
) => {
|
||||||
|
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId('showRoleMenu')
|
||||||
|
.setLabel('Show role menu')
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
);
|
||||||
|
|
||||||
|
await i.channel?.send({ content: '**Role menu**', components: [row] });
|
||||||
|
|
||||||
|
await i.reply({ content: 'Done!', ephemeral: true });
|
||||||
|
};
|
130
src/index.ts
130
src/index.ts
|
@ -4,6 +4,9 @@ import {
|
||||||
Partials,
|
Partials,
|
||||||
OAuth2Scopes,
|
OAuth2Scopes,
|
||||||
InteractionType,
|
InteractionType,
|
||||||
|
SelectMenuBuilder,
|
||||||
|
ActionRowBuilder,
|
||||||
|
GuildMemberRoleManager,
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { reuploadCommands } from './_reupload';
|
import { reuploadCommands } from './_reupload';
|
||||||
|
|
||||||
|
@ -20,6 +23,7 @@ import { jokeCommand } from './commands/joke';
|
||||||
import random from 'just-random';
|
import random from 'just-random';
|
||||||
import { green, bold, yellow } from 'kleur/colors';
|
import { green, bold, yellow } from 'kleur/colors';
|
||||||
import 'dotenv/config';
|
import 'dotenv/config';
|
||||||
|
import { roleMenuCommand } from './commands/rolemenu';
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [
|
intents: [
|
||||||
|
@ -35,6 +39,14 @@ const client = new Client({
|
||||||
partials: [Partials.Channel],
|
partials: [Partials.Channel],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const allowedRoles = [
|
||||||
|
'Alert',
|
||||||
|
'Events',
|
||||||
|
'Progress',
|
||||||
|
'Lenny is very special and thinks the UK needs a role',
|
||||||
|
'Roly Poly Cult',
|
||||||
|
];
|
||||||
|
|
||||||
client.once('ready', async () => {
|
client.once('ready', async () => {
|
||||||
console.log(green('Discord bot ready!'));
|
console.log(green('Discord bot ready!'));
|
||||||
|
|
||||||
|
@ -91,35 +103,101 @@ client.once('ready', async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('interactionCreate', async (interaction) => {
|
client.on('interactionCreate', async (interaction) => {
|
||||||
if (!interaction.isChatInputCommand()) return;
|
if (interaction.isChatInputCommand()) {
|
||||||
|
const { commandName } = interaction;
|
||||||
|
|
||||||
const { commandName } = interaction;
|
if (commandName === 'ping') {
|
||||||
|
await interaction.reply({
|
||||||
|
content: `Pong! \`${client.ws.ping}ms\``,
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
} else if (commandName === 'members') {
|
||||||
|
await membersCommand(interaction);
|
||||||
|
} else if (commandName === 'stars') {
|
||||||
|
await starsCommand(interaction);
|
||||||
|
} else if (commandName === 'modrinth') {
|
||||||
|
await modrinthCommand(interaction);
|
||||||
|
} else if (commandName === 'rolypoly') {
|
||||||
|
await interaction.reply(
|
||||||
|
'https://media.discordapp.net/attachments/985048903126769764/985051373886382100/rollin-time.gif?width=324&height=216'
|
||||||
|
);
|
||||||
|
} else if (commandName === 'say') {
|
||||||
|
if (!interaction.channel) return;
|
||||||
|
|
||||||
if (commandName === 'ping') {
|
await interaction.deferReply({ ephemeral: true });
|
||||||
await interaction.reply({
|
await interaction.channel.send(interaction.options.getString('content')!);
|
||||||
content: `Pong! \`${client.ws.ping}ms\``,
|
await interaction.editReply('I said what you said!');
|
||||||
ephemeral: true,
|
} else if (commandName === 'tag') {
|
||||||
});
|
await tagsCommand(interaction);
|
||||||
} else if (commandName === 'members') {
|
} else if (commandName === 'joke') {
|
||||||
await membersCommand(interaction);
|
await jokeCommand(interaction);
|
||||||
} else if (commandName === 'stars') {
|
} else if (commandName === 'rolemenu') {
|
||||||
await starsCommand(interaction);
|
await roleMenuCommand(interaction);
|
||||||
} else if (commandName === 'modrinth') {
|
}
|
||||||
await modrinthCommand(interaction);
|
} else if (interaction.isButton()) {
|
||||||
} else if (commandName === 'rolypoly') {
|
if (interaction.customId === 'showRoleMenu') {
|
||||||
await interaction.reply(
|
if (!interaction.guild || !interaction.member) return;
|
||||||
'https://media.discordapp.net/attachments/985048903126769764/985051373886382100/rollin-time.gif?width=324&height=216'
|
|
||||||
);
|
|
||||||
} else if (commandName === 'say') {
|
|
||||||
if (!interaction.channel) return;
|
|
||||||
|
|
||||||
await interaction.deferReply({ ephemeral: true });
|
const roles = await interaction.guild.roles
|
||||||
await interaction.channel.send(interaction.options.getString('content')!);
|
.fetch()
|
||||||
await interaction.editReply('I said what you said!');
|
.then((a) => a.filter((b) => allowedRoles.includes(b.name)));
|
||||||
} else if (commandName === 'tag') {
|
|
||||||
await tagsCommand(interaction);
|
const row = new ActionRowBuilder<SelectMenuBuilder>().addComponents(
|
||||||
} else if (commandName === 'joke') {
|
new SelectMenuBuilder()
|
||||||
await jokeCommand(interaction);
|
.setCustomId('roleMenuSelect')
|
||||||
|
.setPlaceholder('Nothing selected')
|
||||||
|
.addOptions(
|
||||||
|
roles.map((role) => ({
|
||||||
|
label: role.name,
|
||||||
|
value: role.id,
|
||||||
|
default: (
|
||||||
|
interaction.member!.roles as GuildMemberRoleManager
|
||||||
|
).cache.has(role.id),
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
.setMaxValues(roles.toJSON().length)
|
||||||
|
.setMinValues(0)
|
||||||
|
);
|
||||||
|
|
||||||
|
await interaction.reply({
|
||||||
|
content: 'Select your roles here.',
|
||||||
|
components: [row],
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (interaction.isSelectMenu()) {
|
||||||
|
if (interaction.customId === 'roleMenuSelect') {
|
||||||
|
if (!interaction.guild || !interaction.member) return;
|
||||||
|
|
||||||
|
await interaction.deferReply({ ephemeral: true });
|
||||||
|
|
||||||
|
const selectedRoles = interaction.values;
|
||||||
|
|
||||||
|
const roleManager = interaction.member.roles as GuildMemberRoleManager;
|
||||||
|
|
||||||
|
for (const role of allowedRoles) {
|
||||||
|
const roleID = interaction.guild.roles.cache
|
||||||
|
.find((a) => a.name === role)
|
||||||
|
?.id.toString();
|
||||||
|
|
||||||
|
if (!roleID) continue;
|
||||||
|
|
||||||
|
if (roleManager.cache.has(roleID) && !selectedRoles.includes(roleID)) {
|
||||||
|
await roleManager.remove(roleID);
|
||||||
|
}
|
||||||
|
if (!roleManager.cache.has(roleID) && selectedRoles.includes(roleID)) {
|
||||||
|
await roleManager.add(roleID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await interaction.editReply({
|
||||||
|
content: 'Roles updated.',
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
interaction.deleteReply();
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue