I have made a Discord bot using javascript (I’m very new to js) and once I add it to two different discord servers, even though it has the same permissions, it doesn’t work the same way.
The bot should post a message every monday at 9am to a specific channel named “design-challenges”. In one server it does exactly that, in the other server it doesn’t. The bot is online in both servers and I can see it runs basic commands like !hello fine in both.
Here’s my code, if that helps:
const { Client, GatewayIntentBits } = require('discord.js');
const fs = require('fs').promises;
const cron = require('node-cron');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const prefix = '!';
const challengeChannelName = 'design-challenges';
let challenges;
// Store the current challenge index
let currentChallengeIndex = 1;
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}`);
challenges = await loadChallenges();
scheduleWeeklyChallenge();
});
client.on('messageCreate', (message) => {
// Ignore messages from bots
if (message.author.bot) return;
// Check if the message starts with the command prefix
if (message.content.startsWith(prefix)) {
const [command, ...args] = message.content.slice(prefix.length).split(' ');
if (command === 'ping') {
message.channel.send('Pong!');
} else if (command === 'hello') {
message.channel.send('Hello!');
}
if(command === 'challenge') {
const challenge = getNextChallenge();
message.channel.send(challenge);
}
}
});
function scheduleWeeklyChallenge() {
// Set a cron job to send a challenge every Monday at 9:00 AM
cron.schedule('0 8 * * 1', () => {
const channel = client.channels.cache.find(ch => ch.name === challengeChannelName);
if (channel) {
const introText = `
@everyone
# Er du klar til at udfordre dine designfærdigheder?
Vi inviterer dig til at deltage i vores seneste udfordring og give dine medstuderende feedback.nHusk at upload din challenge lige herunder, ikke som reply.
Denne uges challenge er:`;
const challenge = getNextChallenge();
// Send introductory message
channel.send(introText)
.then(() => {
// Wait for a moment (you can adjust the timeout duration) before sending the challenge
return new Promise(resolve => setTimeout(resolve, 2000));
})
.then(() => {
// Send the challenge
channel.send(challenge);
})
.catch(error => console.error('Error sending challenge:', error));
}
});
}
async function loadChallenges() {
try {
const data = await fs.readFile('challenges.json');
return JSON.parse(data);
} catch (error) {
console.error('Error loading challenges:', error);
return [];
}
}
function getNextChallenge() {
const currentIndex = currentChallengeIndex;
const nextChallenge = challenges[currentIndex];
// Increment the challenge index for the next time
currentChallengeIndex = (currentIndex + 1) % challenges.length;
return `## ${currentIndex + 1}: ${nextChallenge.title}
* **What**: ${nextChallenge.what}
* **Target**: ${nextChallenge.target}
`;
}
client.on('messageCreate', async (message) => {
if (message.channel.name === 'design-challenges' && message.attachments.size > 0) {
const thread = await message.channel.threads.create({
name: `feedback-${message.author.displayName}`,
});
await thread.send(`${message.author} har indgivet et bidrag. Giv jeres konstruktive feedback herunder.`);
await thread.send(`Oprindelig fil: ${message.attachments.first().url}`);
}
});
client.login(process.env.TOKEN);
I don’t understand why it works in one server but it doesn’t work in the other. Please help

