Discord.js messageReactionAdd Not firing - javascript

I understand that messageReactionAdd only works with cached messages but even after manually caching them it still wont fire.
Currently my code is:
client.once('ready', () => {
console.log(config.readymesssage);
var channel = client.channels.resolve('761577724379791450');
channel.messages.fetch({limit: 90}).then((message) => {
console.log("done")
})
});
client.on('messageReactionAdd', async(reaction) => {
console.log("reacted");
});
I have tried using raw events I used the code block from Here and I have also tried using partials, neither of which seem to be working. I'm unsure as to what else I cant try.

Enabling intents has fixed the issue, they first needed turning on in the developer portal and then I needed to add the following lines,
let Intss = new Discord.Intents(Discord.Intents.ALL);
const client = new Discord.Client({ws: { intents: Intss }});
Now the event is firing as expected.

Another working solution that is most likely better is
const bot = new Discord.Client({ partials: ['USER', 'REACTION', 'MESSAGE'] });
Now, you will have to check for partials before doing anything with the data (and fetch the data if it is partial). But this way, you won't have to deal with intents and if your bot scales to 100+ servers, you won't have to wait for verification either.

Related

copy to clipboard from Firestore with Navigator.Clipboard API in ReactJS

I'm new here, and new to coding (started learning when the whole pandemic shenanigans started) right now working on a ReactJS project. one of the element I want my app to do is: on click of button copy text from Cloud Firestore.
How I have my code set up it gets today's timestamp as to when the button was pressed and pushes it to an array pressedOnDates (has dates of all the times the button was pressed) and also updates the date in lastPressedOn (last time it was pressed on) Then using Navigator.Clipboard I copy sampleMessage out of that item's document (why I used it, idk? maybe had something to do with my laziness, and maybe because I saw this feature on home brew's website and they used it, and it worked on there website) Lastly, I have a function call to a function that updates or "repulls" data from Cloud Firestore, and because I get new data fed to my components they redraw in ascending order, component that was just pressed on moves to the bottom and the component that was pressed some time ago moves to the top.
Problem I'm having: this line of code "await navigator.clipboard.writeText(item.sampleMessage);" doesn't work on iPhones or iPads, and because there is no break and it is an await the app is stuck on this broken line of code, and 1. the app never copies the sample message, and 2. (even though it pushes new timestamps to Cloud Firestore) data never gets "repulled" and components never redraw. this code works perfectly fine on MacBook and Android phone (at least as I intended: pushes Timestamp, copies message, and redraws)
Here's my code:
const handleUpdateDate = async (itemID, item) => {
try {
const todayTimestamp = Timestamp.fromDate(new Date());
await db
.collection('sample')
.doc(itemID)
.update({
pressedOnDates: firestore.FieldValue.arrayUnion(todayTimestamp),
lastPressedOn: todayTimestamp,
});
await navigator.clipboard.writeText(item.sampleMessage);
await updateItems();
} catch ({ message }) {
console.error(`Error # handleUpdateDate, Error:${message}`);
}
};
If you can please help me out and point out what's wrong with my code, or suggest a different way to copy to clipboard, I would be more than grateful. Thank you to all of you in advance. May StackOverflow always have an answer to you're question. May the Dark Themes repel all your bugs 🙂. Wish you all the very best.
#jacobvr (good friend) helped me out on Github.
He told me to setup the fn like so:
copy to clipboard (navigator.clipboard)
push to pressedOnDates[] and update lastPressedOn
updateItems
His explanation: "this way you are not waiting for db call to finish to copy to clipboard"
What he proposed:
const handleUpdateDate = async (itemID, item) => {
try {
const todayTimestamp = Timestamp.fromDate(new Date());
await navigator.clipboard.writeText(item.sampleMessage);
await db
.collection('sample')
.doc(itemID)
.update({
pressedOnDates: firestore.FieldValue.arrayUnion(todayTimestamp),
lastPressedOn: todayTimestamp,
});
await updateItems();
} catch ({ message }) {
console.error(`Error # handleUpdateDate, Error:${message}`);
}
};
This is how I set it up ( both ways work on all devices, but copy to clipboard
has to be first action in the fn ):
const copySampleMessage = async (item) => {
try {
await navigator.clipboard.writeText(item.sampleMessage);
} catch ({ message }) {
alert(`Error # copySampleMessage, Error:${message}`);
}
};
const handleUpdatePressedOnDate = async (itemID, item) => {
try {
const todayTimestamp = Timestamp.fromDate(new Date());
await copySampleMessage(item);
await db
.collection('items')
.doc(itemID)
.update({
pressedOnDates: firestore.FieldValue.arrayUnion(todayTimestamp),
lastPressedOn: todayTimestamp,
});
await updateItems();
} catch ({ message }) {
alert(`Error # handleUpdatePressedOnDate, Error:${message}`);
}
};
Thank you to all who responded and helped out with my question, wish y'all the best.

Why is guild.members.fetch timing out

Hello so I am writing a discord bot and all I want is an ordered list of members ordered by their ids. To achieve this I am running following code, however it just console.logs "Couldn't fetch members", with no further errors. :(
Upon further inspection, I discovered that it is a timeout error. Everything else works fine and I think that the const guild isn't the problem, but other than that hope that you people can help, Cheers!
const IDs = new Map();
var repeat = new Boolean(false);
var randomInt = new Number(0);
client.on('ready', () => {
console.log('Ready!');
const guild = client.guilds.cache.get("xxxxxxxxxxxxxxxxxx");
guild.members.fetch().then(members => {
console.log("Found the members");
IDs = members.map(user => user.id);
const OnlineMembers = members.filter(member => member.presence.status == "online");
}).catch(e => console.log("Couldn't fetch members."));
});
P.S. This is running on a Raspberry Pi over Nodemon --inspect, in case that changes anything
I found the solution, sorry if I bothered anybody, there is this really small setting in the Discord Dev Hub. That enables or disables this feature, obviously I was to dumb to turn it on from the start, thank you to anybody who took some time for this, Cheers!

Issue with bot adding server members to its membercount | discord.js

My bot got added to a 4.9k server and everytime i ran the "zbotstats" or restarted the console it didn't add those 4.9k members what is the issue here?
Before the 4.9k server was added it had 3.8k which shouldve added up to 8.7k
My code for guild user count:
console.log(`${bot.user.username} is online on ${bot.guilds.cache.size} servers!`);
console.log(`lovell is looking over ${bot.users.cache.size} users`)
Using: Discord.v12
This is because bot.users.cache.size is the number of cached users. And not all the users of all the guilds are cached. So, how to fix?
First way: cache ALL the members
You can use the following code to force discord.js to cache all the members in all the servers:
const bot = new Discord.Client({ fetchAllMembers: true });
bot.on('ready', () => {
console.log(client.guilds.cache.size); // right count
});
You will see that bot.users.cache.size will be the right count. Note that this way will cost a lot of RAM if your bot starts to be on a lot of big servers.
Second way: use guild.memberCount instead of the cache
You can use:
const users = bot.guilds.cache.map((guild) => guild.memberCount).reduce((p, c) => p + c);
console.log(users); // right count

Can't send embed message on join

No idea why it won't work, the exact code worked on my old bot. Code:
client.on("guildMemberAdd", member => {
const Discord = require("discord.js");
const embed = new Discord.RichEmbed()
.setTitle("**Please be sure to read our rules carefully thanks**")
.setAuthor("Welcome to BACKUP")
.setColor(3447003)
.setDescription("Please enjoy your stay")
.setThumbnail(message.author.avatarURL)
client.channels.get('505107608391254030').send({embed});
})
}
The thing that confuses me most, is that if I replace that code with this code, it works fine.
client.on('guildMemberAdd', member => {
member.guild.channels.get('505107608391254030').send("This works, but embed does not, fix it boi, line 102");
});
(On the code that did not work, I tried: client.channels.get, member.channels.get, member.guild.channels.get, client.guild.channels.get
The problem is when you are finding the channel, client.channels.get isn't a method. doesn't work in this situation for reasons I'm not aware of
You have to use client.guilds.get(GUILD_ID).channels.get(CHANNEL_ID).send({embed});

Set Timeout not working in React Native - Debug JS Remotely

I drove myself nuts trying to figure out what the problem is with my code. I am building a React Native application and developing in the IOS Simulator after running run-ios. I enabled Debug JS Remotely so I can verify the condition of my tokens returned from the server.
I am attempting to build a simple service that will refresh my tokens using a setTimeout.
let sessionTimeout = null;
export const session = (response) => {
let tokens = response.tokens;
Store.set(tokens);
setSessionTimeout(tokens.access.expiresIn);
};
const setSessionTimeout = (duration) => {
let time = duration - 5000;
clearTimeout(sessionTimeout);
sessionTimeout = setTimeout(() => {
console.log('settimeout called');
refreshToken();
}, 1000);
};
const refreshToken = () => {
let tokens = Store.get();
refresh(tokens)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
};
In the above code a began my initial test by hardcoding 1000 and it worked fine. It successfully made the call to my server. I then attempted to implement the proper time duration and found it does not work. Even by replacing the 1000 with 2000 causes it to fail and the call to the backend is never made.
I then tried using the following packages and got the same result:
react-native-timer
react-timer-mixin
After banging my head on the wall for two days and having another attempt on stack shot down because my issue could not be reproduced I disabled the Debug JS Remotely and everything works as expected.
However now I am wondering how I am supposed to debug the frontend portion of my application with out the use of this feature. If anyone has any ideas or advice on issues like this I would appreciate the feedback as this is my first React Native app.

Categories

Resources