Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/commands/admin/event/signeduprole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
import { ConfigService } from '#services/configService.js';
import logger from '#utils/logger.js';

export const data = new SlashCommandBuilder()
.setName('signeduprole')
.setDescription('Get or Set the signed up role')
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addRoleOption((option) =>
option
.setName('role')
.setDescription('The role to set as the signed up role')
.setRequired(false)
);

/**
* @param {ChatInputCommandInteraction} interaction
*/
export async function execute(interaction) {
const role = interaction.options.getRole('role');

// if role doesn't exist, get current role if it exists
if (!role) {
const currentRole = await ConfigService.getSignedUpRole(interaction.guildId);
if (currentRole) {
await interaction.reply(`Current signed up role: <@&${currentRole}>`);
} else {
await interaction.reply('No signed up role set.');
}
return;
}

// if role exists, set it as the signed up role
await ConfigService.setSignedUpRole(interaction.guildId, role.id);
await interaction.reply(`Signed up role set to <@&${role.id}>`);
logger.info(`Signed up role set to ${role.id} for guild ${interaction.guildId}`);
}
10 changes: 10 additions & 0 deletions src/commands/event/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ export async function execute(interaction) {
}
}
}
const signedUpRole = await ConfigService.getSignedUpRole(interaction.guildId);
if (signedUpRole) {
const member = await interaction.guild.members.fetch(interaction.user.id);
await member.roles.add(signedUpRole);
logger.info(
`Added signed up role ${signedUpRole} to user ${interaction.user.username} (${interaction.user.id})`
);
} else {
logger.warn(`No signed up role set for guild ${interaction.guildId}`);
}
} catch (error) {
console.log(error);
logger.error('Error registering user to event', error);
Expand Down
1 change: 1 addition & 0 deletions src/constants/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export const SettingTypes = {
APPROVAL_CHANNEL: 'approval_channel',
ACCEPTED_CHANNEL: 'accepted_channel',
SIGNED_UP_CHANNEL: 'signed_up_channel',
SIGNED_UP_ROLE: 'signed_up_role',
};
65 changes: 65 additions & 0 deletions src/services/configService.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,69 @@ export class ConfigService {
throw error;
}
}

/**
* Get Signed up role for a guild
* @param {String} guildId - A Discord guild ID
* @returns {Promise<String>} The signed up role ID
*/
static async getSignedUpRole(guildId) {
try {
const config = await prisma.config.findUnique({
where: {
guildId_settingType: {
guildId,
settingType: SettingTypes.SIGNED_UP_ROLE,
},
},
});

return config?.value;
} catch (error) {
logger.error(`Error getting signed up role for guild ${guildId}:`, error);
throw error;
}
}

/**
* Set the signed up role for a guild
* @param {String} guildId - A Discord guild ID
* @param {String} roleId - The signed up role ID
* @returns {Promise<void>}
*/
static async setSignedUpRole(guildId, roleId) {
try {
const existingConfig = await prisma.config.findUnique({
where: {
guildId_settingType: {
guildId,
settingType: SettingTypes.SIGNED_UP_ROLE,
},
},
});

if (existingConfig) {
await prisma.config.update({
where: {
guildId_settingType: {
guildId,
settingType: SettingTypes.SIGNED_UP_ROLE,
},
},
data: { value: roleId },
});
} else {
await prisma.config.create({
data: {
guildId,
settingType: SettingTypes.SIGNED_UP_ROLE,
value: roleId,
},
});
}
} catch (error) {
logger.error(`Error setting signed up role for guild ${guildId}:`, error);
throw error;
}
}
}