harshfeudal
DIAdiscord.js - Imagine an app
•Created by harshfeudal on 7/8/2023 in #djs-questions
Cannot use `{data.length}` (Typescript)
i dont know the reason why
{data.length}
get error, it announced that 'data' is of type 'unknown'.ts(18046)
Here is the code:
import fs from 'node:fs'
import path from 'node:path'
import { REST, Routes } from 'discord.js'
import { client } from '../src/index'
import * as dotenv from 'dotenv'
dotenv.config();
const rest = new REST().setToken(process.env.TOKEN!);
const commands = [];
const foldersPath = path.join(__dirname, '../commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders)
{
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles)
{
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command)
commands.push(command.data.toJSON());
}
}
(async () =>
{
try
{
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(client.user!.id),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
// ^^^^
}
catch (error) { console.error(error); }
}
)();
import fs from 'node:fs'
import path from 'node:path'
import { REST, Routes } from 'discord.js'
import { client } from '../src/index'
import * as dotenv from 'dotenv'
dotenv.config();
const rest = new REST().setToken(process.env.TOKEN!);
const commands = [];
const foldersPath = path.join(__dirname, '../commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders)
{
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles)
{
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command)
commands.push(command.data.toJSON());
}
}
(async () =>
{
try
{
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(client.user!.id),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
// ^^^^
}
catch (error) { console.error(error); }
}
)();
8 replies
DIAdiscord.js - Imagine an app
•Created by harshfeudal on 8/9/2022 in #djs-questions
Cannot catch the correct offline members
So I tried to add this command as the same on Discord.js Guide:
https://discord.js.org/#/docs/discord.js/main/typedef/PresenceStatus
https://discordjs.guide/popular-topics/faq.html#how-do-i-find-all-online-members-of-a-guild
However, both
// First use guild.members.fetch to make sure all members are cached
guild.members.fetch({ withPresences: true }).then(fetchedMembers => {
const totalOffline = fetchedMembers.filter(member => member.presence?.status === 'offline');
// Now you have a collection with all online member objects in the totalOnline variable
console.log(`There are currently ${totalOffline.size} members online in this guild!`);
});
// First use guild.members.fetch to make sure all members are cached
guild.members.fetch({ withPresences: true }).then(fetchedMembers => {
const totalOffline = fetchedMembers.filter(member => member.presence?.status === 'offline');
// Now you have a collection with all online member objects in the totalOnline variable
console.log(`There are currently ${totalOffline.size} members online in this guild!`);
});
online
, idle
and dnd
are correct, except offline
status.
- I add my bot in the test server where I stored a lot of offline bots and my alt account, but the console still log 0
offline members.
All intents are carefully checked. May I ask why I cannot have a correct offline stats?4 replies
DIAdiscord.js - Imagine an app
•Created by harshfeudal on 7/15/2022 in #djs-questions
DiscordAPIError Interaction has already been acknowledged.
I suddenly got this error, and idk why (they're both fine in my last test)
Started refreshing application (/) commands.
Successfully reloaded application (/) commands.
Ready! Logged in as Shikimori-san~#6050
------------------------------------
Harshfeudal#0001(622450109317251088) in #hall triggered an interaction.
Id: 997533401114492948
Date: 15/7/2022 (timeflake: 1657900895545)
------------------------------------
DiscordAPIError: Interaction has already been acknowledged.
at RequestHandler.execute (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async CommandInteraction.reply (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:103:5)
at async Object.execute (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\commands\selfRole-button.js:38:13)
at async Client.<anonymous> (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\index.js:39:3) {
method: 'post',
path: '/interactions/997533401114492948/aW50ZXJhY3Rpb246OTk3NTMzNDAxMTE0NDkyOTQ4OlFlVzJSdzBtbmdiMWZyVUNqbWpSVkRBamNFaXhWbFRZd05MSzVJMGQ2UTB4cUtuOVg3QnRlQW5URFJIUkJ1QjluaXNrMVlBd1hYeXZ2TkUwdEo4OFRodzZ2Sm40RUh1MUxSTzBFY0t6d1FWVWFWWmVOTzhTOFZBMzVsOTJ1ckty/callback',
code: 40060,
httpStatus: 400,
requestData: { json: { type: 4, data: [Object] }, files: [] }
}
E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:350
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Interaction has already been acknowledged.
at RequestHandler.execute (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async CommandInteraction.reply (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:103:5)
at async Client.<anonymous> (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\index.js:42:3) {
method: 'post',
path: '/interactions/997533401114492948/aW50ZXJhY3Rpb246OTk3NTMzNDAxMTE0NDkyOTQ4OlFlVzJSdzBtbmdiMWZyVUNqbWpSVkRBamNFaXhWbFRZd05MSzVJMGQ2UTB4cUtuOVg3QnRlQW5URFJIUkJ1QjluaXNrMVlBd1hYeXZ2TkUwdEo4OFRodzZ2Sm40RUh1MUxSTzBFY0t6d1FWVWFWWmVOTzhTOFZBMzVsOTJ1ckty/callback',
code: 40060,
httpStatus: 400,
requestData: {
json: {
type: 4,
data: {
content: 'There was an error while executing this command!',
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: 64,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
}
},
files: []
}
}
Started refreshing application (/) commands.
Successfully reloaded application (/) commands.
Ready! Logged in as Shikimori-san~#6050
------------------------------------
Harshfeudal#0001(622450109317251088) in #hall triggered an interaction.
Id: 997533401114492948
Date: 15/7/2022 (timeflake: 1657900895545)
------------------------------------
DiscordAPIError: Interaction has already been acknowledged.
at RequestHandler.execute (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async CommandInteraction.reply (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:103:5)
at async Object.execute (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\commands\selfRole-button.js:38:13)
at async Client.<anonymous> (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\index.js:39:3) {
method: 'post',
path: '/interactions/997533401114492948/aW50ZXJhY3Rpb246OTk3NTMzNDAxMTE0NDkyOTQ4OlFlVzJSdzBtbmdiMWZyVUNqbWpSVkRBamNFaXhWbFRZd05MSzVJMGQ2UTB4cUtuOVg3QnRlQW5URFJIUkJ1QjluaXNrMVlBd1hYeXZ2TkUwdEo4OFRodzZ2Sm40RUh1MUxSTzBFY0t6d1FWVWFWWmVOTzhTOFZBMzVsOTJ1ckty/callback',
code: 40060,
httpStatus: 400,
requestData: { json: { type: 4, data: [Object] }, files: [] }
}
E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:350
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Interaction has already been acknowledged.
at RequestHandler.execute (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async CommandInteraction.reply (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:103:5)
at async Client.<anonymous> (E:\Trung Son\Code Project\Discord Bot Project\Server Bot\Shikimori-ButtonRole\index.js:42:3) {
method: 'post',
path: '/interactions/997533401114492948/aW50ZXJhY3Rpb246OTk3NTMzNDAxMTE0NDkyOTQ4OlFlVzJSdzBtbmdiMWZyVUNqbWpSVkRBamNFaXhWbFRZd05MSzVJMGQ2UTB4cUtuOVg3QnRlQW5URFJIUkJ1QjluaXNrMVlBd1hYeXZ2TkUwdEo4OFRodzZ2Sm40RUh1MUxSTzBFY0t6d1FWVWFWWmVOTzhTOFZBMzVsOTJ1ckty/callback',
code: 40060,
httpStatus: 400,
requestData: {
json: {
type: 4,
data: {
content: 'There was an error while executing this command!',
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: 64,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
}
},
files: []
}
}
5 replies
DIAdiscord.js - Imagine an app
•Created by harshfeudal on 7/15/2022 in #djs-questions
How do I make a Button role for add only one role only?
I have make like this
Another file:
So, if i want my member can only choose only Pink or, if they change their mind, and they press Azure color, so how do I make a button that it can add only 1 role, and check if they have another role so it will be removed and add a new role that they've selected?
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageActionRow, MessageButton } = require('discord.js');
// This file is only for creating buttons only!
module.exports = {
data: new SlashCommandBuilder()
.setName('selfrole')
.setDescription('Create button role (Only Harshfeudal can use)'),
async execute(interaction) {
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('Pink')
.setLabel(': Shikimori Pink')
.setEmoji('<:ShikimoriPink:996998214010024036>')
.setStyle('SECONDARY'),
)
.addComponents(
new MessageButton()
.setCustomId('Azure')
.setLabel(': Azure')
.setEmoji('<:Azure:997181460169633894>')
.setStyle('SECONDARY'),
)
if(interaction.user.id !== '622450109317251088') {
return interaction.reply({ content: "Harshfeudal only!", ephemeral: true });
}
await interaction.reply({
content: `Menu created!`,
ephemeral: true
});
await interaction.channel.send({
content: `Choose your role:`,
components: [row]
});
}
}
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageActionRow, MessageButton } = require('discord.js');
// This file is only for creating buttons only!
module.exports = {
data: new SlashCommandBuilder()
.setName('selfrole')
.setDescription('Create button role (Only Harshfeudal can use)'),
async execute(interaction) {
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('Pink')
.setLabel(': Shikimori Pink')
.setEmoji('<:ShikimoriPink:996998214010024036>')
.setStyle('SECONDARY'),
)
.addComponents(
new MessageButton()
.setCustomId('Azure')
.setLabel(': Azure')
.setEmoji('<:Azure:997181460169633894>')
.setStyle('SECONDARY'),
)
if(interaction.user.id !== '622450109317251088') {
return interaction.reply({ content: "Harshfeudal only!", ephemeral: true });
}
await interaction.reply({
content: `Menu created!`,
ephemeral: true
});
await interaction.channel.send({
content: `Choose your role:`,
components: [row]
});
}
}
// This file is only create for interacting with buttons only!
module.exports = {
name: 'interactionCreate',
async execute(interaction) {
if (interaction.isButton()) {
const ShikimoriPink = "996997651872628847";
const Azure = "997181348475322418";
const member = interaction.member;
if (interaction.customId === 'Pink') {
// Shikimori Pink
interaction.guild.roles.fetch(ShikimoriPink)
.then(role => member.roles.add(role))
.catch(console.error());
return interaction.reply({
content: `Your role will be added shortly!`,
ephemeral: true });
} else if (interaction.customId === 'Azure') {
// Azure
interaction.guild.roles.fetch(Azure)
.then(role => member.roles.add(role))
.catch(console.error());
return interaction.reply({
content: `Your role will be added shortly!`,
ephemeral: true
});
};
}
},
};
// This file is only create for interacting with buttons only!
module.exports = {
name: 'interactionCreate',
async execute(interaction) {
if (interaction.isButton()) {
const ShikimoriPink = "996997651872628847";
const Azure = "997181348475322418";
const member = interaction.member;
if (interaction.customId === 'Pink') {
// Shikimori Pink
interaction.guild.roles.fetch(ShikimoriPink)
.then(role => member.roles.add(role))
.catch(console.error());
return interaction.reply({
content: `Your role will be added shortly!`,
ephemeral: true });
} else if (interaction.customId === 'Azure') {
// Azure
interaction.guild.roles.fetch(Azure)
.then(role => member.roles.add(role))
.catch(console.error());
return interaction.reply({
content: `Your role will be added shortly!`,
ephemeral: true
});
};
}
},
};
2 replies