Discord.js message.react is not a function - javascript

I have a discord bot set up that does reaction roles. I use IDs to cache the messages that need to have reactions. I make the bot first react with the according emojis before it handles them. This is what caching the messages looks like:
const guild = await client.guilds.fetch(info.GUILD_ID)
const channel1 = guild.channels.cache.get(info.MAINROLES_CHANNEL_ID)
const message1 = await channel1.messages.fetch(info.MAINROLES_MESSAGE_ID)
const channel2 = guild.channels.cache.get(info.HELPERROLES_CHANNEL_ID)
const message2 = await channel2.messages.fetch(info.HELPERROLES_MESSAGE_ID)
const channel3 = guild.channels.cache.get(info.VERIFICATION_CHANNEL_ID)
const message3 = await channel3.messages.fetch(info.VERIFICATION_MESSAGE_ID)
The problem I have is with message 2. When doing message1.react("🇱") everything works fine. However, when I do message2.react("🇱") I get an error. I see no reason for the error to be thrown as I get both messages the exact same way. Does this have to do with discord bot permissions? Or is it something else that I'm doing wrong?
This is the error:
(node:4102) UnhandledPromiseRejectionWarning: TypeError: message2.react is not a function
at Client.<anonymous> (/Users/denesgarda/Documents/GitHub/cmd/bot/index.js:47:12)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4102) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4102) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I can't comment yet but I will try to help you. So I think that the message1 and message2 are stuck because they are entered in the same time.
I am not sure but you can try to make it like this
const guild = await client.guilds.fetch(info.GUILD_ID)
const channel1 = guild.channels.cache.get(info.MAINROLES_CHANNEL_ID)
const message1 = await channel1.messages.fetch(info.MAINROLES_MESSAGE_ID)
message1.react(emoji);
const channel2 = guild.channels.cache.get(info.HELPERROLES_CHANNEL_ID)
const message2 = await channel2.messages.fetch(info.HELPERROLES_MESSAGE_ID)
message2.react(emoji);
const channel3 = guild.channels.cache.get(info.VERIFICATION_CHANNEL_ID)
const message3 = await channel3.messages.fetch(info.VERIFICATION_MESSAGE_ID)
message3.react(emoji);
If that doesn't work maybe try to get the id using const channel = bot.channels.cache.find(ch => ch.id === 'channelid'); this function

Related

Why am I receiving an Invalid Form Body error with Discord API?

I'm making a discord bot but when I create a ping command it says the following:
(node:37584) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
embed.footer.icon_url: Scheme "flashybot" is not supported. Scheme must be one of ('http', 'https').
embeds[0].footer.icon_url: Scheme "flashybot" is not supported. Scheme must be one of ('http', 'https').
at RequestHandler.execute (C:\Users\niels\Documents\VsCode Projects\Discord Bots\FlashyBot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async RequestHandler.push (C:\Users\niels\Documents\VsCode Projects\Discord Bots\FlashyBot\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
at async callback (C:\Users\niels\Documents\VsCode Projects\Discord Bots\FlashyBot\commands\misc\ping.js:15:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:37584) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:37584) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
My code for ping.js is:
const { MessageEmbed } = require("discord.js")
module.exports = {
commands: ["ping", 'pong'],
expectedArgs: "",
permissionError: '',
minArgs: 0,
maxArgs: null,
callback: async(message, args, text, client) => {
let pinging = new MessageEmbed()
.setTitle('Pinging....')
.setColor("RED")
.setFooter(client.user.avatarURL(), client.user.username)
const msg = await message.channel.send(pinging)
let embed = new MessageEmbed()
.setTitle('Pong! 🏓')
.addField("Api Latency", `${Math.floor(message.createdTimestamp - message.createdTimestamp)}ms`)
.addField("Latency", `${Math.floor(client.ws.ping)}ms`)
.setColor("LIGHT_GREEN")
.setFooter(client.user.avatarURL(), client.user.username)
.setTimestamp()
msg.edit(embed)
}
}
Can someone help me with this? I tried doing message.client.user.avatarURL() and also the displayAvatarURL() function but it keeps giving this error
Your logs say
embed.footer.icon_url: Scheme "flashybot" is not supported. Scheme must be one of ('http', 'https'). embeds[0].footer.icon_url: Scheme "flashybot" is not supported. Scheme must be one of ('http', 'https').
Which seems to indicate the url provided for your icon is incorrect (and does not start with http or https which is required).
I assume this is client.user.avatarURL() which is incorrect, try to replace it with a hard coded picture link and see if it helps. You may have an incorrect value in avatarURL...

My code only seems to be working with some subreddits

It works with "memes" and some other subreddits but not with "kungenavedsbyn". I'm probably really dumb but I can't figure out how to fix it.
bot.on('message', async msg => {
if (msg.content === '-örsk') {
let subreddits = [
"kungenavedsbyn"
];
let subreddit = subreddits[Math.floor(Math.random()*(subreddits.length))];
let img = await api(subreddit)
const Embed = new Discord.MessageEmbed()
.setTitle(`Här har du lite örsk`)
.setURL(`https://www.reddit.com/r/kungenavedsbyn`)
.setColor('RANDOM')
.setImage(img)
msg.channel.send(Embed)
}
});
Here's the message I get when I write the command in dc:
(node:11700) UnhandledPromiseRejectionWarning: err: [object Object]
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11700) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async functioch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/ap
(node:11700) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled

