U
U
DIAdiscord.js - Imagine an app
Created by U on 1/31/2024 in #djs-questions
Autocompletion in Slash commands
Auto ocmpletion code:
const D = require("discord.js")
const fs = require("node:fs/promises")
module.exports = {
data: new D.SlashCommandBuilder()
.setName("querydata")
.setDescription("query the db for result")
.addStringOption(option =>
option.setName("q")
.setDescription("query data in memory")
.setAutocomplete(true)),

async autocomplete(Interaction) {
try {
console.log("\n Autocompletion started...")
const focusedvalue = Interaction.options.getFocused()
let files = []
const readdir = await fs.readdir("./Memory")
readdir.forEach(element => {
files.push(element)
console.log(`\n Pushing file: ${element}`)
});

const filteresdata = files.filter(filess => filess.startsWith(focusedvalue))
await Interaction.respond(
filteresdata.map(filess => ({ name: filess, value: filess }))
)
} catch (e) {
console.log(`\n Autocomplete function failed with error ${e}`)
return
}
},

async execute(Interaction) {
console.log(`\n Autocomplete finished`)
}
}
const D = require("discord.js")
const fs = require("node:fs/promises")
module.exports = {
data: new D.SlashCommandBuilder()
.setName("querydata")
.setDescription("query the db for result")
.addStringOption(option =>
option.setName("q")
.setDescription("query data in memory")
.setAutocomplete(true)),

async autocomplete(Interaction) {
try {
console.log("\n Autocompletion started...")
const focusedvalue = Interaction.options.getFocused()
let files = []
const readdir = await fs.readdir("./Memory")
readdir.forEach(element => {
files.push(element)
console.log(`\n Pushing file: ${element}`)
});

const filteresdata = files.filter(filess => filess.startsWith(focusedvalue))
await Interaction.respond(
filteresdata.map(filess => ({ name: filess, value: filess }))
)
} catch (e) {
console.log(`\n Autocomplete function failed with error ${e}`)
return
}
},

async execute(Interaction) {
console.log(`\n Autocomplete finished`)
}
}
Here is the code in Interacton event:
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);

try {
if (interaction.isAutocomplete()) {
await command.autocomplete(interaction)
return
}
await command.execute(interaction);
} catch (e) {
console.log(`\n InteractionCreate event failed with code: ${e}`)
return
}
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);

try {
if (interaction.isAutocomplete()) {
await command.autocomplete(interaction)
return
}
await command.execute(interaction);
} catch (e) {
console.log(`\n InteractionCreate event failed with code: ${e}`)
return
}
});
Any tips on what todo? dosnt seem like the code executes at all depsite autocomplete beeing active?
8 replies
DIAdiscord.js - Imagine an app
Created by U on 1/28/2024 in #djs-questions
Error with choices in slash commands
Error:
ExpectedConstraintError: Invalid string format
at Object.run (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:2350:64)
at C:\Users\4339u\Desktop\UNORP2.2\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:939:67
at Array.reduce (<anonymous>)
at _StringValidator.parse (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:939:29)
at validateName (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\builders\dist\index.js:1578:17)
at SlashCommandUserOption.setName (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\builders\dist\index.js:1678:5)
at C:\Users\4339u\Desktop\UNORP2.2\Commands\Duno-send.cjs:8:11
at MixedClass._sharedAddOptionMethod (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\builders\dist\index.js:2280:50)
at MixedClass.addUserOption (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\builders\dist\index.js:2212:17)
at Object.<anonymous> (C:\Users\4339u\Desktop\UNORP2.2\Commands\Duno-send.cjs:7:10) {
constraint: 's.string.regex',
given: 'UserID',
expected: 'expected /^[\\p{Ll}\\p{Lm}\\p{Lo}\\p{N}\\p{sc=Devanagari}\\p{sc=Thai}_-]+$/u.test(expected) to be true'
}

Node.js v20.11.0
ExpectedConstraintError: Invalid string format
at Object.run (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:2350:64)
at C:\Users\4339u\Desktop\UNORP2.2\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:939:67
at Array.reduce (<anonymous>)
at _StringValidator.parse (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:939:29)
at validateName (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\builders\dist\index.js:1578:17)
at SlashCommandUserOption.setName (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\builders\dist\index.js:1678:5)
at C:\Users\4339u\Desktop\UNORP2.2\Commands\Duno-send.cjs:8:11
at MixedClass._sharedAddOptionMethod (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\builders\dist\index.js:2280:50)
at MixedClass.addUserOption (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\builders\dist\index.js:2212:17)
at Object.<anonymous> (C:\Users\4339u\Desktop\UNORP2.2\Commands\Duno-send.cjs:7:10) {
constraint: 's.string.regex',
given: 'UserID',
expected: 'expected /^[\\p{Ll}\\p{Lm}\\p{Lo}\\p{N}\\p{sc=Devanagari}\\p{sc=Thai}_-]+$/u.test(expected) to be true'
}

