How does Getstream.io's own_reactions work? Thought it would contain the reactions from the user fetching - getstream-io

Getstream.io's own_reactions are storing all reactions, not just the ones from the user fetching. I thought it would contain the reactions from the user fetching the activities, or maybe even the own reactions of the user who created the activity.
In the documentation, it makes it seem like own_reactions would contain the reactions from the user fetching:
"# read bob's timeline and include most recent reactions to all activities and her own reactions"
https://getstream.io/docs_rest/#reactions
But there's not much other info about this.
In this code, for example:
let feed = client.feed('user', 'alice', streamUserToken);
let response = await feed.get({ limit: 30, enrich: true, reactions: {"counts": true, "recent": true, "own": true} })
I expect to see only Alice's own reactions, but instead I get all reactions, regardless of who they came from...

you have to specify the user_id when you read the feed (ie. different users can read the same feed)
make sure to pass user_id="alice" to feed.get
click here

Related

Get highest role of a member with mentions

How can I get the highest role name of a mentioned member? I tried something like this but it doesn't work. Thanks! :) btw this is a ban command and I need this bc my bot is crashing when someone is trying to ban a user with a higher rank than bot.
if(message.member.hasPermission('BAN_MEMBERS')){
const user = message.mentions.users.first()
console.log(user.roles.highest.name)
if(!user) return console.log("test1")
if(!args[2]) return console.log("test2")
const ddays = args[1]
What you could do is:
Getting a user's highest role:
Get the UserId from the Mention inside of the Message
Get the Cache from the RoleManager of the Guild in which the Message was sent in
(I don't know if the roles in RoleCache are sorted by position, so sort if needed)
Iterate through the roles in RoleCache and check if the UserId is contained inside a specific role
Get the position of the role
Getting the bot's highest role:
Repeat steps 2-5 for your bot (or integrate them within the previous iteration of the RoleCache)
Then compare both numbers and find out if the bot's "role number" is higher than the one of the user's.

Reacted user as channel name with Discord.js

I'm building a ticket system that whenever a user react's with the emoji, a channel get's created with him in it and the team that deals with the tickets. My problem is that when I want to display the user's name in the channel through the following code await message.guild.channels.create(``🎫│${reactor}``, {type: 'text',} and reactor is defined by const reactor = reaction.message.guild.members.cache.get(user.id); I get the user's ID in the channel name. My question is if there's a way to name the channel 🎫│User instead of 🎫│ID (with ID I mean the user's Discord ID, not the actual text 'ID').
There's quite a simple solution to your issue, being that you're attempting to use a GuildMember object, instead of a User object.
At the time that I'm writing this (And it will probably stay like this forever), you're not able to get the username of a GuildMember, but would first of all have to convert it into a User object.
This could be very easily done with the following code:
const member = reaction.guild.member(user.id)
const name = member.user.username
message.guild.channels.create(`${name}s ticket`, {
type: 'text'
})
I'd also like to mention that you're not required to add message after reaction when you're trying to get the guild object! You can simply use the reaction variable you've created in your callback and use it to get the guild object using reaction.guild!

Filtering by multiple fields using Firestore

To put simply. It is a filtering problem that i am having with Firestore. Take this model as an example:
Collection: Room
- Document#1
- query_role: Array - Limit 10
- query_user: Array - Limit 10
- players: Array<Object> [{
playerId: string
user: Object<id, firstname, lastname, ...>
role: Object<id, name, ...>
}]
...
My application has a page called rooms that anyone could use to search for (you guessed it) rooms.
With filters to narrow down the type of room they can enter but for this example, we'll just use role as the only filter. Simple stuff or so I thought.
The logic behind the search/filter page are to:
Find all rooms that still have space in them
Use the filter to find rooms that still have a certain role in them (e.g. Mafia, Doctor, Villager, etc...) that aren't assigned to any users
Collection: Room
- Document#1
- query_role: ['111', '222', '333', '333']
- query_user: ['user1', 'user2', 'user3', false, false, false, false, false, false, false]
- players: [{
playerId: 'djs123d21sSd213r'
user: null
role: {
id: '111',
value: 'Mafia'
...
},
...
}]
...
Every role that is created for this room is inserted into the players->role with the user set to null. The role is then inserted into query_roles to use as a filter on the page if a user chooses to search by available roles:
.where('query_roles', 'array-contains', role)
Now the problem is, with firestore. You can only use array-contains once in a query. I keep getting errors like this: A maximum of 1 \'ARRAY_CONTAINS\' filter is allowed.'. The other array-contains as i've mentioned before, is being used on query_users to find rooms that still have space by doing:
.where('query_user`, 'array-contains', false)
I cant put them in an object like so:
{
user1: true,
user2: true,
false: true,
false :true
}
since all of those false would slap into one field and it would be the same with roles. Since there can be multiple roles that are the same, it would merge into one field.
The only way I can think of is manually setting up a flag that says full: true|false that will be used to determine if a room is full or not. I'd still need to use the query_user because in the application i use it to search for all Rooms a specific user is in.
I am writing to see if there are any alternative ways anyone else can think of without messing about with creating flags. Long question, but I appreciate you reading up until here.
Find all rooms that still have space in them
To solve this you can simply add into your room document two new properties. One would be named numberOfRoles and the other one numberOfUsers. Once you add a new role to the query_role array increase the value of numberOfRoles by one and every time you add a new user to the query_user array increase the value of numberOfUsers by one. In this way you can simply query your rooms collection using:
roomsRef.where('numberOfRoles', '<=', 10).where('numberOfUsers', '<=', 10)
Chaning multimple array-contains functions is not allowed in Coud Firestore but chaining multiple where() calls will work perfectly fine.
Use the filter to find rooms that still have a certain role in them (e.g. Mafia, Doctor, Villager, etc...) that aren't assigned to any users
Once you get a positive result from the above query then you can get the content of your query_role array and see the available rolls.
Every role that is created for this room is inserted into the players->role with the user set to null.
There's no need for that anymore.
The only way i can think of is manually setting up a flag that says full: true|false that will be used to determine if a room is full or not. I'd still need to use the query_user because in the application i use it to search for all Rooms a specific user is in.
Setting a flag to true|false will not solve your entire problem. Please note that if you want to search for a specific user in the query_user array you can still do that since you perform only a single query using the array-contains operator.

Discord bot cannot get users collection by ID

I must be missing something because I am fairly sure this should work, since I have used this same method before in a different command in the same bot...
I have a bot that connects to a mysql database. Inside a table in the database that stores the info for a match of a game I'm making, I store the Discord ID's of each player. After pulling those ID's from the table, specifying which one I'd like the user object retrieved for, and trying to use .get() (also attempted to use .find() with a username instead), both result in "undefined"... I have a feeling the bot doesn't find the entire user collection, because when I console.log(client.users), I get Collection [Map] {} So, it looks to be empty? I have also put in the ID itself instead of a variable and it still can't find it. So, I have a feeling the bot just doesn't get the users collection for some reason.
In the below code:
playerIDArray = the player ID's I stored at the start of the match of the game, pulling them from the mysql table. Stored in array format ("player 1's ID" as index 0, "player 2's ID" as index 1, "player 3's ID" as index 2, etc)
playerIndex = the index of the target player in playerIDArray, found in an earlier step before the below code runs, that I know returns what I expect.
const Discord = require('discord.js');
const client = new Discord.Client();
const mysql = require("mysql2/promise");
...
...
var targetUserID = playerIDArray[playerIndex]
var targetUser = client.users.get(targetUserID)
console.log(targetUser)
Each time I run this, I get "undefined" and for the life of me can't figure out why... I just want to retrieve the user group so I can send a message to that user, giving them an opportunity to counter before I modify the mysql table and move cards around that would then need to be immediately altered again to restore what was just removed via a counter card.
I must just be dumb and am missing something but I'm lost as to what to try now... Thanks for any help in advance.
You can only use client.users ( and other discord.js Collections ) after the ready event fired.
So you need to put the code that uses client.users into
client.on('ready', () => { /* CODE */});
ready will be fired after successful client.login('token');.

How to protect properties for different roles using loopback

I was just wondering how you would restrict property access to the $owner role only. For instance in my case I have a Joke which has an Author. The Author has User as base. I would like other "Authers" / Users to see who created the Joke, but they should not be able to see the Authers email, only if the Author is the $owner of the Joke itself it should be OK to show their email, just for the sake of this case.
Looking at the built-in User model you can see that they use the hidden feature to hide the password, but using that for their email will also hide their email for the $owner, which is not what I wanted
Let me know if something is not clear.
Thanks in advance
Register beforeRemote hook and check if current user is the $owner.
Joke.boforeRemote('findById', function(context, joke, next) {
// 1. find current user by id, using context.req.accessToken.userId
// 2. check if he is owner or not, by Role.isOwner
// 3. remove email from returned joke instance if user is not $owner
})
Note: it can be a bit complicated to cover all endpoints that return Jokes. But is there another way to do it?
To modify the output/results, you can use the afterRemote hook, as per the docs. The output/results are stored in ctx.result.
'findById' hooks into your GET requests when the call is like GET http://myModel/id. Use 'find' if you are not including the id in your request e.g. GET http://myModel. Just notice that in case of 'find', the returned instance(s) (joke(s)) is usually not just one so it is in an array of objects.
Joke.afterRemote('findById', function(ctx, joke, next) {
//your code
});
Get the id of current logged-in user: var currentUser = context.req.accessToken.userId
Compare the user id of the current logged-in user with that of the joke owner. If both are not the same (i.e. if (!(currentUser == joke.userId))), then:
before calling next(), remove the email attribute from returned joke instance. Because sometimes some ways don't work, here are a few:
delete ctx.result.email;
ctx.result.email = '';
loop through the attributes and transferring them to a new var, except the email, then save that new var the result: ctx.result = newVar;
You can create your own role resolver. See https://github.com/strongloop/loopback-example-access-control/blob/master/server/boot/role-resolver.js for an example. Just add your own logic once you determine the user.

Resources