Basically, I am trying to make it so a user can trigger a command that gives them 30 seconds to hit various reactions. It would collect reactions and later use them.
Everything I have tried to be able to use a command and get it to wait for multiple reactions has not worked.
Flat out, I am not good at this. I don't really even know a lot about JS. I am just trying to put something together for my friends and I. I am not asking someone to do everything for me, just need a framework.
You can use Message#awaitReactions() (the discord.js guide has a topic on collectors you might want to read). The first parameter to this function is a filter, which passes reaction and user.
If you're adding reactions beforehand, you might want to make sure that the reaction is one of the aforementioned selected few. You should also make sure that the user is the message author.
The second parameter is for options. The option you're looking for is time. This property takes a number of milliseconds, and when that amount of time is up, it will resolve the promise the function returns in a Collection. Your finished code might look like this:
// react with your emojis
await message.react('😀');
await message.react('😦');
// create the filter function
const filter = (reaction, user) =>
['😀', '😦'].includes(reaction.emoji.name) // compare emojis
&& user.id === message.author.id // compare users
// call the function
const collected = await message.awaitReactions(
filter, // pass the filter
{ time: 30000 } // set the time limit
);
// log your findings!
console.log(collected);
Related
How do I make it so that if 2 users execute a command that needs to detect if I press buttons, only that user can react to their own interaction, and if you press the others one, you will get an ephemeral message saying that it’s not yours?
I’ve thought of a solution, but I don’t know if it works:
Changing the filter in a collector.
Problem is, I don’t even know if it’s possible to change a filter, and if multiple people run that code at once, will the filter update to the most recent thing?
I know its a lot of questions, so the main question is just the title.
The way I handle it is like this:
create a filter to include the ID's of the person/people you want to be able to use the button
create the interaction collector with that filter
=> The collector will only trigger for people you specified, everyone else will get a message saying that the interaction failed.
Alternatively, you could simply make a filter that collects all interactions, and then check for the ID of the person that interacted in the code.
If it's not someone that should be able to use the button, return an ephemeral message.
//filters for only for interactions on a certain message and by a certain user
const filter = (interaction) => interaction.user.id == <userid> && interaction.message.id == <messageid>
//create a component collector for 5 minutes for that specific message or alternatively an entire channel (if you create it for a single message you don't need to check for the message ID in the filter)
const collector = <message/channel>.createMessageComponentCollector({filter: filter, time: 30000});
//when the filter collects something, this runs
collector.on('collect', async i => {
//alternatively check for user IDs here
if(interaction.user.id != <userid>) return <error message>
//your code here
...
})
//this runs once the collector stops
collector.on('end', async collected => {...})
I wanted to get your opinion on something.
I'm trying to understand how a subscription works. however, I couldn't find a way to pull an array of objects in a subscription. for example, if I use createMany, I can not return all the result via subscription.
The second issue is if I return a single item for example if it's a new item, I have to "manually (air quote)" add that item to the list that is already displayed. But this feels to me I don't actually display real-time true data.
So my question is using something like
useEffect(() => {
// refetching original query when subscription is triggered
refetch();
}, [updatedNotificationData]);
would there be any downside like hitting up the server more than I should? let's say every time there is a refetching happens I might be pulling thousands of notifications (I know there is caching but still) or is there a better way to deal with bringing new data.
Also, I tried adding subscribed data to the original list but for some reason react adds 2 of the same item every time.
Thanks in advance if you can give me in the right direction.
if I use createMany, I can not return all the result via subscription.
That shouldn't be a problem if you define the return type of the subscription as array.
type Subscription{
onChange:[ObjectType]
}
It would allow you to avoid fetching again but updating cache can get a bit complicated.
Also, I tried adding subscribed data to the original list but for some reason react adds 2 of the same item every time.
In case you are using the the subscribeToMore method it's not really reacts fault but the way how the updateQuery method works: github issue regarding this.
My workaround was to subscribe via the useSubscription hook and handle the cache modifications inside the onSubscriptionData callback apollo documentation and also setting the useQuery hooks skip option once I get the data so it wont query on each rerender.
Hello everyone I'm new to coding but I want to code the bot mentioned in the title so my questions are:
-Is there a command that reads the game activity?
-What is the command that makes the bot move someone?
I set the basics up but don't really know where to go from here:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Online');
});
client.login('*the token*');
All of a user's game activities are stored as an array, activities, within the presence property of your user object.
For example, if you want to get the game statuses of the author of a message, you can use <message>.author.presence.activities to get that array. However, since a user may have multiple statuses (custom statuses, games, Rich Presences, etc), you'll generally want to get a specific activity.
If you need to check if the name of the status is a certain one, you'll need to get the specific index first, such as <message>.author.presence.activities[0].name. If you want to check each status and see if any of them align with a needed name, you can use a for...of loop.
For more information, here is the documentation on the presence of a user.
As for "moving someone," you are not giving any more explanation as to what you mean, so that should be implemented once your bot can properly detect certain games.
In the meantime, try following a guide on how to turn user input into actual commands, and then this may make more sense if you are still facing issues.
So, I'm trying to limit the amount of reactions that can be done by each user. I want a user to only be able to react to the message once. If they react more than once, then the reaction is removed and the if statement for reaction is not executed. So,
React once, function executed
React twice, "You can't do that" remove reaction
I currently have the reaction collector setup and working, however I need to find out how to get a const to equal the user.id and the amount of reactions that user.id has.
If anyone has any insight, please let me know. Thank you <3
I am trying to use discord.js to post a topic at 3 different intervals: every day, every week and every month.
I will have a different file with the topics for each day. I also want each chat room for the separate topics to clear before posting the new topic.
I am using the code below to automate the interval at which it posts but I need the clear function and also maybe a more streamlined way to post it, or a way to post at a specific time.
bot.on('message', function(message) {
// Now, you can use the message variable inside
if (message.content === "$Next") {
var interval = setInterval(function() {
// use the message's channel (TextChannel) to send a new message
message.channel.send("Test Topic")
.catch(console.error); // add error handling here
}, 1 * 10);
}
});
I need to automate these functions and to clear the chat before it posts the new topic. My current code works for automatically posting at the given interval. I need help clearing the channels chat and automatically posting a topic from a saved file.
Use the node-schedule package to help with the defined intervals or specific time.
As for pulling the topic from a file, please take a look at Node's native fs module.