f(x) = 1/x
f(x) = 1/x
DIAdiscord.js - Imagine an app
Created by f(x) = 1/x on 4/1/2024 in #djs-questions
command handler
my code in 01registercommands.js
const { testServer } = require('../../../config.json');
const areCommandsDifferent = require('../../utils/areCommandsDifferent');
const getApplicationCommands = require('../../utils/getApplicationCommands');
const getLocalCommands = require('../../utils/getLocalCommands');

module.exports = async (client) => {

try {
const localCommands = getLocalCommands();
const applicationCommands = await getApplicationCommands(client, testServer);

for (const localCommand of localCommands) {
const { name, description, options } = localCommand;

const existingCommand = await applicationCommands.cache.find(
(cmd) => cmd.name === name
);

if (existingCommand) {
if (localCommand.delted) {
await applicationCommands.delete(existingCommand.id);
continue;
}

if (areCommandsDifferent(existingCommand, localCommand)) {
await applicationCommands.edit(existingCommand.id, {
description,
options,
});
console.log(`Edited command "${name}".`)
}
} else {
if (localCommand.delted) {
console.log(`Skipping registering command "${name}." as it's set to delete.`);
continue;
}

await applicationCommands.create({
name,
description,
options,
})

console.log(`Registered command "${name}."`)
}
}
} catch (error) {
console.log(`There was an error: "${error}"`)
}
};
const { testServer } = require('../../../config.json');
const areCommandsDifferent = require('../../utils/areCommandsDifferent');
const getApplicationCommands = require('../../utils/getApplicationCommands');
const getLocalCommands = require('../../utils/getLocalCommands');

module.exports = async (client) => {

try {
const localCommands = getLocalCommands();
const applicationCommands = await getApplicationCommands(client, testServer);

for (const localCommand of localCommands) {
const { name, description, options } = localCommand;

const existingCommand = await applicationCommands.cache.find(
(cmd) => cmd.name === name
);

if (existingCommand) {
if (localCommand.delted) {
await applicationCommands.delete(existingCommand.id);
continue;
}

if (areCommandsDifferent(existingCommand, localCommand)) {
await applicationCommands.edit(existingCommand.id, {
description,
options,
});
console.log(`Edited command "${name}".`)
}
} else {
if (localCommand.delted) {
console.log(`Skipping registering command "${name}." as it's set to delete.`);
continue;
}

await applicationCommands.create({
name,
description,
options,
})

console.log(`Registered command "${name}."`)
}
}
} catch (error) {
console.log(`There was an error: "${error}"`)
}
};
6 replies
DIAdiscord.js - Imagine an app
Created by f(x) = 1/x on 4/1/2024 in #djs-questions
bot status
let status = [
{
name: 'death',
type:ActivityType.Playing,
},
{
name: 'life',
type:ActivityType.Playing,
},
]

client.on('ready', (c) => {
console.log('bot is online.');

setInterval(() => {
let random = Math.floor(Math.random * status.length);
client.user.setActivity(status[random]);
}, 15000);
});
let status = [
{
name: 'death',
type:ActivityType.Playing,
},
{
name: 'life',
type:ActivityType.Playing,
},
]

client.on('ready', (c) => {
console.log('bot is online.');

setInterval(() => {
let random = Math.floor(Math.random * status.length);
client.user.setActivity(status[random]);
}, 15000);
});
but the bot won't show status
6 replies
DIAdiscord.js - Imagine an app
Created by f(x) = 1/x on 4/1/2024 in #djs-questions
null value from slash command option
index.js
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;

if (interaction.commandName === 'embed') {
const embed = new EmbedBuilder().setTitle("This is title").setDescription("This is description");

interaction.reply({ embeds: [embed] });
}
})
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;

if (interaction.commandName === 'embed') {
const embed = new EmbedBuilder().setTitle("This is title").setDescription("This is description");

interaction.reply({ embeds: [embed] });
}
})
resgister-commands.js
{
name: 'embed',
description: 'sends an embed',
},
{
name: 'embed',
description: 'sends an embed',
},
error
node:events:496
throw er; // Unhandled 'error' event
^

