Reputation:
I'm trying to make a /ping command for my discord. The bot is using type script and a plugin called WOKCommands.
I'm getting this error below (This only happens when i use the slash command instead of the prefix command {>ping} ).
Cannot read properties of undefined (reading 'channel')
this happens because of line const msg = await message.channel.send ({content: `> 🏓 Pinging..`})
in the code below
import { Client, Message, MessageEmbed, MessageActionRow, MessageButton, MessageSelectMenu } from'discord.js';
const db = require('quick.db')
const moment = require('moment')
import { ICommand } from "wokcommands";
export default {
name: 'ping',
description: "Fetches the client latency",
category: "Utilities",
slash: 'both',
testonly: true,
/**
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
callback: async({client, message, args}) => {
const msg = await message.channel.send ({content: `> 🏓 Pinging..`})
msg.edit({content: `> 🏓 Pong! Latency: **${client.ws.ping}ms**`})
}
} as ICommand
Upvotes: 0
Views: 216
Reputation: 4520
This only happens when I use the slash command instead of the prefix command
>ping
This is the cause of your error. When you use a slash command, there is no message involved. Slash commands are not sent as messages, they are sent as interactions. When you use your ping command as a slash command, message
is undefined.
Additionally, when using slash commands you must send a reply instead of simply sending a message in the channel. Otherwise, you will get an additional error. I would recommend switching to using the .reply()
method for both prefix commands and slash commands here (though I haven't made said switch for your prefix commands in the below code). This is one way to fix your callback:
({ client, message, interaction, args }) => {
if (message) {
const msg = await message.channel.send ({content: `> 🏓 Pinging..`})
msg.edit({content: `> 🏓 Pong! Latency: **${client.ws.ping}ms**`})
}
else if (interaction) {
await interaction.reply({content: `> 🏓 Pinging..`});
interaction.editReply({content: `> 🏓 Pong! Latency: **${client.ws.ping}ms**`});
}
}
You may find the WOKCommands documentation useful for issues such as this one. Their docs even have an example of a ping command, albeit a simpler version.
Upvotes: 1