Reputation: 116
This has just recently started happening, not sure if it's a bug with node or discord.js or something else, but I can't find anything that would be causing issues in my code.
The program having issues is a program for a discord bot I made. The error only happens during one command, which sends a message to the mentioned user and stores some info in a log for later. The relevant code:
if (!rating) {
let index = Math.floor(Math.random() * PARANOIAQUESTIONS["pg13"].length)
message.mentions.users.first().send("Question from " + message.author.username + ": \n" + PARANOIAQUESTIONS["pg13"][index] + "\nReply with '+ans [answer]'.").catch((err) => {message.channel.send("That user has their DMs set to closed."); console.log(err)})
paranoiaLog[message.mentions.users.first()] = message.channel
askedQuestions[message.mentions.users.first()] = PARANOIAQUESTIONS["pg13"][index]
} else {
let index = Math.floor(Math.random() * PARANOIAQUESTIONS["r"].length)
message.mentions.users.first().send("Question from " + message.author.username + ": \n" + PARANOIAQUESTIONS["r"][index] + "\nReply with '+ans [answer]'.").catch((err) => {message.channel.send("That user has their DMs set to closed."); console.log(err)})
paranoiaLog[message.mentions.users.first()] = message.channel
askedQuestions[message.mentions.users.first()] = PARANOIAQUESTIONS["r"][index]
}
This code segment (with slight differences) is repeated many times throughout the function, different depending on the specific permissions of the server and channel the command was sent in. The error can happen in any of these repetitions, so I don't think it's a strange typo (plus it only just started happening). The strange part is this: The error message points me to 49:94, while this code is at line 1100 or so. Lines 48-52:
client.on('channelCreate', (channel) => {
console.log("Channel created: " + channel.name + ", " + channel.id + " in " + channel.guild.name + " (" + channel.guild.systemChannelID + ")")
serverSettingsList[channel.guild.systemChannelID][channel.id] = {"muted?": 0, "truth pg": 1, "truth pg13": 1, "truth r": 0, "dare pg": 1, "dare pg13": 1, "dare r": 0, "dare d": 1, "dare irl": 1, "wyr pg": 1, "wyr pg13": 1, "wyr r": 0, "nhie pg": 1, "nhie pg13": 1, "nhie r": 0, "paranoia pg":1, "paranoia pg13": 1, "paranoia r": 0}
fs.writeFileSync('serversettingslist.json', JSON.stringify(serverSettingsList, null, '\t'))
})
Character 94 is the l in "guild" in channel.guild.name
. I don't know why it would be pointing me here when this code isn't even running at the time. I put console.log
s all over the function that is running to try and see exactly what line is causing an error, but all the correct things log, then the error throws. On top of that, it's only caught half of the time by the .catch
statement, so I really have no idea what it is.
EDIT: The issue turned out to be, as others noticed, that channel.guild is not defined for DM channels. I had assumed the channelCreate
code wouldn't be running when it messaged me, as I've had a DM open with the bot ever since I started making it, and forgot that channel
objects are not limited to servers and apply to DMs and group DMs as well. Thanks for helping me realize my mistake!
Upvotes: 0
Views: 151
Reputation: 707228
In this line of code:
console.log("Channel created: " + channel.name + ", " + channel.id + " in " + channel.guild.name + " (" + channel.guild.systemChannelID + ")")
you have multiple references to .name
. Based on the column number in the error, it appears that channel.guild
is undefined
so when you try to log channel.guild.name
, you get the error.
In the newest versions of Javascript, this is what the optional chaining operator ?.
can be useful for. In earlier versions, you just have to check to see if channel.guild
is valid before accessing channel.guild.name
.
Upvotes: 1