const results = await client.shard.broadcastEval(
async (client, { channelId, reminderText, userId }) => {
try {
// Try to get the channel from cache first
let channel = client.channels.cache.get(channelId);
// If not in cache, try to fetch
if (!channel) {
try {
channel = await client.channels.fetch(channelId, { force: true }).catch(() => null);
} catch (error) {
return { success: false, error: error.message, shardId: client.shard.ids[0] };
}
}
// If channel not found or not accessible by this shard, report failure
if (!channel) {
return { success: false, reason: 'Channel not found', shardId: client.shard.ids[0] };
}
// Check permissions - check for DM channels without using ChannelType enum
if (
channel.type === 'DM' ||
channel.isDMBased?.() ||
channel.permissionsFor?.(client.user)?.has(PermissionsBitField.Flags.SendMessages)
) {
// Fetch user to mention them properly in the channel
const user = await client.users.fetch(userId).catch(() => null);
if (!user) {
return { success: false, reason: 'User not found', shardId: client.shard.ids[0] };
}
// Send the reminder
await channel.send(`${user}, here is your reminder: "${reminderText}"`);
return { success: true, shardId: client.shard.ids[0] };
} else {
return {
success: false,
reason: 'Missing permissions',
shardId: client.shard.ids[0],
};
}
} catch (error) {
return { success: false, error: error.message, shardId: client.shard.ids[0] };
}
},
{
context: {
channelId: reminder.channel,
reminderText: reminder.reminder,
userId: user.id,
},
},
);
const results = await client.shard.broadcastEval(
async (client, { channelId, reminderText, userId }) => {
try {
// Try to get the channel from cache first
let channel = client.channels.cache.get(channelId);
// If not in cache, try to fetch
if (!channel) {
try {
channel = await client.channels.fetch(channelId, { force: true }).catch(() => null);
} catch (error) {
return { success: false, error: error.message, shardId: client.shard.ids[0] };
}
}
// If channel not found or not accessible by this shard, report failure
if (!channel) {
return { success: false, reason: 'Channel not found', shardId: client.shard.ids[0] };
}
// Check permissions - check for DM channels without using ChannelType enum
if (
channel.type === 'DM' ||
channel.isDMBased?.() ||
channel.permissionsFor?.(client.user)?.has(PermissionsBitField.Flags.SendMessages)
) {
// Fetch user to mention them properly in the channel
const user = await client.users.fetch(userId).catch(() => null);
if (!user) {
return { success: false, reason: 'User not found', shardId: client.shard.ids[0] };
}
// Send the reminder
await channel.send(`${user}, here is your reminder: "${reminderText}"`);
return { success: true, shardId: client.shard.ids[0] };
} else {
return {
success: false,
reason: 'Missing permissions',
shardId: client.shard.ids[0],
};
}
} catch (error) {
return { success: false, error: error.message, shardId: client.shard.ids[0] };
}
},
{
context: {
channelId: reminder.channel,
reminderText: reminder.reminder,
userId: user.id,
},
},
);