diff options
| author | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2026-07-23 22:39:33 +0100 |
|---|---|---|
| committer | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2026-07-23 22:39:33 +0100 |
| commit | 4cccbdda3496aad74bdb3b4b2ad746f4367b13d9 (patch) | |
| tree | 38f8d27bee36eab2295dc5a2b044e6075bd20faf /174bg/discord-bot | |
| parent | 2d7362b6fc73f4807f09f6f522fd2e1e2b306e04 (diff) | |
| download | unnamed-group-4cccbdda3496aad74bdb3b4b2ad746f4367b13d9.tar unnamed-group-4cccbdda3496aad74bdb3b4b2ad746f4367b13d9.tar.gz unnamed-group-4cccbdda3496aad74bdb3b4b2ad746f4367b13d9.tar.bz2 unnamed-group-4cccbdda3496aad74bdb3b4b2ad746f4367b13d9.tar.xz unnamed-group-4cccbdda3496aad74bdb3b4b2ad746f4367b13d9.zip | |
opti refactor
Diffstat (limited to '174bg/discord-bot')
4 files changed, 47 insertions, 35 deletions
diff --git a/174bg/discord-bot/source/commands/requisition.js b/174bg/discord-bot/source/commands/requisition.js index 02c7699..7c547d5 100644 --- a/174bg/discord-bot/source/commands/requisition.js +++ b/174bg/discord-bot/source/commands/requisition.js @@ -1,6 +1,6 @@ import { SlashCommandBuilder } from "discord.js"; import createTicket from "../utilities/createTicket.js"; -import notifyRoleByName from "../utilities/notifyRoleByName.js"; +import messageMembersByRoleName from "../utilities/messageMembersByRoleName.js"; export default { data: new SlashCommandBuilder() @@ -33,7 +33,7 @@ export default { }); // notify quartermasters - await notifyRoleByName( + await messageMembersByRoleName( interaction.guild, "quartermaster", `New requisition request from <@${interaction.user.id}>: ${contents}`, diff --git a/174bg/discord-bot/source/utilities/createTicket.js b/174bg/discord-bot/source/utilities/createTicket.js index 6289715..98a146d 100644 --- a/174bg/discord-bot/source/utilities/createTicket.js +++ b/174bg/discord-bot/source/utilities/createTicket.js @@ -1,38 +1,39 @@ import { ChannelType, PermissionFlagsBits } from "discord.js"; -export default async ( +/** + * Creates a new ticket channel under the "tickets" category with the specified options. + * + * @param {Interaction} interaction The interaction object from Discord.js. + * @param {Object} options The options for creating the ticket. + * @param {Array<string>} options.roleNamesWithAccess The names of the roles that should have access to the ticket. + * @param {string} options.ticketNamePrefix The prefix for the ticket channel name. + * + * @returns {Promise<Channel>} The newly created ticket channel. + */ +export default async function createTicket( interaction, options = { roleNamesWithAccess: [], ticketNamePrefix: "ticket-" }, -) => { +) { const ticketsCategory = interaction.guild.channels.cache.find( (channel) => channel.name.toLowerCase() === "tickets" && channel.type === ChannelType.GuildCategory, ); - if (!ticketsCategory) - return await interaction.reply("Tickets category not found."); + if (!ticketsCategory) throw new Error("Tickets category not found in the guild."); - // get the roles that should have access to this ticket const rolesWithAccess = options.roleNamesWithAccess.map((roleName) => { return interaction.guild.roles.cache.find( (role) => role.name.toLowerCase() === roleName.toLowerCase(), ); }); - // create a new text channel under the "tickets" category with the following format: ticket-<timestamp_short> - const timestamp = new Date() - .toISOString() - .replace(/[-:.T]/g, "") - .slice(2, 16); - - const channelName = `${options.ticketNamePrefix}${timestamp}`; + const timestamp = Date.now().toString(36); // base36 const newChannel = await interaction.guild.channels.create({ - name: channelName, + name: `${options.ticketNamePrefix}-${timestamp}`, type: ChannelType.GuildText, parent: ticketsCategory.id, - // set permissions so that only the user who created the ticket and the roles with access can view it permissionOverwrites: [ { id: interaction.user.id, diff --git a/174bg/discord-bot/source/utilities/messageMembersByRoleName.js b/174bg/discord-bot/source/utilities/messageMembersByRoleName.js new file mode 100644 index 0000000..0730c32 --- /dev/null +++ b/174bg/discord-bot/source/utilities/messageMembersByRoleName.js @@ -0,0 +1,30 @@ +/** + * Messages all members of a specific role in a Discord guild by the role's name. + * + * @param {Guild} guild The Discord guild. + * @param {string} roleName The name of the role. + * @param {string} message The message to send. + * + * @returns {Promise<{successes: Array, failures: Array}>} The results of the messaging attempts. + */ +export default async function messageMembersByRoleName(guild, roleName, message) { + const role = guild.roles.cache.find( + (role) => role.name.toLowerCase() === roleName.toLowerCase(), + ); + + if (!role) throw new Error(`Role "${roleName}" not found.`); + + let successes = []; + let failures = []; + + for (const member of role.members.values()) { + try { + let result = await member.send(message); + successes.push(result); + } catch (error) { + failures.push({ member, error }); + } + } + + return { successes, failures }; +} diff --git a/174bg/discord-bot/source/utilities/notifyRoleByName.js b/174bg/discord-bot/source/utilities/notifyRoleByName.js deleted file mode 100644 index 1b2b82a..0000000 --- a/174bg/discord-bot/source/utilities/notifyRoleByName.js +++ /dev/null @@ -1,19 +0,0 @@ -export default async (guild, roleName, message) => { - // get the role by name - const role = guild.roles.cache.find( - (role) => role.name.toLowerCase() === roleName.toLowerCase(), - ); - - if (!role) { - console.error(`Role "${roleName}" not found.`); - return; - } - - // get all members with the role - const membersWithRole = role.members; - - // send a DM to each member with the role - for (const member of membersWithRole.values()) { - await member.send(message); - } -}; |
