Just for some background I'm creating a discord bot using discord.js. I'm currently following this video guide (linked at the timestamp where I'm having an issue) and I've copied exactly what has been done up until that point.
Here are the versions of everything I'm using:
I'll put my code and errors below but also here's a SrcShare: SrcShare Link
index.ts
import DiscordJS, { Intents } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()
const client = new DiscordJS.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
],
})
client.on('ready', () => {
console.log('The bot is ready.')
const guildId = '276237909378465794'
const guild = client.guilds.cache.get(guildId)
let commands
if (guild) {
commands = guild.commands
} else {
commands = client.application?.commands
}
commands?.create({
name: 'ping',
description: 'Replies with pong.',
})
})
client.login(process.env.TOKEN)
Errors I get when running ts-node index.ts in the terminal
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
index.ts(22,39): error TS1109: Expression expected.
index.ts(23,5): error TS1005: ':' expected.
index.ts(25,14): error TS1109: Expression expected.
index.ts(29,1): error TS1005: ':' expected.
[nodemon] app crashed - waiting for file changes before starting...
I've tried the solutions from this other stackoverflow post where the problem also seems to be optional chaining with the ? operator. All of the solutions in that thread say you need to update to TypeScript 3.7+ where optional chaining is supported but I'm using TypeScript 4.6.2.
If anyone knows what's going on here I'd appreciate the help!
UPDATE
So I think this might be a problem with nodemon. If I run ts-node index.ts in terminal the bot starts up without errors. But in the package.json I have a script that looks like this "dev": "nodemon index.ts". When I run npm run dev I still get the errors shown above even though nodemon should just be running ts-node index.ts.
Check the node_modules folder to see if you are actually using the version of TypeScript you think you're using. My issue was that a package I was depending on was depending on TypeScript 3.4.3 and installing that version in my project's node_modules folder.
Updating the package to one that didn't specify the TypeScript version it relies on fixed it for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With