addamsson
addamsson
Explore posts from servers
DIAdiscord.js - Imagine a boo! 👻
Created by addamsson on 5/7/2024 in #djs-questions
My bot doesn't get the full list of channels on a server
Hi there! I'm working on a bot that posts messages in a channel and I've just noticed that this functionality no longer works. I did some digging and it seems that not all channels are listed when using client.channels.cache. I checked all permissions, and viewed the server by using the bot's role and everything looks fine, my bot is supposed to see the relevant channels, but when I do this:
const channel = client.channels.cache.get(myChannelId);
const channel = client.channels.cache.get(myChannelId);
only a subset of all visible channels is returned. I double checked and the channel id I'm using (myChannelId) is good, but it is not in the list. Am I supposed to invalidate the cache somehow? What am I missing?
65 replies
DIAdiscord.js - Imagine a boo! 👻
Created by addamsson on 10/10/2023 in #djs-questions
How to send a message to a channel?
No description
12 replies
DIAdiscord.js - Imagine a boo! 👻
Created by addamsson on 9/18/2023 in #djs-questions
onInteractionCreate doesn't get called when I select a previously deselected parameter
I have a command that has 2 autocomplete parameters:
const data = new SlashCommandBuilder()
.setName(name)
.setDescription("Sets the current channel as a daily sync channel")
.addStringOption((option) =>
option
.setName(Options.org)
.setDescription("Filter for organization")
.setRequired(true)
.setAutocomplete(true)
)
.addStringOption((option) =>
option
.setName(Options.team)
.setDescription("Filter for team")
.setRequired(false)
.setAutocomplete(true)
);
const data = new SlashCommandBuilder()
.setName(name)
.setDescription("Sets the current channel as a daily sync channel")
.addStringOption((option) =>
option
.setName(Options.org)
.setDescription("Filter for organization")
.setRequired(true)
.setAutocomplete(true)
)
.addStringOption((option) =>
option
.setName(Options.team)
.setDescription("Filter for team")
.setRequired(false)
.setAutocomplete(true)
);
By default it pops up the org as an option which works fine, but if I press backspace and delete this suggestion I can select other parameters. In my case I selected team, and proceeded to select a team. After that when I select org from the parameters I get displayed the previous suggestions. My problem is that I have some code that filters organizations if a team is selected:
if (focusedOption.name === OrgAndTeamOptions.org) {
// 📘 if we have a team, then only the team's org is selectable
if (teamId) {
const teamOrgId = user.teams.find(
(team) => team.teamId === teamId
)?.team.orgId;
choices = user.organizations
.filter((org) => org.orgId === teamOrgId)
.map((org) => ({
name: org.organization.name,
value: org.orgId,
}));
} else {
choices = user.organizations.map((org) => ({
name: org.organization.name,
value: org.orgId,
}));
}
}
if (focusedOption.name === OrgAndTeamOptions.org) {
// 📘 if we have a team, then only the team's org is selectable
if (teamId) {
const teamOrgId = user.teams.find(
(team) => team.teamId === teamId
)?.team.orgId;
choices = user.organizations
.filter((org) => org.orgId === teamOrgId)
.map((org) => ({
name: org.organization.name,
value: org.orgId,
}));
} else {
choices = user.organizations.map((org) => ({
name: org.organization.name,
value: org.orgId,
}));
}
}
This works if the user starts to type something, but the whole InteractionCreate callback doesn't get called when I simply select org, the old list is displayed instead. How can I force the reloading of the organizations that the user can pick? I'd like to use autocomplete as it is possible that there are many organizations / teams to choose from.
8 replies
DIAdiscord.js - Imagine a boo! 👻
Created by addamsson on 9/12/2023 in #djs-questions
How to make sure that a channels.create call followed by a channels.fetch call is consistent?
Right now I have some code that creates a category and then adds some channels to it. What I've noticed is that there is some sort of race condition. Whenever I create a new category with
await guild.channels.create({
type: ChannelType.GuildCategory,
name: groupToCreate,
});
await guild.channels.create({
type: ChannelType.GuildCategory,
name: groupToCreate,
});
then there is a seemingly random chance that a subsequent
await guild.channels.fetch();
await guild.channels.fetch();
call will not contain the newly created category. How can i make sure that fetch is in a happens-before relationship with create? Note that I also tried:
await guild.channels.fetch(undefined, {
force: true,
});
await guild.channels.fetch(undefined, {
force: true,
});
to no avail. 😦
5 replies
DIAdiscord.js - Imagine a boo! 👻
Created by addamsson on 9/8/2023 in #djs-questions
How to create a new channel for a guild?
I have a bot that will create a text channel to write into when invited. My question is how can I do this with discord.js? More specifically. I have this code that I have written by just exploring the API:
const guild = await client.guilds.cache.get("<some-id>");
guild?.channels.create({
name: "test",
type: ChannelType.GuildText,
topic: "test",
});
const guild = await client.guilds.cache.get("<some-id>");
guild?.channels.create({
name: "test",
type: ChannelType.GuildText,
topic: "test",
});
I'm guessing that this will work, but I'm not sure I understand what cache is about. I've seen this throughout the API, but I'm not sure how it works. Do I have to load the cache, or does it happen lazily whenever I try to interact with something?
78 replies