code cleanup (command proccessing)

This commit is contained in:
mmacierzanka committed 2021-08-08 14:31:51 +02:00
1 parent 6144f3e577
commit d94f2ec92e
1 file changed
+98 -118
+98 -118
View File
@@ -95,25 +95,42 @@ export class Command {
interaction?: Message | CommandInteraction, interaction?: Message | CommandInteraction,
cmdParams?: InputParameter[] cmdParams?: InputParameter[]
): Promise<void> { ): Promise<void> {
if (interaction instanceof Message) { const paramFindFn = function (
const memberPermissions: Readonly<Permissions> = query: string,
interaction?.member?.permissions || new Permissions(); returnType?: "value" | "object"
if ( ) {
!this.permissions || return returnType === "object"
(this.permissionCheck == "ALL" ? cmdParams?.find((p) => p.name === query) || null
? memberPermissions.has(this.permissions, true) : cmdParams?.find((p) => p.name === query)?.value || null;
: memberPermissions.any(this.permissions, true)) };
) { const memberPermissions: Readonly<Permissions> =
const fnResult = await this.function.call( (interaction?.member?.permissions as Permissions) ||
this, new Permissions();
function (query: string, returnType?: "value" | "object") { if (
return returnType === "object" !this.permissions ||
? cmdParams?.find((p) => p.name === query) || null (this.permissionCheck == "ALL"
: cmdParams?.find((p) => p.name === query)?.value || ? memberPermissions.has(this.permissions, true)
null; : memberPermissions.any(this.permissions, true))
}, ) {
interaction if (interaction instanceof CommandInteraction) {
); setTimeout(async () => {
if (!interaction.replied) {
await interaction.reply({
embeds: [
new MessageEmbed()
.setColor("#ffff00")
.setTitle("🔄 Please wait..."),
],
});
}
}, 2000);
}
const fnResult = await this.function.call(
this,
paramFindFn,
interaction
);
if (interaction instanceof Message) {
if ( if (
fnResult instanceof Object && fnResult instanceof Object &&
("content" in (fnResult as any) || ("content" in (fnResult as any) ||
@@ -128,108 +145,71 @@ export class Command {
} else if (fnResult instanceof MessageEmbed) { } else if (fnResult instanceof MessageEmbed) {
await interaction?.channel.send({ embeds: [fnResult] }); await interaction?.channel.send({ embeds: [fnResult] });
} }
} else { } else if (interaction instanceof CommandInteraction) {
throw new PermissionsError(this, interaction?.member); try {
} if (
} else if (interaction instanceof CommandInteraction) { fnResult instanceof Object &&
const memberPermissions = interaction.member?.permissions; ("content" in (fnResult as any) ||
if (memberPermissions instanceof Permissions) { "embeds" in (fnResult as any) ||
if ( "files" in (fnResult as any) ||
!this.permissions || "components" in (fnResult as any) ||
(this.permissionCheck === "ALL" "sticker" in (fnResult as any))
? memberPermissions.has(this.permissions, true) ) {
: memberPermissions.any(this.permissions, true)) interaction.replied
) { ? await interaction.editReply(
setTimeout(async () => { fnResult as ReplyMessageOptions
if (!interaction.replied) { )
await interaction.reply({ : await interaction.reply(
embeds: [ fnResult as ReplyMessageOptions
new MessageEmbed() );
.setColor("#ffff00") } else if (typeof fnResult == "string") {
.setTitle("🔄 Please wait..."), interaction.replied
], ? await interaction.editReply({
}); content: fnResult,
} embeds: [],
}, 2000); })
const fnResult = await this.function.call( : await interaction.reply({
this, content: fnResult,
function ( embeds: [],
query: string, });
returnType?: "value" | "object" } else if (fnResult instanceof MessageEmbed) {
) { interaction.replied
return returnType === "object" ? await interaction.editReply({
? cmdParams?.find((p) => p.name === query) || embeds: [fnResult],
null })
: cmdParams?.find((p) => p.name === query) : await interaction.reply({
?.value || null; embeds: [fnResult],
}, });
interaction } else {
); interaction.replied
try { ? await interaction.editReply({
if ( embeds: [
fnResult instanceof Object && new MessageEmbed()
("content" in (fnResult as any) || .setColor("#00ff00")
"embeds" in (fnResult as any) || .setTitle(
"files" in (fnResult as any) || "✅ Task completed successfully"
"components" in (fnResult as any) || ),
"sticker" in (fnResult as any)) ],
) { })
interaction.replied : await interaction.reply({
? await interaction.editReply( embeds: [
fnResult as ReplyMessageOptions new MessageEmbed()
) .setColor("#00ff00")
: await interaction.reply( .setTitle(
fnResult as ReplyMessageOptions "✅ Task completed successfully"
); ),
} else if (typeof fnResult == "string") { ],
interaction.replied });
? await interaction.editReply({
content: fnResult,
embeds: [],
})
: await interaction.reply({
content: fnResult,
embeds: [],
});
} else if (fnResult instanceof MessageEmbed) {
interaction.replied
? await interaction.editReply({
embeds: [fnResult],
})
: await interaction.reply({
embeds: [fnResult],
});
} else {
interaction.replied
? await interaction.editReply({
embeds: [
new MessageEmbed()
.setColor("#00ff00")
.setTitle(
"✅ Task completed successfully"
),
],
})
: await interaction.reply({
embeds: [
new MessageEmbed()
.setColor("#00ff00")
.setTitle(
"✅ Task completed successfully"
),
],
});
}
} catch (e) {
console.error("[❌ ERROR] Cannot reply.", e);
} }
} catch (e) {
console.error("[❌ ERROR] Cannot reply.", e);
} }
} else {
throw new PermissionsError(
this,
interaction.member as GuildMember
);
} }
} else {
throw new PermissionsError(
this,
interaction?.member as GuildMember
);
} }
} }