Fallen
Fallen
Explore posts from servers
DIdiscord.js - Imagine ❄
Created by Fallen on 7/17/2023 in #djs-questions
How to fetch a guild from the cache on another shard
Hello, I am making a command that needs the functionality of fetching a guild from the cache, however it is a sharded bot. Is there a method I can call to check other shards client caches for this guild?
3 replies
DIdiscord.js - Imagine ❄
Created by Fallen on 6/14/2023 in #djs-questions
Error fetching guild on client ready event
I have a function that will check for all guilds that have member count enabled However this specific group of code seems to always fail to fetch the guild, even tho when evaluating the same code it succeeds. This specific line const guild = await client.guilds.fetch(g.guild).catch(() => {}); always returns void or undefined. Even tho the provided guild ID is valid. This function is called from the ClientReady event.
import Client from '@/app';
import Guild from '@/models/Guild';
import { ChannelType } from 'discord.js';
import { schedule } from 'node-cron';

export default async (client: Client) => {
schedule('*/5 * * * *', async () => {
const guilds = await Guild.find({ 'memberCount.enabled': true });
for (const g of guilds) {
if (!g.memberCount.channel) return;
const guild = await client.guilds.fetch(g.guild).catch(() => {});
if (!guild) return;
const channel = await client.channels.fetch(g.memberCount.channel).catch(() => {});
if (!channel) return;
const members = await guild.members.fetch();
const filtered = members.filter((m) => !m.user.bot);
const ch = await channel.fetch();
if (ch.type !== ChannelType.GuildVoice) return;
await ch.edit({ name: g.memberCount.name ? g.memberCount.name.replace('{members}', filtered.size.toString()) : `Members: ${filtered.size}`, reason: 'Automated Action: Member Count Updated' });
}
});
};
import Client from '@/app';
import Guild from '@/models/Guild';
import { ChannelType } from 'discord.js';
import { schedule } from 'node-cron';

export default async (client: Client) => {
schedule('*/5 * * * *', async () => {
const guilds = await Guild.find({ 'memberCount.enabled': true });
for (const g of guilds) {
if (!g.memberCount.channel) return;
const guild = await client.guilds.fetch(g.guild).catch(() => {});
if (!guild) return;
const channel = await client.channels.fetch(g.memberCount.channel).catch(() => {});
if (!channel) return;
const members = await guild.members.fetch();
const filtered = members.filter((m) => !m.user.bot);
const ch = await channel.fetch();
if (ch.type !== ChannelType.GuildVoice) return;
await ch.edit({ name: g.memberCount.name ? g.memberCount.name.replace('{members}', filtered.size.toString()) : `Members: ${filtered.size}`, reason: 'Automated Action: Member Count Updated' });
}
});
};
20 replies
DIdiscord.js - Imagine ❄
Created by Fallen on 5/25/2023 in #djs-voice
Audio Player state changes to playing when resource ends
I am creating an audio resource from a ytdl download, and when I play this resource on the player with player.play(resource)the players state is set to "buffering", once the resource stops playing in the voice channel the players state changes to "playing". I am listening for when the player goes idle to queue the next song, however it never goes idle. My code is below:
const audio = ytdl(song, { filter: 'audioonly' });
const connection = joinVoiceChannel({
guildId: interaction.guild?.id!,
channelId: member.voice.channel.id,
adapterCreator: interaction.guild?.voiceAdapterCreator!
});
const player = createAudioPlayer();
connection.subscribe(player);
const resource = createAudioResource(audio);
const newQueue = new Queue(meta, player, resource);
client.queues.set(interaction.guild?.id!, newQueue);
const audio = ytdl(song, { filter: 'audioonly' });
const connection = joinVoiceChannel({
guildId: interaction.guild?.id!,
channelId: member.voice.channel.id,
adapterCreator: interaction.guild?.voiceAdapterCreator!
});
const player = createAudioPlayer();
connection.subscribe(player);
const resource = createAudioResource(audio);
const newQueue = new Queue(meta, player, resource);
client.queues.set(interaction.guild?.id!, newQueue);
Queue:
this.info = info;
this.player = player;
this.resource = resource;

this.player.play(resource);

this.player.on('stateChange', (state) => {
console.log(state.status);
if (state.status !== AudioPlayerStatus.Idle) return;
if (!this.songs[0]) return;
const song = ytdl(this.songs[0].videoDetails.video_url, { filter: 'audioonly' });
const newResource = createAudioResource(song);
this.resource = newResource;
this.player.play(newResource);
this.songs.shift();
});
this.info = info;
this.player = player;
this.resource = resource;

this.player.play(resource);

this.player.on('stateChange', (state) => {
console.log(state.status);
if (state.status !== AudioPlayerStatus.Idle) return;
if (!this.songs[0]) return;
const song = ytdl(this.songs[0].videoDetails.video_url, { filter: 'audioonly' });
const newResource = createAudioResource(song);
this.resource = newResource;
this.player.play(newResource);
this.songs.shift();
});
5 replies