Is it possible to detect the removal of a reaction? - discord.js

I know you can detect the addition of a new reaction but using a ReactionCollector. But is it possible to detect the removal of a reaction?
I know i could keeping track of all reactions in a DB, and continuously poll the discord api and compare the DB version to the live version. But I'd rather not do this if there's a better solution out there.

Yes, the discord.js API has an event you can listen for: messageReactionRemove. You have to maintain a message cache, because it only fires for messages in your cache. You fill out your cache using fetchMessages.

You can make a event for this, called 'messageReactionRemove' to see if someone removes a reaction.
This is a simple example for a reaction role bot i have:
const events = {
MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
};
Then you can make for instance an if statement, and ask for the MESSAGE_REACTION_REMOVE
if (event.t === "MESSAGE_REACTION_REMOVE") member.addRole(guildRole.id);

Related

Which is better to listen events from smart contract, Contract.on or response.wait()?

Hi I am now trying to create a simple dApp to learn Solidity and interaction with it.
I am wondering which is a better way for listening events from smart contracts and applying state changes from transactions to the front end, using on method from Contract instance or wait from TransactionResponse.
I am using ethers.js, and it gives you something like this to listen events from smart contracts:
const myContract = new Contract(address, abi, provider);
myContract.on("eventName", (...args) => { // do something here}
Also calling the transaction method from the contract instance like the above, it returns Promise<ethers.ContractTransaction> type.
The returned object has wait method and it waits until the transaction has been mined like below:
myContract.someFunction(...args).then(async (response: ContractTransaction) => {
await response.wait();
// do something after transaction has been mined
}
**ContractTransaction is extended from TransactionResponse
Considering two ways to handle values after transactions have been mined, is one better the other?
Or is there even another way better than these?
I would imagine "better" depends on how I structure components (I am using React) and other things.
But I would like to know your preference at least :)
Thanks!
The first one would be better because it will passively listen for changes and update when they happen. With the 2nd one, you have to stop the execution of the function and whatever else you're doing in order to display something after the transaction happens. This means you'd have to set up some sort of loading page, but if the user leaves before the transaction is mined then what you will be waiting for is lost.

Subscription Refetch Updating Data

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.

Custom status using discord.py rewrite

This is my code
activity=discord.Game(name="Padamine", type=1)
Don’t worry, I put it in a #bot.event function. The problem is that my bot don’t put the custom status in his status. Can you help me?
Use change_presence() method to change bot status:
await bot.change_presence(activity=discord.Game("Padamine"))
If it's a static status (a status that won't change), it is recommended to use the activity keyword argument inside the bot constructor. If you want to make a dynamic status (a status that changes every now and then), use #tasks.loop of discord.ext.tasks (you can find more about tasks here)
# depending on what you name your variable, client/bot
client/bot = commands.Bot(command_prefix="yourprefix", activity=discord.Game("Padamine")) # removed type because there is no such key word argument
Also, it is recommended to not use await bot.change_presence() inside your on_ready event, I am mentioning this because many people do it and it's not good:
Discord has a high chance to completely disconnect you during the READY or GUILD_CREATE events (1006 close code) and there is nothing you can do to prevent it. As noted in the docs, on_ready is also triggered multiple times, not just once.
Basically: don't do anything in on_ready. (except printing that it came online obviously)

Discord bot that moves people based on game activity

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.

Salesforce Trigger workflow on record delete

I want to listen change in my legacy system whenever there is any change in SF object (add/update/delete). So I have created outbound message and workflow. But in workflow I don't see any way to fire if object is deleted.
Is there anyway I can trigger outbound message on record delete? I know have heard that it can be done by trigger. But I don't want to write apex code for this.
To the best of my knowledge it cannot be done, the workflow actions are decoupled from the workflow rule (you can even reuse them) so they probably do not receive the transaction scope and when they execute the record is already gone and any reference inside action would point to a non-existing data. Thus the only way I know how to do it is via trigger.
Here is a workaround. However this will only be able to capture deletion made via std. Salesforce UI.
1.Create a custom checkbox field "Is Deleted"
2.Override the Del link with a custom VF page, that first updates the record status to "Is Deleted", and deletes the record.
3.Write workflow rule using the "Is Deleted" field.
Perhaps a compromise architecture would be to write an extremely small and simple after delete trigger that simply copies the deleted records in question to some new custom object. That new custom object fires your workflow rule and thus sends the outbound message you're looking for. The only issue with this would be to periodically clean up your custom object data that would grow in size as you deleted records from your other object. In other words, your "scratch" object would just need periodic cleaning - which could be done on a nightly schedule with batch Apex.
Here's a delete trigger that would do the trick using Opportunity as an example:
trigger AfterDelete on Opportunity (after delete)
{
List<CustObj__c> co = new List<CustObj__c>();
for(Opportunity o : Trigger.old)
{
CustObj__c c = new CustObj__c();
c.Name = o.Name;
c.Amount__c = o.Amount;
c.CloseDate__c = o.CloseDate;
c.Description__c = o.Description;
// etc.
co.add(c);
}
insert co;
}
It's not ideal but at least this would save you from having to code your own trigger-based outbound messages. These can only be done using the #Future annotation, btw, since callouts directly from triggers are forbidden. Hope that helps.
write a single email send in the trigger delete event. You have it in less than 1 hour.

Resources