Guest User

Untitled

a guest
May 3rd, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import {
  3.     ActionRowBuilder,
  4.     ButtonBuilder,
  5.     ButtonStyle,
  6.     MessageFlags
  7. } from 'discord.js';
  8.  
  9. import Battle       from '../data/Battle.js';
  10. import BattleMember from '../data/BattleMember.js';
  11. import Trainer      from '../data/Trainer.js';
  12.  
  13. const BattleActionButtons = {
  14.     data: {
  15.         name: 'BattleAction',
  16.         button: {
  17.             Join: 'Join',
  18.             Leave: 'Leave',
  19.             Start: 'Start',
  20.             Cancel: 'Cancel'
  21.         }      
  22.     },
  23.  
  24.     async build(interaction) {
  25.         // Create the buttons
  26.         const joinButton = new ButtonBuilder()
  27.             .setCustomId(`${this.data.name}.${this.data.button.Join}`)
  28.             .setLabel(this.data.button.Join)
  29.             .setStyle(ButtonStyle.Primary);
  30.        
  31.         const leaveButton = new ButtonBuilder()
  32.             .setCustomId(`${this.data.name}.${this.data.button.Leave}`)
  33.             .setLabel(this.data.button.Leave)
  34.             .setStyle(ButtonStyle.Secondary);
  35.  
  36.         const startButton = new ButtonBuilder()
  37.             .setCustomId(`${this.data.name}.${this.data.button.Start}`)
  38.             .setLabel(this.data.button.Start)
  39.             .setStyle(ButtonStyle.Success);
  40.        
  41.         const cancelButton = new ButtonBuilder()
  42.             .setCustomId(`${this.data.name}.${this.data.button.Cancel}`)
  43.             .setLabel(this.data.button.Cancel)
  44.             .setStyle(ButtonStyle.Danger);
  45.        
  46.         return new ActionRowBuilder()
  47.             .addComponents(joinButton, leaveButton, startButton, cancelButton);
  48.     },
  49.    
  50.     async handle(interaction) {
  51.         const client = interaction.client;
  52.         const message = interaction.message;
  53.         const action = interaction.customId.split('.')[1];
  54.  
  55.         const battleRec  = await Battle.get({ messageId: message.id, unique: true });
  56.         const trainerRec = await Trainer.get({id: interaction.user.id, unique: true});
  57.  
  58.         // Log some stuff for debugging
  59.         client.logger.debug(`Handling Battle Action Button for Message ID = ${message.id}`);
  60.         client.logger.debug(`Action = ${action}`);
  61.         client.logger.debug(`Battle Record = `);
  62.         client.logger.dump(battleRec);
  63.         client.logger.debug(`Trainer Record =`);
  64.         client.logger.dump(trainerRec);
  65.  
  66.         if (!trainerRec) {
  67.             interaction.reply(Trainer.getSetupTrainerFirstMessage());
  68.             return;
  69.         }
  70.        
  71.         switch (action) {
  72.             case this.data.button.Join: this.handleJoin(interaction, battleRec, trainerRec); break;
  73.             case this.data.button.Leave: this.handleLeave(interaction, battleRec, trainerRec); break;
  74.             case this.data.button.Start: this.handleStart(interaction, battleRec, trainerRec); break;
  75.             case this.data.button.Cancel: this.handleCancel(interaction, battleRec, trainerRec); break;
  76.         }
  77.     },
  78.  
  79.     async handleJoin(interaction, battleRec, trainerRec) {
  80.         const client = interaction.client;
  81.         const hostTrainerRec = await Trainer.get({ id: battleRec.hostTrainerId, unique: true });
  82.        
  83.         // Check if the host is trying to join the raid
  84.         //if (battleRec.hostTrainerId == trainerRec.id) {
  85.         //    await interaction.reply({
  86.         //        content: `You cannot join a raid that you are hosting`,
  87.         //        flags: MessageFlags.Ephemeral
  88.         //    });
  89.         //    return;
  90.         //}
  91.  
  92.         // Check if this trainer has already joined the raid
  93.         let battleMemberObj = {
  94.             battleId: battleRec.id,
  95.             trainerId: trainerRec.id,
  96.             unique: true
  97.         };
  98.         let battleMemberRec = await BattleMember.get(battleMemberObj);
  99.  
  100.         if (battleMemberRec) {
  101.             await interaction.reply({
  102.                 content: `You have already joined this raid`,
  103.                 flags: MessageFlags.Ephemeral
  104.             });
  105.             return;
  106.         }
  107.  
  108.         // Join the trainer to this raid
  109.         battleMemberRec = new BattleMember(battleMemberObj);
  110.         await battleMemberRec.create();
  111.  
  112.         hostTrainerRec.formattedCode
  113.  
  114.         //await client.channels.fetch(interaction.message.channelId);
  115.  
  116.         // Update the embed (noting we have to make sure the channel is in the cache first)
  117.         const battleEmbed = await battleRec.buildEmbed();
  118.         await interaction.message.edit({
  119.             embeds: [battleEmbed]
  120.         });
  121.  
  122.         await interaction.reply({
  123.             content: `You have joined this raid, please make sure to add the host to your friends list`,
  124.             flags: MessageFlags.Ephemeral
  125.         });
  126.  
  127.         await interaction.followUp({
  128.             content: `${hostTrainerRec.formattedCode} -- ${hostTrainerRec.name}`,
  129.             flags: MessageFlags.Ephemeral
  130.         });
  131.  
  132.         // Debug some stuff
  133.         client.logger.debug(`Interaction Message =`);
  134.         client.logger.dump(interaction.message);
  135.     },
  136.  
  137.     async handleLeave(interaction, battleRec, trainerRec) {
  138.         const client = interaction.client;
  139.  
  140.         // Check if the host is trying to leave the raid
  141.         //if (battleRec.hostTrainerId == trainerRec.id) {
  142.         //    await interaction.reply({
  143.         //        content: `You cannot leave a raid that you are hosting, please click ${this.data.button.Cancel} to cancel this raid`,
  144.         //        flags: MessageFlags.Ephemeral
  145.         //    });
  146.         //    return;
  147.         //}
  148.  
  149.         // Check if this trainer has not yet joined the raid
  150.         let battleMemberObj = {
  151.             battleId: battleRec.id,
  152.             trainerId: trainerRec.id,
  153.             unique: true
  154.         };
  155.         let battleMemberRec = await BattleMember.get(battleMemberObj);
  156.  
  157.         if (!battleMemberRec) {
  158.             await interaction.reply({
  159.                 content: `You have not yet joined this raid`,
  160.                 flags: MessageFlags.Ephemeral
  161.             });
  162.             return;
  163.         }
  164.  
  165.         // Remove the trainer from this raid
  166.         await battleMemberRec.delete();
  167.  
  168.         //await client.channels.fetch(interaction.message.channelId);
  169.        
  170.         // Update the embed (noting we have to make sure the channel is in the cache first)
  171.         const battleEmbed = await battleRec.buildEmbed();
  172.         await interaction.message.edit({
  173.             embeds: [battleEmbed]
  174.         });
  175.  
  176.         await interaction.reply({
  177.             content: `You have left this raid`,
  178.             flags: MessageFlags.Ephemeral
  179.         });
  180.  
  181.         // Debug some stuff
  182.         client.logger.debug(`Interaction Message =`);
  183.         client.logger.dump(interaction.message);
  184.     },
  185.  
  186.     async handleStart(interaction, battleRec, trainerRec) {
  187.         const client = interaction.client;
  188.  
  189.         //await interaction.reply({
  190.         //    content: `Battle Action Button Not Processed Yet - ${this.data.button.Start}`,
  191.         //    flags: MessageFlags.Ephemeral
  192.         //});
  193.  
  194.         // Update the embed (noting we have to make sure the channel is in the cache first)
  195.         const battleEmbed = await battleRec.buildEmbed();
  196.         //await client.channels.fetch(interaction.message.channelId);
  197.         await interaction.message.edit({
  198.             embeds: [battleEmbed]
  199.         });
  200.        
  201.     },
  202.  
  203.     async handleCancel(interaction, battleRec, trainerRec) {
  204.         const client = interaction.client;
  205.  
  206.         // Only the host can cancel the raid
  207.         if (battleRec.hostTrainerId != trainerRec.id) {
  208.             await interaction.reply({
  209.                 content: `Only the host can cancel this raid`,
  210.                 flags: MessageFlags.Ephemeral
  211.             });
  212.             return;
  213.         }
  214.        
  215.         // Delete the battle members
  216.         const battleMemberRecs = await BattleMember.get({ battleId: battleRec.id });
  217.         for (const battleMemberRec of battleMemberRecs ) {
  218.             await battleMemberRec.delete();
  219.         }
  220.  
  221.         // Delete the battle record
  222.         await battleRec.delete();
  223.  
  224.         // Delete the message (noting we have to make sure the channel is in the cache first)
  225.         //client.logger.debug(`Channel ID = ${interaction.message.channelId}`);
  226.         //await client.channels.fetch(interaction.message.channelId);
  227.         //client.logger.debug(`Deleting message`);
  228.         await interaction.message.delete();
  229.  
  230.         await interaction.reply({
  231.             content: `Raid cancelled`
  232.         });
  233.     }
  234. };
  235.  
  236. export default BattleActionButtons;
  237.    
Advertisement
Add Comment
Please, Sign In to add comment