MysteeriMikitin
MysteeriMikitin

Reputation: 3

How to fix logging bot error [object Undefined]?

I have been trying to make a bot that logs everything in a specific channel. As I use this script, the bot posts "[object Undefined]" to the channel. If leave the toString out, the bot only posts the "User" part.

client.channels.cache.get('843442692993318913').send(toString('User', message.author.username, message.author.id, 'used command in a channel', message.channel.name))

Could someone help me, I am a beginner in these things.

Upvotes: 0

Views: 63

Answers (2)

Islam Ahmady
Islam Ahmady

Reputation: 81

You are passing the other values as another parameters to toString()

Only the first one will be converted , which is 'user'

if You need other data as string ,you can use interpolated string

you define it between ``

Then add your variables and text like this:

`The Id : {$message.author.id}  -  The Username : ${message.author.username} - ...`

Upvotes: 0

MrCodingB
MrCodingB

Reputation: 2314

Don't use the toString function for this, use an interpolated string. Also I would recommend not using (or not only using) the bot's cache to find a channel. It can cause unexpected problems, so either use resolve per default or as a fallback:

const channel = client.channels.cache.get('843442692993318913') ?? client.channels.resolve('843442692993318913');

channel.send(`User ${message.author.username} ${message.author.id} used command in a channel ${message.channel.name}`);

Upvotes: 1

Related Questions