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:
- VS Code: 1.65.2
- Node: 16.14.2
- npm: 8.5.0
- TypeScript: 4.6.2
- dotenv: 16.0.0
- discord.js: 13.6.0
- nodemon: 2.0.15
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
.