|
| 1 | +import { SlashCommandBuilder, EmbedBuilder } from "discord.js"; |
| 2 | +import mysql from "mysql"; |
| 3 | +const allowedroleID = "" // do this later when I have perms |
| 4 | +const logchannel = "" |
| 5 | +const connection = mysql.createConnection({ |
| 6 | + // credientials to be added soon |
| 7 | +}); |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + data: new SlashCommandBuilder() |
| 11 | + .setName("log") |
| 12 | + .setDescription("Logs offical events for KOG."), |
| 13 | + |
| 14 | + async execute(interaction) { |
| 15 | + try { |
| 16 | + if (!interaction.member.role.cache.has(allowedroleID)) { |
| 17 | + const noperms = new EmbedBuilder() |
| 18 | + .setColor("#E73A3A") |
| 19 | + .setTitle("Error") |
| 20 | + .setDescription("You don't have the required role to use this command.") |
| 21 | + .setTimestamp() |
| 22 | + |
| 23 | + await interaction.reply({ embeds: [noperms], ephemeral: true}) |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + const logStart = new EmbedBuilder() |
| 28 | + .setColor("#9033FF") |
| 29 | + .setTitle("Logging") |
| 30 | + .setDescription("To log an event, please follow the following format:\n\n<@1344176447551574078>,<@1138235120424325160>,<@110877167897853952>,<@573540579682811906> and so on.\n\nNames must be seperated by commas and must be mentions. Otherwise you'll be asked to re do it.\n\nYou have 2 minutes to submit attendees, before this times out.\n\nTo cancel, please type **cancel**.") |
| 31 | + .setTimestamp() |
| 32 | + |
| 33 | + await interaction.reply({ embeds: [logStart], ephemeral: true}) |
| 34 | + |
| 35 | + const filter = (message) => message.author.id === interaction.user.id && message.channel.id === interaction.channel.id; |
| 36 | + const input = await interaction.channel.awaitMessages({ filter, max: 1, time: 60000, errors: ['time'] }); |
| 37 | + const response = userInput.first().content; |
| 38 | + |
| 39 | + const mentionRegexthing = /^<@\d+>(?:,\s?<@\d+>)*$/; |
| 40 | + |
| 41 | + if (!mentionRegexthing.test(response)) { |
| 42 | + return interaction.followUp("Invalid format. Please make sure the names are separated by commas and each name is a mention. Run the command again and use the correct format."); |
| 43 | + } |
| 44 | + |
| 45 | + if (error instanceof Error && error.message === 'time') { |
| 46 | + return interaction.reply(`<@${interaction.user.id}> you took too long to follow up, please try again.`); |
| 47 | + } |
| 48 | + if (response.toLowerCase() === 'cancel') { |
| 49 | + return interaction.reply(`<@${interaction.user.id}> canceled the event log.`); |
| 50 | + } |
| 51 | + |
| 52 | + const mentions = response.split(',').map(id => id.trim().replace('<@', '').replace('>', '')); |
| 53 | + const userIds = [] |
| 54 | + |
| 55 | + for (const mention of mentions) { |
| 56 | + const userId = mention.slice(2, -1) |
| 57 | + if (userId) |
| 58 | + userIds.push(userId) |
| 59 | + } |
| 60 | + |
| 61 | + if (userIds.length === 0) { |
| 62 | + return interaction.reply("No users mentioned. Please try again."); |
| 63 | + } |
| 64 | + const timestamp = Math.floor(Date.now() / 1000); |
| 65 | + const logEmbed = new EmbedBuilder() |
| 66 | + .setColor("#9033FF") |
| 67 | + .setTitle("Log Event") |
| 68 | + .setDescription(`A new event was logged.\n\nHost: ${interaction.user.id}\n\nTime: <t:${timestamp}:F>\n\nAttendees: ${mentions.map(id => `<@${id}>`).join(', ')}\n\nSquadron Rally: False`) // set to false until we integrate with squads?? |
| 69 | + .setTimestamp() |
| 70 | + |
| 71 | + await logchannel.send({embeds: [logStart], ephemeral: false}) |
| 72 | + |
| 73 | + // database stuff will go here: example |
| 74 | + |
| 75 | + if ("We have updated the DB") { |
| 76 | + const DB = new EmbedBuilder() |
| 77 | + .setColor("#9033FF") |
| 78 | + .setTitle("Log Event") |
| 79 | + .setDescription(`Database updated, event has been logged succesfully.`) |
| 80 | + .setTimestamp() |
| 81 | + |
| 82 | + await logchannel.send({embeds: [DB], ephemeral: false}) |
| 83 | + } |
| 84 | + else { |
| 85 | + const errorEmbed = new EmbedBuilder() |
| 86 | + .setColor("#E73A3A") |
| 87 | + .setTitle("Error") |
| 88 | + .setDescription("An error occurred while updating the DB. The database has not been updated and the log has failed\nContact <@1344176447551574078> or <@1138235120424325160>") |
| 89 | + |
| 90 | + await logchannel.send({embeds: [errorEmbed], ephemeral: false}) |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + } catch (error) { |
| 95 | + console.log(error) |
| 96 | + const errorEmbed = new EmbedBuilder() |
| 97 | + .setColor("#E73A3A") |
| 98 | + .setTitle("Error") |
| 99 | + .setDescription("An error occurred while executing this command.") |
| 100 | + |
| 101 | + await interaction.reply({embeds: [errorEmbed], ephemeral: true}) |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + } |
0 commit comments