TypeError: Cannot read properties of null (reading 'value')
at Client.<anonymous> (C:\Users\SWEET HOME\Desktop\BTEP\src\index.js:91:57)
at Client.emit (node:events:530:35)
at InteractionCreateAction.handle (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketManager.emit (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:282:31)
at WebSocketShard.<anonymous> (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\@discordjs\ws\dist\index.js:1173:51)
at WebSocketShard.emit (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:282:31)
at WebSocketShard.onMessage (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\@discordjs\ws\dist\index.js:988:14)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:401:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)

Node.js v20.11.1
node:events:496
throw er; // Unhandled 'error' event
^

TypeError: Cannot read properties of null (reading 'value')
at Client.<anonymous> (C:\Users\SWEET HOME\Desktop\BTEP\src\index.js:91:57)
at Client.emit (node:events:530:35)
at InteractionCreateAction.handle (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketManager.emit (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:282:31)
at WebSocketShard.<anonymous> (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\@discordjs\ws\dist\index.js:1173:51)
at WebSocketShard.emit (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:282:31)
at WebSocketShard.onMessage (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\@discordjs\ws\dist\index.js:988:14)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:401:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)

Node.js v20.11.1
55 replies
DIAdiscord.js - Imagine an app
Created by f(x) = 1/x on 4/1/2024 in #djs-voice
addition
part of index.js
client.on('interactionCreate', (interaction) => {

if (interaction.commandName === 'add') {
const num1 = interaction.options.get('first-number').value;
const num2 = interaction.options.get('second-number').value;
const num3 = interaction.options.get('third-number')?.value;
const num4 = interaction.options.get('fourth-number')?.value;
const num5 = interaction.options.get('fifth-number')?.value;
const num6 = interaction.options.get('sixth-number')?.value;
const num7 = interaction.options.get('seventh-number')?.value;
const num8 = interaction.options.get('eight-number')?.value;
const num9 = interaction.options.get('nineth-number')?.value;

interaction.reply(`The sum is ${num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9}`);
}
}
client.on('interactionCreate', (interaction) => {

if (interaction.commandName === 'add') {
const num1 = interaction.options.get('first-number').value;
const num2 = interaction.options.get('second-number').value;
const num3 = interaction.options.get('third-number')?.value;
const num4 = interaction.options.get('fourth-number')?.value;
const num5 = interaction.options.get('fifth-number')?.value;
const num6 = interaction.options.get('sixth-number')?.value;
const num7 = interaction.options.get('seventh-number')?.value;
const num8 = interaction.options.get('eight-number')?.value;
const num9 = interaction.options.get('nineth-number')?.value;

interaction.reply(`The sum is ${num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9}`);
}
}
26 replies
DIAdiscord.js - Imagine an app
Created by f(x) = 1/x on 3/31/2024 in #djs-questions
addition and subtraction
part of index.js
client.on('interactionCreate', (interaction) => {

if (interaction.commandName === 'add') {
const num1 = interaction.options.get('first-number');
const num2 = interaction.options.get('second-number');

interaction.reply(`The sum is ${num1 + num2}`);
}

if (interaction.commandName === 'subtract') {
const num1 = interaction.options.get('first-number');
const num2 = interaction.options.get('second-number');

interaction.reply(`The difference is ${num1 - num2}`);
}
})
client.on('interactionCreate', (interaction) => {

if (interaction.commandName === 'add') {
const num1 = interaction.options.get('first-number');
const num2 = interaction.options.get('second-number');

interaction.reply(`The sum is ${num1 + num2}`);
}

if (interaction.commandName === 'subtract') {
const num1 = interaction.options.get('first-number');
const num2 = interaction.options.get('second-number');

interaction.reply(`The difference is ${num1 - num2}`);
}
})
part of register-commands.js
const commands = [
{
name: 'add',
description: 'adds two numbers',
options: [
{
name: 'first-number',
description: 'the first number to be added',
type: ApplicationCommandOptionType.Number,
required: true,
},
{
name: 'second-number',
description: 'the second number to be added',
type: ApplicationCommandOptionType.Number,
required: true,
}
]
},
{
name: 'subtract',
description: 'subtracts two numbers',
options: [
{
name: 'first-number',
description: 'the first number to be subtracted',
type: ApplicationCommandOptionType.Number,
required: true,
},
{
name: 'second-number',
description: 'the second number to be subtracted',
type: ApplicationCommandOptionType.Number,
required: true,
}
]
},
];
const commands = [
{
name: 'add',
description: 'adds two numbers',
options: [
{
name: 'first-number',
description: 'the first number to be added',
type: ApplicationCommandOptionType.Number,
required: true,
},
{
name: 'second-number',
description: 'the second number to be added',
type: ApplicationCommandOptionType.Number,
required: true,
}
]
},
{
name: 'subtract',
description: 'subtracts two numbers',
options: [
{
name: 'first-number',
description: 'the first number to be subtracted',
type: ApplicationCommandOptionType.Number,
required: true,
},
{
name: 'second-number',
description: 'the second number to be subtracted',
type: ApplicationCommandOptionType.Number,
required: true,
}
]
},
];
results for addition: The sum is [object Object][object Object] results for subtraction: The difference is NaN
5 replies
DIAdiscord.js - Imagine an app
Created by f(x) = 1/x on 3/31/2024 in #djs-questions
help with simple ping command ;-;
User this is my code const { Client, IntentsBitField } = require ('discord.js');
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});

