Create use effect command #419
4 changed files with 94 additions and 28 deletions
|
@ -1,6 +1,7 @@
|
|||
import {ButtonInteraction} from "discord.js";
|
||||
import {ButtonInteraction,EmbedBuilder} from "discord.js";
|
||||
Vylpes marked this conversation as resolved
Outdated
|
||||
import {ButtonEvent} from "../type/buttonEvent";
|
||||
import EffectHelper from "../helpers/EffectHelper";
|
||||
import { EffectDetails } from "../constants/EffectDetails";
|
||||
|
||||
export default class Effects extends ButtonEvent {
|
||||
public override async execute(interaction: ButtonInteraction) {
|
||||
|
@ -10,6 +11,9 @@ export default class Effects extends ButtonEvent {
|
|||
case "list":
|
||||
await this.List(interaction);
|
||||
break;
|
||||
case "use":
|
||||
await this.Use(interaction);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,4 +34,53 @@ export default class Effects extends ButtonEvent {
|
|||
components: [ result.row ],
|
||||
});
|
||||
}
|
||||
|
||||
private async Use(interaction: ButtonInteraction) {
|
||||
const subaction = interaction.customId.split(" ")[2];
|
||||
|
||||
Vylpes marked this conversation as resolved
Outdated
VylpesTester
commented
I don't like that its public I don't like that its public
|
||||
switch (subaction) {
|
||||
case "confirm":
|
||||
await this.UseConfirm(interaction);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async UseConfirm(interaction: ButtonInteraction) {
|
||||
const id = interaction.customId.split(" ")[3];
|
||||
|
||||
const effectDetail = EffectDetails.get(id);
|
||||
|
||||
if (!effectDetail) {
|
||||
Vylpes marked this conversation as resolved
Outdated
VylpesTester
commented
I don't like that thats public I don't like that thats public
|
||||
await interaction.reply("Unable to find effect!");
|
||||
return;
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const whenExpires = new Date(now.getMilliseconds() + effectDetail.duration);
|
||||
Vylpes marked this conversation as resolved
Outdated
Copilot
commented
The error message 'Unable to find effect!' is unclear. It should be 'Effect not found in the system!'. The error message 'Unable to find effect!' is unclear. It should be 'Effect not found in the system!'.
|
||||
|
||||
const result = await EffectHelper.UseEffect(interaction.user.id, id, whenExpires);
|
||||
|
||||
if (result) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("Effect Used")
|
||||
.setDescription("You now have an active effect!")
|
||||
.addFields([
|
||||
{
|
||||
name: "Effect",
|
||||
value: effectDetail.friendlyName,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: "Expires",
|
||||
value: `<t:${whenExpires.getMilliseconds()}:f>`,
|
||||
inline: true,
|
||||
},
|
||||
]);
|
||||
|
||||
await interaction.update({ embeds: [ embed ] });
|
||||
return;
|
||||
}
|
||||
|
||||
await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import {CommandInteraction, EmbedBuilder, SlashCommandBuilder} from "discord.js"
|
|||
import {Command} from "../type/command";
|
||||
import EffectHelper from "../helpers/EffectHelper";
|
||||
import {EffectDetails} from "../constants/EffectDetails";
|
||||
import UserEffect from "../database/entities/app/UserEffect";
|
||||
|
||||
export default class Effects extends Command {
|
||||
constructor() {
|
||||
|
@ -67,32 +68,29 @@ export default class Effects extends Command {
|
|||
return;
|
||||
Vylpes marked this conversation as resolved
VylpesTester
commented
I think we should log a warning in this case, since it shouldn't be possible I think we should log a warning in this case, since it shouldn't be possible
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const whenExpires = new Date(now.getMilliseconds() + effectDetail.duration);
|
||||
const canUseEffect = await EffectHelper.CanUseEffect(interaction.user.id, id)
|
||||
|
||||
const result = await EffectHelper.UseEffect(interaction.user.id, id, whenExpires);
|
||||
|
||||
if (result) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("Effect Used")
|
||||
.setDescription("You now have an active effect!")
|
||||
.addFields([
|
||||
{
|
||||
name: "Effect",
|
||||
value: effectDetail.friendlyName,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: "Expires",
|
||||
value: `<t:${whenExpires.getMilliseconds()}:f>`,
|
||||
inline: true,
|
||||
},
|
||||
]);
|
||||
|
||||
await interaction.reply({ embeds: [ embed ] });
|
||||
if (!canUseEffect) {
|
||||
await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown");
|
||||
return;
|
||||
}
|
||||
|
||||
await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown");
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("Effect Confirmation")
|
||||
.setDescription("Would you like to use this effect?")
|
||||
.addFields([
|
||||
{
|
||||
name: "Effect",
|
||||
value: effectDetail.friendlyName,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: "Length",
|
||||
value: "",
|
||||
inline: true,
|
||||
},
|
||||
]);
|
||||
|
||||
await interaction.reply({ embeds: [ embed ] });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,22 @@ export default class EffectHelper {
|
|||
}
|
||||
|
||||
public static async UseEffect(userId: string, name: string, whenExpires: Date): Promise<boolean> {
|
||||
const canUseEffect = await this.CanUseEffect(userId, name);
|
||||
|
||||
if (!canUseEffect) return false;
|
||||
|
||||
const effect = await UserEffect.FetchOneByUserIdAndName(userId, name);
|
||||
|
||||
if (!effect) return false;
|
||||
|
||||
effect.UseEffect(whenExpires);
|
||||
|
||||
await effect.Save(UserEffect, effect);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static async CanUseEffect(userId: string, name: string): Promise<boolean> {
|
||||
const effect = await UserEffect.FetchOneByUserIdAndName(userId, name);
|
||||
const now = new Date();
|
||||
|
||||
|
@ -34,10 +50,6 @@ export default class EffectHelper {
|
|||
return false;
|
||||
}
|
||||
|
||||
effect.UseEffect(whenExpires);
|
||||
|
||||
await effect.Save(UserEffect, effect);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -118,4 +118,7 @@ export default class TimeLengthInput {
|
|||
|
||||
return desNumber;
|
||||
}
|
||||
|
||||
public static ConvertFromMilliseconds(ms: number): TimeLengthInput {
|
||||
Vylpes marked this conversation as resolved
VylpesTester
commented
I want tests generated for this I want tests generated for this
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue
Fix spacing