I've been facing an obstacle while trying to make my Discord bot work in TypeScript. Every time I attempt msg.reply(string), I get the error message "Cannot invoke an object which is possibly 'null'." despite having just checked if it is null. Below is my current code:
import Discord = require("discord.js");
const client: Discord.Client = new Discord.Client();
client.on("ready", () => {
console.log(`connected as ${client.user?.tag}`);
});
client.on("message", msg => {
if (msg == null){
return;
}
msg.reply("hi");
});
Although disabling strict null checking can solve this issue, I prefer to keep it enabled.
I would greatly appreciate any assistance, thank you!
edit:
This is the error that the VS Code compiler displays:
app.ts:14:5 - error TS2721: Cannot invoke an object which is possibly 'null'.
14 msg.reply("hi");
~~~~~~~~~
Found 1 error.
The terminal process terminated with exit code: 2