How to save a user's role history - discord

Is there any way to properly save a user's role history without the need for a database and/or a JSON file on Discord.js? I would like to create a function that logs the roles a user has at the beginning of the command and gives them back after a specific function is triggered.

There is an easy way to save a user's Role history on Discord without the need for a Database and/or any JSON file, you must add a few lines to your code in order to do this.
You first need to make sure your Discord bot has "Privileged Gateway Intents" enabled within the Discord Developer Portal panel, this will highly improve the accuracy of the bot's cache fetching tasks. Next, create a constant for the user mention or user ID, just like the following:
const saveRolesFor = (message.mentions.members.first() || message.guild.members.cache.get(args[0]));
With these 2 arguments, you will be able to state the id of the user or mention the user instead.
Then you must save the roles for the specified user inside another constant variable, just like the following:
const roleHistory = saveRolesFor.roles.cache.map(r => r.id);
The reason why mapping the roles is the best thing to do is due to the roles cache containing other elements, aside from the actual user roles, mapping them would simply include the actual roles of the user, preventing potential issues.
Now you must state the new role you'd like the user to have, just like the following:
let roleToAdd = message.guild.roles.cache.find(r => r.name === 'Role');
Time to remove all of the previous roles and add the roleToAdd to the user, you should do it as:
await(saveRolesFor.roles.set([roleToAdd.id]));
You will want to set the user's roles, as this function automatically removes any other role the user had, and add the single role you have stated, you no longer require the .remove and .add functions.
After these functions have been performed, you can be creative and add the rolehistory back after certain functions are completed.
A very simple example of this would be to add the roles back to the user once a message is sent, for example:
message.channel.send(`The roles for ${saveRolesFor} have been added back!`)
.then(saveRolesFor.roles.set(roleHistory));
All of the user's previous roles will be added successfully, and the roleToAdd will be removed, as you .set the roleHistory of the user, meaning you added those specific roles only. The .then function is calling the roleHistory set after the message is sent to the channel, you could modify it to properly fit into what you attempt to obtain out of your code.
This won't be the best way to add the roles back, as it will take less than a second, but the functions shown above will be highly useful at the time of creating a similar command.
Hope it was helpful!

Related

In Dialogflow, how do I set parameter to a custom value with my webhook (Dont want to ask the user)

If the user has already given me his name in entirely different intent, I store it in my DB.
However I need to request it again in another intent where webhook slot filling is enabled and given_name is a required value.
But if he has already given it to me, then there is no need to ask, so I want to grab the stored name in DB and fill the parameter (given_name) with this value, so I don't need to ask
How can I do this? Can I directly alter parameter values with my webhook?
Wouldn't it be possible to call you DB instead of using the parameter values?
If your database returns a name, you know that your user has mentioned a name in a previous intent before. From there you can just use the response from your database. If your database doesn't send back a name, then you would know when to ask your user about their name.
A simple example of this would be:
async function handleUsername(agent) {
// Check if the user has mentioned a name before in your DB.
const username = await callDatabase();
if (!userName) {
// Ask the user for their name.
}
// Continue the conversation.
}
Note: Since you mentioned that you are checking if the user had mentioned their name in multiple intents, it would nice to re-use the handleUsername() function across your webhook, if possible.

Different views for different users in reactJS

I am having a problem in ReactJS. I want to create a text editor ( with ReactQuill). So, I want different accounts for each user, such that, if one person creates a note and edits it in his account, the other person should not see this.for example this is my current stage - https://wright-text.web.app. After you create your account and login you would see a note called first note because that's the note I created. What should I do so that when you login you see no notes because you have not created a note, but when I login, I should see the note I had already created. How would I do this ??
You need to add a scope to notes endpoint so that user could only GET notes, which they have created.
You need to add query parameters in the API you are fetching. For example, if you are fetching data via a GET request, add in some specific user id that you assign to your user upon validation and then fetch data of a particular user using that id as a query parameter on your API.

Using Drupal rules to email users with a role but only once

We have been trying for a while to solve a Drupal Rules problem.
The situation is;
On every cron run we would like to check for Users who have a certain Role.
We will then send an email to those Users.
But we only want this email to go once.
We are thinking therefore that we will create a new custom/hidden User field called 'email sent'. Then the rule will;
EVENT: On every cron run
CONDITION: Check for Users who have the role AND have a null value in that field
ACTIONS: Send an email to the
users AND set the value of the user 'email sent field to 1.
We think this is possible but we can't see how you can set up a rule to do this.
Any help really appreciated
I think that you can resolve your problem via a custom module where create an action rule that set this fields. In situation like yours I follow this method. Search hook_rules_action_info().
Or if you use profile2 module you can see if there is a rule that can be set this value. I think that there is because Profile2 support Rules.
M.

Get a Google App Engine user by their user_id

In GAE, can you look up a User with the User object's user_id?
In other words, is there something equivalent to:
from google.appengine.api.users import User
user = User.get_by_id(user_id)
I don't think so.
... they certainly wouldn't just give you access to every holder of a google account!
The idea is that you store user ids as properties in your datastore, once they've logged in to your app.
There is a property type called UserProperty
http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html
So, you can query your list of users from your own datastore definition.
hth
This seems to be the only post on the internet regarding this and since I was looking for a solution, I thought I'd post what I found here.
What amir75 said about using the UserProperty is fine for storing the User object itself returned by the google.appengine.users module.
But if you need to lookup a User by the user_id field instead of the default email field, usually something like user = User(email = 'validmail#gmailorgapps.com')
You can use this to query by user_id. user = User(_user_id = 'validuserid') The valid user_id is something that you got earlier from calling user.user_id()
I'm not sure what amir75 is referring to about having access to all google accounts since the User object returned will only have the email address and nickname, and that too only if the user authorizes the application to access that information.
My use case for this is I want people to sign up on the site, but they need an administrator to confirm them for using the site. The form used by the administrator for confirming the users can use email id as the field to identify the checkbox for confirming the user, but given that it might change, the user_id seems to be a safer field to use.

Secure Menu Items Based on User

On this winform application I am writing, I want to secure one menu item from most users. It runs a month-end and cannot be easily backed out if accidentally run. The menu option opens up a window to prompt the user for some information before processing. I don't care where exactly I do the check, but I want to be sure only certain users can run this function.
A Google search (on my question title above) didn't turn up anything obvious. Can anyone point me in a direction to pick up who is signed into Windows and how to check if they are authorized?
This page has some code for getting user details and checking them.
This code:
public string GetloggedinUserName()
{
System.Security.Principal.WindowsIdentity currentUser =
System.Security.Principal.WindowsIdentity.GetCurrent();
return currentUser.Name;
}
returns the current user name. You could use this to check against your list of authorised users.
More details on the WindowsIdentityclass can be found here.
It has a Groups property which you could use to check for membership of a group rather than having to check individual users.

Resources