client.on('ready', (c) => {
console.log('bot is online.'); //you can use ${user.tag.id(or .tag or smth)} to mention the bot like console.log(`${user.tag.id} is online.`);
});

client.login('nuggets');
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});

client.on('ready', (c) => {
console.log('bot is online.'); //you can use ${user.tag.id(or .tag or smth)} to mention the bot like console.log(`${user.tag.id} is online.`);
});

client.login('nuggets');
how do i add command in it to reply to a message ?tag ping as pong
10 replies
DIAdiscord.js - Imagine an app
Created by f(x) = 1/x on 3/31/2024 in #djs-questions
nodemon: error
my code:
const { Client, IntentsBitField } = require ('discord.js');

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.GuildContent,
],
});

Client.login(
'no hehe'
);
const { Client, IntentsBitField } = require ('discord.js');

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.GuildContent,
],
});

Client.login(
'no hehe'
);
the error:
[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node src/index.js`
C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:174
throw new DiscordjsRangeError(ErrorCodes.BitFieldInvalid, bit);
^

RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined.
at IntentsBitField.resolve (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:174:11)
at C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:168:35
at Array.map (<anonymous>)
at IntentsBitField.resolve (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:168:18)
at new BitField (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:33:38)
at new IntentsBitField (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\IntentsBitField.js:9:1)
at Client._validateOptions (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\Client.js:514:25)
at new Client (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\Client.js:80:10)
at Object.<anonymous> (C:\Users\SWEET HOME\Desktop\BTEP\src\index.js:3:16)
at Module._compile (node:internal/modules/cjs/loader:1376:14) {
code: 'BitFieldInvalid'
}

Node.js v20.11.1
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node src/index.js`
C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:174
throw new DiscordjsRangeError(ErrorCodes.BitFieldInvalid, bit);
^

RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined.
at IntentsBitField.resolve (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:174:11)
at C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:168:35
at Array.map (<anonymous>)
at IntentsBitField.resolve (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:168:18)
at new BitField (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\BitField.js:33:38)
at new IntentsBitField (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\util\IntentsBitField.js:9:1)
at Client._validateOptions (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\Client.js:514:25)
at new Client (C:\Users\SWEET HOME\Desktop\BTEP\node_modules\discord.js\src\client\Client.js:80:10)
at Object.<anonymous> (C:\Users\SWEET HOME\Desktop\BTEP\src\index.js:3:16)
at Module._compile (node:internal/modules/cjs/loader:1376:14) {
code: 'BitFieldInvalid'
}

Node.js v20.11.1
[nodemon] app crashed - waiting for file changes before starting...
6 replies