How to mention a user mentioned in the command discord js - discord.js

So im working on a discord bot, and working on a command where it mentions a user. For example: -fakekick #user and the bot would say User has been kicked I've read through the docs and found a few sites but I couldn't really understand it as I just started discord js a few days ago. Thanks

To get the user object of a mentioned user, you can very simply use the mentions extension of the message object. We can very simply use it in order to define our user, and later on, mention him in a future message. That can be done very simply as so:
const user = message.mentions.users.first(); // Would get the first mentioned user's object
message.channel.send(`${user} has been kicked!`); // To mention a user, you simply need to mention his user object.

Related

How to save a user's role history

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!

Discord.js How to list the display names of all members with a specific role

I have a command that currently gives a count of how many people have a specific role. That functions properly. I am attempting to make a subcommand of that command that will list the members who have that role and I can get it to list their names as usernames, but I'm looking for it to list it as a display name instead.
This is the code that currently works
let nameList= message.guild.roles.resolve('Role-ID').members.map(m=>m.user.username).join('\n');
The "Role-ID is the actual ID in my code. I know that the displayName is a property of the GuildMember but I don't know how to integrate it here. Thoughts?
I figured it out, but instead of deleting my post I figured I would post the solution.
All I needed to do was change .user.username to .displayName
let nameList= message.guild.roles.resolve('Role-ID').members.map(m=>m.user.username).join('\n');

YAGPDB Discord Bot - Custom command to limit reaction

I am trying to create a voting channel where the YAGPDB bot will automatically react "👍" and "👎" to every message posted in that particular channel. And people will be able to react only one of them. Can someone advise on how to do it?
I was able to write the code this far:
{{addMessageReactions nil $.Message.ID ":thumbsup:" ":thumbsdown:"}}
On the Role Command category under Role Commands, just set it to Mode: Single

Why does client.users.cache only show 2 users discord.js

For a server of mine, I need my bot to DM everyone at regular periods of time. I use
client.users.cache.get(args[1]).send(toSend);
to do this, where args[1] contains the user's id and toSend is the message to send. However, it just turns up Cannot read property 'send' of undefined. Then I tried logging client.users.cache to the console and it appears that only 2 users are in it, the bot itself and me. Why is this so, given that the server has at least 30 users?

Watson Dialog does not recognize continuation of conversation

When trying to use the Dialog tool to get a feel for how the APIs work, I ran into a problem where a POST to /conversation creates a new conversation, instead of continuing an existing one. I am using the docs found at : http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/dialog/api/v1/
An initial POST returns a conversation and client id, but subsequent requests with those values added, along with an input value, just return information for a new conversation. Are these docs inaccurate?
Here is a screenshot from one of my many attempts at getting this to work. The client and connection IDs are from a previous POST to /conversation
http://imgur.com/4035dWe
Thanks for your help!
Your first call to Converse you should not specify the conversation ID. Dialog will return a conversation ID with the first response.
You then use that ID going forward to maintain the conversation. It is unclear if you are doing this in the example above.
It turns out I was using the incorrect encoding for the conversation / client ID's and input. Watson expects the form values to be URL encoded. Once I made that change, the problem was resolved.
Thanks to everyone who offered their time and help!

Resources