Swyftey
Swyftey
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by Swyftey on 11/17/2024 in #djs-questions
What is the best way to detect a change in server ownership when the bot happens to be offline?
Hi, I am using Discord.JS and am trying to figure out how I might change a server in my database's owner_id field when the owner id is found to be different. Here is an example:
// ClientReady.ts

try {
// Get database servers (my api)
const databaseServersResponse: Response = await ApiHandler.get(`http://api:${EnvHandler.get("API_PORT")}/servers`);
const databaseServers: Server[] = (await databaseServersResponse.json() as APIResponse).data?.servers;

// Get servers bot is in (discord.js)
const botServers: Record<string, any>[] = await ShardingHandler.getAllGuilds(botClient);
const botServerIds: string[] = botServers.map((server: Record<string, any>) => server.id);

// Get servers bot is not in
const notFoundServers: Server[] = databaseServers.filter((server: Server) => !botServerIds.includes(server.id));

for (const foundServer of botServers) {
await ApiHandler.patch(`http://api:${EnvHandler.get("API_PORT")}/users/${foundServer.ownerId}/servers/${foundServer.id}`, { has_bot: true });
}


// Here is where I am struggling. I don't think I can get the new owner id, because the bot is not in the guild.
for (const notFoundServer of notFoundServers) {
await ApiHandler.patch(`http://api:${EnvHandler.get("API_PORT")}/users/${notFoundServer.owner_id}/servers/${notFoundServer.id}`, { has_bot: false });
}

console.log("Successfully initizalied the bot and updated all necessary data.");
// ClientReady.ts

try {
// Get database servers (my api)
const databaseServersResponse: Response = await ApiHandler.get(`http://api:${EnvHandler.get("API_PORT")}/servers`);
const databaseServers: Server[] = (await databaseServersResponse.json() as APIResponse).data?.servers;

// Get servers bot is in (discord.js)
const botServers: Record<string, any>[] = await ShardingHandler.getAllGuilds(botClient);
const botServerIds: string[] = botServers.map((server: Record<string, any>) => server.id);

// Get servers bot is not in
const notFoundServers: Server[] = databaseServers.filter((server: Server) => !botServerIds.includes(server.id));

for (const foundServer of botServers) {
await ApiHandler.patch(`http://api:${EnvHandler.get("API_PORT")}/users/${foundServer.ownerId}/servers/${foundServer.id}`, { has_bot: true });
}


// Here is where I am struggling. I don't think I can get the new owner id, because the bot is not in the guild.
for (const notFoundServer of notFoundServers) {
await ApiHandler.patch(`http://api:${EnvHandler.get("API_PORT")}/users/${notFoundServer.owner_id}/servers/${notFoundServer.id}`, { has_bot: false });
}

console.log("Successfully initizalied the bot and updated all necessary data.");
If there isn't a way around this to changing the owner_id, what should I do? I'd rather a user who once owned a server (which is added to my database) not have access to it if they are no longer the owner.
12 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 11/2/2024 in #djs-questions
What is the best way to handle components when they've been timed out?
Hi, I'm sorry, I've probably asked this question a long time ago but have forgotten the answer as it has been a minute. What is the best way to go about timing buttons/components out? For example, a 60-second collector is started when a command is ran. When the 60 seconds are up, the buttons either disable or (what I'd prefer) have a different custom ID so that an error reply shows if the ID says the button is disabled. Also, what should be done (if anything) for when let's say the bot goes offline during the time a collector is running, how does the button disable/become timed out then? Thanks for any help.
5 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 10/30/2024 in #djs-questions
Embed Builder Not Defined When Using BroadcastEval
No description
53 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 10/24/2024 in #djs-questions
Discord slash command(s) deleting itself. (Sorry closed last post too early)
No description
53 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 10/24/2024 in #djs-questions
Why might a slash command delete itself after one use?
No description
19 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 10/20/2024 in #djs-questions
Sharding questions.
Hi. I am building a Discord bot which I can anticipate will eventually be in more than 2500 guilds pretty quick. I have a few questions about sharding which I am looking to have answered, any help is greatly appreciated: 1. Is it best to prepare and set up sharding for the bot now while I can? 2. Once the sharding manager is created, do I have to go back into my code (such as commands) and change it to work with all shards? Or does that depend on if the command should affect other guilds, for example? 3. What are the main use cases for using commands such as .broadcastEval() to communicate to all shards? Thanks!
33 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 7/8/2024 in #djs-questions
Can the REST class be used to work with global rate limits (make sure you don't throttle)?
No description
3 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 7/8/2024 in #djs-questions
How can I make sure slash commands are only added/registered once?
I was adding and removing my bot from my server once for testing purposes and recently ran into the "Interaction already acknowledged" error. I fixed it by once again re-adding my bot, but how can I prevent slash commands from being added if they already exist or just prevent this in general?
11 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 6/18/2024 in #djs-questions
Just double checking to be extra sure: Do ALL DiscordJS fetch methods check the cache first?
I'm trying to understand if I should still make my own cache module, or if I can (or if it is *okay * to) trust that the cache will be called first before attempting to use discord's API. For example, just calling guild.fetchWebhooks() upon an event that is fired, or checking my own cache module to see if the webhook is found before calling that.
3 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 6/12/2024 in #djs-questions
Does DiscordJS support activities/activity creation? If not, will it in the future?
Title displays question. Sorry if this is regularly asked, couldn't find.
10 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 6/3/2024 in #djs-questions
Role Select Menu not clearing/refreshing?
I swear it was working before. The input will not clear. Once the "next-button" is clicked, the message should update and the select menu should not display the previous inputs. The page number is correct and the data to set/display for the role select menu is correct as well.
case "next-button": {
if (currentConfig.index < info.pages.length-1) {
currentConfig.index += 1
currentConfig.page = info.pages[currentConfig.index]

await component.update({
embeds: [embeds.embed({
title: currentConfig.page.embed.title,
color: currentConfig.page.embed.color,
fields: currentConfig.page.embed.fields
})],
components: await setComponents(currentConfig)
});
}
break;
}
case "next-button": {
if (currentConfig.index < info.pages.length-1) {
currentConfig.index += 1
currentConfig.page = info.pages[currentConfig.index]

await component.update({
embeds: [embeds.embed({
title: currentConfig.page.embed.title,
color: currentConfig.page.embed.color,
fields: currentConfig.page.embed.fields
})],
components: await setComponents(currentConfig)
});
}
break;
}
6 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 5/30/2024 in #djs-questions
Is it possible to check if component timed out AFTER it has timed out?
For example, when a message is sent with a select-menu component:
try {
const select = await initialReply.awaitMessageComponent({ filter: i => i.user.id === interaction.user.id, time: 15_000 });
} catch (error) {
console.log(`[setup.js]`, error);
// Instead of making a chat message appear stating the command timed out automatically, send the message if the user continues to try using the component within the message (if it exists)
}
try {
const select = await initialReply.awaitMessageComponent({ filter: i => i.user.id === interaction.user.id, time: 15_000 });
} catch (error) {
console.log(`[setup.js]`, error);
// Instead of making a chat message appear stating the command timed out automatically, send the message if the user continues to try using the component within the message (if it exists)
}
18 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 5/29/2024 in #djs-questions
Client.Users.Fetch(UserId) erroring, stating UserId is null when it is not.
No description
2 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 5/28/2024 in #djs-questions
Webhook channel does not change after edit (webhook sends to wrong channel).
When I edit the webhook and change the channel, the webhook still sends to the prevous channel. Does the token or id update after an edit? If so, how can I get the token after the edit?
2 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 5/28/2024 in #djs-questions
Webhook channels.
If you edit a webhook to post to a different channel, are you also supposed to visibly see the webhook within the new channel's integrations tab? For example: Using this function to edit the webhook
await foundWebhook.edit({
name: interaction.client.user.username,
avatar: interaction.client.user.displayAvatarURL(),
channel: channel.id -- change the channel
})
await foundWebhook.edit({
name: interaction.client.user.username,
avatar: interaction.client.user.displayAvatarURL(),
channel: channel.id -- change the channel
})
2 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 5/21/2024 in #djs-questions
How can I catch specific errors, or what is the best way to handle error catching?
I'm sorry if this is a more common question, but I want to know how I can catch specific discord.js errors. For example, when
prompt.awaitMessageComponent({time: 15_000})
prompt.awaitMessageComponent({time: 15_000})
times out, I want a specific message to be sent, but what if it's another error instead?
6 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 5/20/2024 in #djs-questions
What are some ways to make your code more neat and organized?
For example, slash commands. Is there a way to neatly organize what I've got going on here? Especially the
.awaitMessageComponent
.awaitMessageComponent
, I have at least two of them here and it feels like there is a potential better way of using them for cleaner code.
4 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 4/30/2024 in #djs-questions
message.edit erroring with "Unknown Interaction", but message is found.
No description
4 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 4/28/2024 in #djs-questions
Is there a way to loop through each guild and channel without getting error using fetch?
No description
70 replies
DIAdiscord.js - Imagine an app
Created by Swyftey on 4/26/2024 in #djs-questions
What Discord.js features have the potential to hit the limit/go over the set Discord API limits?
Where can I even find Discord's API limits? I just want to make sure that what I am doing with my bot won't eventually be throttled if it grows.
14 replies