Reputation: 39
So I am trying to use some of my old code from discord.js v 11 in my v12 bot and I am having issues figuring out what is right and why It is not wanting to run properly
I think the issue is somewhere in these two bits, I just do not know where
async finish(bot) {
const now = new Date();
const message = await this.getPollMessage(bot);
if (!message) {
console.error("Cant find poll message");
return;
}
if (message.embeds.length < 1) {
console.error("The poll message ha no embeds.");
return;
}
this.hasFinished = true;
const embed = new Discord.MessageEmbed(message.embeds[0]);
embed.setColor("FF0800")
.setAuthor(`${this.question} [FINISHED]`)
.setFooter(`Poll ${this.id} finished ${now.toUTCString()}`);
try {
await message.edit({ embed: embed });
await this.getVotes(message);
await this.showResults(message.channel);
} catch (error) {
console.error(error);
}
}
async getPollMessage(message, bot) {
try {
return await message.guild.cache.get(this.guildId).channel.cache.get(this.channelId).messages.cache.get(this.id);
} catch (err) {
return;
}
}
Upvotes: 0
Views: 227
Reputation: 4520
Problem
I would assume the first step to converting your bot from DJS v11 to v12 would be actually looking at the DJS documentation. I have no clue what you're trying to do on this line:
return await message.guild.cache.get(this.guildId).channel.cache.get(this.channelId).messages.cache.get(this.id);
What is that supposed to be? That line is completely wrong.
A quick look at the docs would show you that message.guild
and message.channel
don't even have a cache
property; this makes sense, because DJS' caches are simply collections of guilds, channels, messages, etc. that have already been fetched from the Discord API (in order to prevent you from having to fetch that info from the API every single time you want to use a guild, channel, message, etc), whereas message.guild
and message.channel
represent a single guild/channel and not a collection of them.
So now we've established that that line is completely wrong, and will certainly cause errors. Well, that line is in a try/catch
, and when you catch an error you are simply doing return;
. That means message
will now be undefined
in finish()
, which is why it will log "Can't find poll message".
Solution
So how do we now solve this? Well, that depends on how you want this part of the code to run, but based on the info I've been given, I assume you want to use a guild ID, channel ID, and message ID to fetch the poll message. Now this guild ID could be the ID of another guild, or it could be the ID of the current guild. That means that using these IDs is beyond the "scope" of message
in getPollMessage()
, because a message object does not have any properties or methods that will lead you to another guild or a channel/message in another guild.
What object do you have access to that can get you information on any guild that your bot is in? Only one, the object that represents your bot itself: client
. Luckily, we can access your client
object through message
. So here's how we could fix the problematic line of code I pointed out above:
return await message.client.guilds.cache.get(this.guildId).channels.cache.get(this.channelId).messages.cache.get(this.id);
Explanation
So what does our new line of code do? Well, we first get our bot client's object using message.client
. Then, we use client.guilds.cache
to get all of the guilds that the bot is in, and specifically fetch the guild with the ID we are looking for. We then use guild.channels.cache
to do the same with channels and the channel ID we are looking for. And finally, we use channel.messages.cache
to once again do the same with the channel's messages and the message ID we are looking for.
Please look more closely at the docs when you run into this kind of issue in the future. I have provided the links to the documentation for each of the components of this answer below, for your reference.
Relevant Resources
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=guilds https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=channels https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=messages
Upvotes: 1