Node.js v20.11.0
Code:
data: new D.SlashCommandBuilder()
.setName("uno-send")
.setDescription("Takes your message and sends it to the member")
.addUserOption(option =>
option.setName('UserID').setDescription('The member to send message to').setRequired(true))
.addUserOption(option =>
option.setName('Preset').setDescription('The preset to use, write 0 for no preset').setRequired(true))
.addUserOption(option =>
option.setName('msg').setDescription('Comments to the message').setRequired(true)),
data: new D.SlashCommandBuilder()
.setName("uno-send")
.setDescription("Takes your message and sends it to the member")
.addUserOption(option =>
option.setName('UserID').setDescription('The member to send message to').setRequired(true))
.addUserOption(option =>
option.setName('Preset').setDescription('The preset to use, write 0 for no preset').setRequired(true))
.addUserOption(option =>
option.setName('msg').setDescription('Comments to the message').setRequired(true)),
Any tips?
7 replies
DIAdiscord.js - Imagine an app
Created by U on 1/28/2024 in #djs-questions
Help registering Slash commands
I need some help with Slash commands
DiscordAPIError[0]: 405: Method Not Allowed
at handleErrors (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\rest\dist\index.js:722:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\rest\dist\index.js:1120:23)
at async SequentialHandler.queueRequest (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\rest\dist\index.js:953:14)
at async _REST.request (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\rest\dist\index.js:1266:22)
at async RCMH (C:\Users\4339u\Desktop\UNORP2.2\Commands\CommandHandler.cjs:41:22)
at async C:\Users\4339u\Desktop\UNORP2.2\Commands\CommandHandler.cjs:23:13 {
requestBody: { files: undefined, json: [ [Object] ] },
rawError: { message: '405: Method Not Allowed', code: 0 },
code: 0,
status: 405,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/983360693963218945/commands/undefined'
}
DiscordAPIError[0]: 405: Method Not Allowed
at handleErrors (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\rest\dist\index.js:722:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\rest\dist\index.js:1120:23)
at async SequentialHandler.queueRequest (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\rest\dist\index.js:953:14)
at async _REST.request (C:\Users\4339u\Desktop\UNORP2.2\node_modules\@discordjs\rest\dist\index.js:1266:22)
at async RCMH (C:\Users\4339u\Desktop\UNORP2.2\Commands\CommandHandler.cjs:41:22)
at async C:\Users\4339u\Desktop\UNORP2.2\Commands\CommandHandler.cjs:23:13 {
requestBody: { files: undefined, json: [ [Object] ] },
rawError: { message: '405: Method Not Allowed', code: 0 },
code: 0,
status: 405,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/983360693963218945/commands/undefined'
}
Code:
async function RCMH(RCMH_Commands, token) {
try {
const rest = new D.REST().setToken(token);
console.log(`Started refreshing ${RCMH_Commands.length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
console.log(`Commands: ${RCMH_Commands.name} \n\n`)
// process.exit(0)
const data = await rest.put(
D.Routes.applicationCommand("983360693963218945"),
{ body: RCMH_Commands },
);
console.log(`Registering Commands ${RCMH_Commands}`)

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
}
async function RCMH(RCMH_Commands, token) {
try {
const rest = new D.REST().setToken(token);
console.log(`Started refreshing ${RCMH_Commands.length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
console.log(`Commands: ${RCMH_Commands.name} \n\n`)
// process.exit(0)
const data = await rest.put(
D.Routes.applicationCommand("983360693963218945"),
{ body: RCMH_Commands },
);
console.log(`Registering Commands ${RCMH_Commands}`)

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
}
Can i get some help with the error message? its when i send the request body to the REST it fails
4 replies
DIAdiscord.js - Imagine an app
Created by U on 1/25/2023 in #djs-questions
Trying to send buttons with embed
DiscordAPIError[50035]: Invalid Form Body components[0][UNION_TYPE_CHOICES]: Value of field "type" must be one of (1,). components[1][UNION_TYPE_CHOICES]: Value of field "type" must be one of (1,).
const embed = new D.EmbedBuilder()
.setTitle(`Note from: ${CTX.author.username}`)
.setDescription(CTX.content)
.setTimestamp()
.setColor(D.Colors.DarkGold)

const ThumbsUp = new D.ButtonBuilder()
.setLabel("1")
.setStyle(D.ButtonStyle.Success)
.setCustomId(`test123`)
.setEmoji("👍")


const ThumbsDown = new D.ButtonBuilder()
.setLabel("1")
.setCustomId(`test1231`)
.setStyle(D.ButtonStyle.Danger)
.setEmoji("👎")


await CTX.channel.send({ephemeral: true, embeds: [embed], components: [ThumbsUp, ThumbsDown]})
const embed = new D.EmbedBuilder()
.setTitle(`Note from: ${CTX.author.username}`)
.setDescription(CTX.content)
.setTimestamp()
.setColor(D.Colors.DarkGold)

const ThumbsUp = new D.ButtonBuilder()
.setLabel("1")
.setStyle(D.ButtonStyle.Success)
.setCustomId(`test123`)
.setEmoji("👍")


const ThumbsDown = new D.ButtonBuilder()
.setLabel("1")
.setCustomId(`test1231`)
.setStyle(D.ButtonStyle.Danger)
.setEmoji("👎")


await CTX.channel.send({ephemeral: true, embeds: [embed], components: [ThumbsUp, ThumbsDown]})
4 replies
DIAdiscord.js - Imagine an app
Created by U on 1/12/2023 in #djs-questions
Receive DM from user to Bot
This is how i check for DM:
// Receive DM from users
if(msg.channel.type === d.ChannelType.DM) {
console.log("We received a DM")
}
// Receive DM from users
if(msg.channel.type === d.ChannelType.DM) {
console.log("We received a DM")
}
This is my intents:
const client = new d.Client({intents: [
d.GatewayIntentBits.Guilds,
d.GatewayIntentBits.DirectMessages,
d.GatewayIntentBits.GuildMessages,
d.GatewayIntentBits.MessageContent,
], partials: ['CHANNEL', 'MESSAGE', 'REACTION']})
const client = new d.Client({intents: [
d.GatewayIntentBits.Guilds,
d.GatewayIntentBits.DirectMessages,
d.GatewayIntentBits.GuildMessages,
d.GatewayIntentBits.MessageContent,
], partials: ['CHANNEL', 'MESSAGE', 'REACTION']})
Yet it dosn't respond to it? the if statement is inside "MessageCreate" i tried to search for answers here but all i found is peolpe missing intents witch i am not? (using latest version)
5 replies