Emoji List Command Discord.js v12

I created an emoji list command here is my code of the command:
const { MessageEmbed } = require('discord.js');
module.exports = {
name: "emojis",
description: "Gets a guild\'s emojis",
async run (client, message, args) {
const emojis = [];
message.guild.emojis.cache.forEach(e => emojis.push(`${e} **-** \`:${e.name}:\``));
const embed = new MessageEmbed()
.setTitle(`Emoji List`)
.setDescription(emojis.join('\n'))
message.channel.send(embed)
}
};
However I get this error in case the characters in the embed exceeds 2048 letters:
(node:211) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
embed.description: Must be 2048 or fewer in length.
at RequestHandler.execute (/home/runner/Utki-the-bot/node_modules/discord.js/src/rest/RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:211) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:211) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Is there any way the bot can still show the emojis and there names. By using discord-menu or like that. I was unable to understand how to do that. Can you help me out? Thanks in Advance
As you can probably tell by the error message, your embed description is too long. You can split your message into several parts by using string.split() and send each shortened string as a separate message. Here's a rudimentary example.
const charactersPerMessage = 2000;
// we're going to go with 2000 instead of 2048 for breathing room
const emojis = message.guild.emojis.cache.map(e=> { return `${e} **-** \`:${e.name}:\`` }); // does virtually the same thing as forEach()
const numberOfMessages = Math.ceil(emojis.length/charactersPerMessage); // calculate how many messages we need
const embed = new MessageEmbed()
.setTitle(`Emoji List`);
for(i=0;i<numberOfMessages;i++) {
message.channel.send(
embed.setDescription(emojis.slice(i*charactersPerMessage, (i+1)*charactersPerMessage))
);
}
Do take note that emojis is now a string rather than an Array.

UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation

So I was watching youtube regarding webscraping and would like to give it a go. I tried mimicking exactly how the tutor teaches but I land into a problem right away.
(node:20976) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
at rewriteError (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:261:23)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext._evaluateInternal (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:161:64)
at async D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:90:30
at async DOMWorld.$x (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:96:26)
at async scrapeProduct (D:\Users\Win81\Desktop\Web-scraper-snkrssg\scraper.js:10:18)
(node:20976) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:20976) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Here is the sample of my codes:
const puppeteer = require('puppeteer');
async function scrapeProduct(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.goto(url)
const [el] = await page.$x('//*[#id="block-1565393554829"]/div/div/div[2]/div/div[1]/a/div/img[2]');
const src = await el.getProperty('src');
const srcTxt = await src.jsonValue();
console.log({srcTxt});
}
scrapeProduct('https://shopnicekicks.com/pages/calendar');
I hope someone could teach me where I'm going wrong.

Play random sound

I'm making a discord bot and if I type $join into chat, I want the bot to join the voice channel that I'm in, and play a random sound.
case"join":
message.delete( {timeout: 5000})
const voiceChannel = message.member.voice.channel
if(voiceChannel) {
const connection = await voiceChannel.join()
const soundFile = fs.readFileSync("./sounds/")
const randFiles = soundFile[Math.floor(Math.random() * randFiles.length)]
const dispatcher = connection.play(randFiles)
} else {
message.reply("you need to be in a voice channel!").then(message => message.delete( {timeout: 5000}))
}
break;
I'm getting this error:
(node:13932) UnhandledPromiseRejectionWarning: Error: EISDIR: illegal operation on a directory, read
at Object.readSync (fs.js:524:3)
at tryReadSync (fs.js:349:20)
at Object.readFileSync (fs.js:386:19)
at Client.<anonymous> (C:\Users\PC\Desktop\doge_bot\doge-bot.js:124:38)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:13932) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13932) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not
handled will terminate the Node.js process with a non-zero exit code.
Have you read the documentation for readFileSync()? The only OS that readFileSync() successfully returns data on a directory path is on FreeBSD.
Instead, what it appears you're trying to do is grab a list of the files at a directory path; for this you could use fs.readdirSync():
const soundFile = fs.readdirSync("./sounds/")
fs.readFileSync("./sounds/") is for reading the contents of a file.
You're probably looking for fs.readdirSync("./sounds/") which gives you an array of files in a directory.

Categories

Resources