#Decorators Message Context?

1 messages · Page 1 of 1 (latest)

shy fog
#

The decorators don't really say, can I use them on top of an interaction? If not, what's the recommended flow to use the decorators e.g. RequiresGuildContext() on things like a command rather than a subcommand?

mortal shoalBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

shy fog
#

also is there any advantage to using shapeshift over zod?

#

e.g.

export class ConfigCommand extends Command {
    guildSupportChannels: Map<string, Channel> = new Map();
    guildSettingsEmbeds: Map<string, EmbedBuilder> = new Map();
    guildPermittedRoles: Map<string, string[]> = new Map();
    guildSupportEnabled: Map<string, boolean> = new Map();
    guildCustomUrls: Map<string, string[]> = new Map();

    public override registerApplicationCommands(registry: Command.Registry) {
        registry.registerChatInputCommand((builder) =>
            builder //
                .setName(this.name)
                .setDescription(this.description)
        );
    }

    public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
        const curChannel = interaction.channel;
        if (curChannel?.type === ChannelType.GuildText && interaction.guild !== null) {
            await this.updateSettingsEmbed(interaction);
        } else {
            await interaction.reply({ content: 'This command can only be used in a text channel in a guild', ephemeral: true });
        }
    }

    @RequiresGuildContext()
    @RequiresUserPermissions(['Administrator', 'ManageGuild'])
    private async updateSettingsEmbed(interaction: Command.ChatInputCommandInteraction) {
hearty stump
#

RequiresGuildContext requires the decorated method's first parameter to be a Message. Note to self add support for Interaction class. Anyway that's your answer.

shy fog
#

copy that, okay, so

hearty stump
#

And subcommands can have preconditions including runIn to limit it to a guild

shy fog
#

if I make the first parameter the message of the interaction, does that count or no?

#

yeah I figured out the subcommands part, but I saw the requirement for message -- just wondering if I can finagle it

#

cause the types for user.permissions is weird

#

string | ReadOnly<PermissionsBitField>

hearty stump
shy fog
#

no I meant, if I changed my

#

updateSettingsEmbed

#

to private async updateSettingsEmbed(message: Message, interaction: Command.ChatInputCommandInteraction) {

hearty stump
#

Oh yeah you could but I would recommend making a PR to support interactions instead

shy fog
#

copy that, will do now

hearty stump
#

Much better and helps everyone and saves me work too kekw

#

I just forgot to add it ages ago

#

And kept forgetting

shy fog
#

lol you know I love the package too much and will do it oh no

#

can you point me to the right dir

#

can find it, just lazy

#

meh one sec

hearty stump
shy fog
#

found it hehe

#

ty

#

probably 20 minutes or so

hearty stump
#

Tip: yarn install in the mono repo then yarn build because decorators depends on packages within the workspace

hearty stump
shy fog
#

I know I'm all about dat efficiency

hearty stump
#

Out for dinner anyway so can't really release anyway

shy fog
#

one quick question, you probably know this better than me

hearty stump
shy fog
#

anything other than Chat.ChatInputCommandInteraction?

#

that I should extend it to cover

hearty stump
#

BaseInteraction from djs catches all

shy fog
#

oh lit

#

thank you

hearty stump
#

The others extend that class

shy fog
#

you prefer yarn over bun?

hearty stump
hearty stump
shy fog
#

yeah true, I mean it does drop-in-replace NodeJS and it's webserver is like

#

900 times faster

#

like if you're using Node WebSockets

#

you should switch

#

cause their websockets are like 98 times faster or some stupid number and native

hearty stump
shy fog
#

which part? workflow or docker?

hearty stump
#

Workflow

#

That project is fully automated

#

Releases are done from GH workflow

#

As well as docker image updates and updating the docker container on my vps

shy fog
#

the automatic data update?

#

cause that's still running yarn in the errored command I see

hearty stump
#

Continuous deployment

#

Also yes I just found that out ffs

#

I made some typo somewhere in the script

#

Cry

shy fog
#

hahahaha

#

all good man the amount of times my bugs are the dumbest thing

#

it looks like your script is stopping for some reason on Generate JavaScript Companion library

#

is there some error throwing? It looks like it's just skipping

hearty stump
#

It's been broken for a while because bulbapedia (the website I scrape) added cloudflare captcha for a while and even flaresolverr couldn't flawlessly get by it. Now they disabled the captcha again so the script finally got to the last few lines but now there's a bug in writing the output

shy fog
#

hehehe I have a Browserless up that gets past all of that

hearty stump
shy fog
#

Lol they just released Pingora, their rust based API framework, maybe with that knowledge you can make it even better

#

I just use a Playwright browser

hearty stump
#

Idk. Maybe if I run into the issue again but I hope they'll keep captcha off now

#

It was because they noticed an increased network activity over November December relates to DDOS

#

Who would want to DDOS bulbapedia

#

Despots is2g

shy fog
# hearty stump Despots is2g

this look good?

export const RequiresClientPermissions = (...permissionsResolvable: PermissionResolvable[]): MethodDecorator => {
    const resolved = new PermissionsBitField(permissionsResolvable);
    const resolvedIncludesServerPermissions = Boolean(resolved.bitfield & DMAvailablePermissions.bitfield);

    return createFunctionPrecondition((context: Message | BaseInteraction) => {
        if (context instanceof Message) {
            if (resolvedIncludesServerPermissions && isDMChannel(context.channel)) {
                throw new UserError({
                    identifier: DecoratorIdentifiers.RequiresClientPermissionsGuildOnly,
                    message: 'Sorry, but that command can only be used in a server because I do not have sufficient permissions in DMs'
                });
            }

            if (isGuildBasedChannel(context.channel)) {
                const missingPermissions = context.channel.permissionsFor(context.guild!.members.me!).missing(resolved);

                if (missingPermissions.length) {
                    throw new UserError({
                        identifier: DecoratorIdentifiers.RequiresClientPermissionsMissingPermissions,
                        message: `Sorry, but I am not allowed to do that. I am missing the permissions: ${missingPermissions}`,
                        context: {
                            missing: missingPermissions
                        }
                    });
                }
            }
        } else {
            if (resolvedIncludesServerPermissions && isDMChannel(context.channel)) {
                throw new UserError({
                    identifier: DecoratorIdentifiers.RequiresClientPermissionsGuildOnly,
                    message: 'Sorry, but that command can only be used in a server because I do not have sufficient permissions in DMs'
                });
            }

            if (context.channel && isGuildBasedChannel(context.channel)) {
                const missingPermissions = context.channel.permissionsFor(context.guild!.members.me!).missing(resolved);

                if (missingPermissions.length) {
                    throw new UserError({
                        identifier: DecoratorIdentifiers.RequiresClientPermissionsMissingPermissions,
                        message: `Sorry, but I am not allowed to do that. I am missing the permissions: ${missingPermissions}`,
                        context: {
                            missing: missingPermissions
                        }
                    });
                }
            }
        }

        return true;
    });
};
hearty stump
#

Can you dump it on https://hastebin.skyra.pw instead? I'm on mobile so that's a bit hard to read (no syntax highlighting, wrapping)

shy fog
#

just swapped the message for context and checked the class instnace

#

yeh

#

that's just client perms just as an example of what i had in mind

hearty stump
#

Deduplicate it, no instanceof check like that.

shy fog
#

okaydokey

#

fixed

#

not gonna use that check lol, but

#

that's the format

#

I'll run tests and pull