Creating a decorator
I have this decorator here:
I use it right here:
Also adding the option:
The function redefined in the decorator doesn't seem to run at all.
export function TemplateAutocomplete(): ClassDecorator {
return createClassDecorator((target: Command) =>
createProxy(target, {
apply(target) {
target.autocompleteRun = async (
interaction: AutocompleteInteraction,
) => {
const template = interaction.options.getString("template");
if (!template) return;
const data = (
await db.template.findMany({ select: { id: true } })
).map((x) => x.id);
const fuse = new Fuse(data);
console.log(
(template ? fuse.search(template).map((x) => x.item) : data)
.slice(0, 25)
.map((x) => ({
name: x,
value: x,
})),
);
return interaction
.respond(
(template ? fuse.search(template).map((x) => x.item) : data)
.slice(0, 25)
.map((x) => ({
name: x,
value: x,
})),
)
.catch(() => {});
};
},
}),
);
}
export function TemplateAutocomplete(): ClassDecorator {
return createClassDecorator((target: Command) =>
createProxy(target, {
apply(target) {
target.autocompleteRun = async (
interaction: AutocompleteInteraction,
) => {
const template = interaction.options.getString("template");
if (!template) return;
const data = (
await db.template.findMany({ select: { id: true } })
).map((x) => x.id);
const fuse = new Fuse(data);
console.log(
(template ? fuse.search(template).map((x) => x.item) : data)
.slice(0, 25)
.map((x) => ({
name: x,
value: x,
})),
);
return interaction
.respond(
(template ? fuse.search(template).map((x) => x.item) : data)
.slice(0, 25)
.map((x) => ({
name: x,
value: x,
})),
)
.catch(() => {});
};
},
}),
);
}
@ApplyOptions<Command.Options>({
description: "Create and generate a template",
})
@TemplateAutocomplete()
export class TemplateCommand extends Command {
@ApplyOptions<Command.Options>({
description: "Create and generate a template",
})
@TemplateAutocomplete()
export class TemplateCommand extends Command {
.addStringOption((string) =>
string
.setName("template")
.setDescription(
"ID of the template to save as (defaults to share ID)",
)
.setRequired(true)
.setAutocomplete(true),
),
.addStringOption((string) =>
string
.setName("template")
.setDescription(
"ID of the template to save as (defaults to share ID)",
)
.setRequired(true)
.setAutocomplete(true),
),
1 Reply
I just tried this:
Works fine. Is there any better way to do this though?
export function TemplateAutocomplete(): ClassDecorator {
return createClassDecorator((target) =>
createProxy(target, {
construct: (ctor, args) => {
const instance = new ctor(...args);
instance.autocompleteRun = async (
interaction: AutocompleteInteraction,
) => {
console.log("e");
const template = interaction.options.getString("template");
if (!template) return;
const data = (
await db.template.findMany({ select: { id: true } })
).map((x) => x.id);
const fuse = new Fuse(data);
const results = (
template ? fuse.search(template).map((x) => x.item) : data
)
.slice(0, 25)
.map((x) => ({ name: x, value: x }));
console.log(results);
return interaction.respond(results).catch(() => {});
};
return instance;
},
}),
);
}
export function TemplateAutocomplete(): ClassDecorator {
return createClassDecorator((target) =>
createProxy(target, {
construct: (ctor, args) => {
const instance = new ctor(...args);
instance.autocompleteRun = async (
interaction: AutocompleteInteraction,
) => {
console.log("e");
const template = interaction.options.getString("template");
if (!template) return;
const data = (
await db.template.findMany({ select: { id: true } })
).map((x) => x.id);
const fuse = new Fuse(data);
const results = (
template ? fuse.search(template).map((x) => x.item) : data
)
.slice(0, 25)
.map((x) => ({ name: x, value: x }));
console.log(results);
return interaction.respond(results).catch(() => {});
};
return instance;
},
}),
);
}