1
0

move repo from github to gitea

This commit is contained in:
2024-03-14 22:17:39 +01:00
commit de71a376dc
9 changed files with 624 additions and 0 deletions

40
src/commands.ts Normal file
View File

@ -0,0 +1,40 @@
import { ApplicationCommandOptionType } from 'discord.js';
export enum CommandsEnum {
HELP = 'help',
SETUP = 'setup',
}
export const commands = [
{
name: CommandsEnum.HELP,
description: 'This is a help command!'
},
{
name: CommandsEnum.SETUP,
description: 'This is the setup command!',
options: [
{
name: 'first-role',
description: 'This is the first role!',
type: ApplicationCommandOptionType.Role,
required: true
},
{
name: 'second-role',
description: 'This is the second role!',
type: ApplicationCommandOptionType.Role
},
{
name: 'third-role',
description: 'This is the third role!',
type: ApplicationCommandOptionType.Role
},
{
name: 'fourth-role',
description: 'This is the fourth role!',
type: ApplicationCommandOptionType.Role
}
]
},
];

103
src/index.ts Normal file
View File

@ -0,0 +1,103 @@
require('dotenv').config();
import {
Client,
IntentsBitField,
EmbedBuilder,
ActivityType,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
GuildMemberRoleManager
} from 'discord.js';
import { CommandsEnum } from './commands';
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
]
});
client.on('ready', (c) => {
client.user?.setActivity({
name: 'server members 🔎',
type: ActivityType.Watching
});
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
if (interaction.commandName === CommandsEnum.HELP) {
const commandsList = Object.values(CommandsEnum).join(', ');
const embed = new EmbedBuilder()
.setTitle('Role-Wizard')
.setURL('https://gitea.amundsson.eu/n1jos/role-wizard-discord-bot')
.setDescription('A Discord bot for role management.')
.addFields([
{ name: 'Featured commands:', value: commandsList },
{ name: 'Further questions?', value: 'Please contact the creator at the linked github repository.' }
])
.setFooter({
iconURL: client.user?.displayAvatarURL(),
text: `I was useful wasn't I?`
})
.setTimestamp()
.setColor('#000000');
await interaction.reply({ embeds: [embed] });
}
if (interaction.commandName === CommandsEnum.SETUP) {
const commandParameters = interaction.options.data;
const row = new ActionRowBuilder<ButtonBuilder>();
commandParameters.forEach((param) => {
if (param.role) {
row.components.push(
new ButtonBuilder()
.setCustomId(param.role.id)
.setLabel(param.role.name)
.setStyle(ButtonStyle.Primary)
)
}
});
await interaction?.reply({
content: 'Please select your roles:',
components: [row]
});
}
});
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) {
interaction.editReply({
content: 'Role was not found.'
});
return;
};
const userHasRole = (interaction.member?.roles as GuildMemberRoleManager).cache.has(role.id);
if (userHasRole) {
await (interaction.member?.roles as GuildMemberRoleManager).remove(role);
} else {
await (interaction.member?.roles as GuildMemberRoleManager).add(role);
}
await interaction.editReply({
content: `Role ${role.name} has been ${userHasRole ? 'removed' : 'added'}.`
});
});
client.login(process.env.DISCORD_TOKEN)
.then(() => console.log(`✅ Bot is running!`))
.catch((err) => console.error(err));

18
src/register-commands.ts Normal file
View File

@ -0,0 +1,18 @@
import { commands } from './commands';
require('dotenv').config();
const { REST, Routes } = require('discord.js');
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
(async () => {
console.log('Registering slash commands...');
try {
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
{ body: commands }
);
console.log('Successfully registered slash commands!');
} catch (error) {
console.error(error);
}
})();