Disord js bot user-info issue - javascript

I'm trying to create a bot, and one of its commands is user-info
for eg. !user-info #<username>
and i want it to display username, id and the avatar
like:
username:<username>
Id:<User Id>
Avatar:<the avatar >
Below is the code i used:
else if (message.content.startsWith(`${prefix}user-info`)) {
var member = message.mentions.users.first();
message.channel.send(`Username: ${member.username}\n ID:${member.id}\n Avatar:${member.displayAvatarURL()}` );
}
However it doesn't work, when i remove the avatar part the output comes out as :
Username:undefined
Id:<the id>
When I add the avatar part I just get a huge error on the command module when I use the bot command. What's the right way and what did I get wrong?

I'd suggest you use an Embed for this, as those can display images in a better way, the code for your request would be:
var user = message.mentions.users.first();
message.channel.send({
embed: {
title: `Profile of ${user.tag}`,
thumbnail: {
url: user.displayAvatarURL(),
},
fields: [
{
title: "ID:",
value: user.id
}
]
}
});
You can find more on this here

Related

(Discord.JS) How do I listen for a user mention for a specific user chosen by the author

So I am in the process of making a Discord.Js bot that includes a command that will let me provide information on certain users. For example: I want to add a command that will provide the PlayStation gamer tag of a mentioned user (lets say the specific users id is <#123>). The input message would look something like this :
"!psn #mention" then the bot would output his gamertag which I will manually log as--> message.channel.send('Here is <#1235467890> 's #psnname');
I want to include the gamertag every member in my server so anyone can request it upon mentioning it with the command "psn", I have gone through tons of trial and error with different code but i can not figure out how to specify the message.mention.members.first(); by a specific user id. Please help
module.exports = {
name: 'codtag',
execute(message, args){
let member = message.mentions.members.first();
if(!args.length){
return message.channel.send({embed: {
color: '#da1801',
title: 'Activision Gamertag: Error',
description: 'You need to tag a user dummy.'
}})
}
if (member !== '<#772597378142306354>')return;
else if (member === `772597378142306354`)return
{
(args[0] === member)
return message.channel.send({embed: {
color: '#1243c6',
title: 'Activision Gamertag',
description: 'Here is <#772597378142306354> Activision: \n\n **WalterWhite#2396124**'
}});
}}
}
For anyone that finds this post with the same question, I figured it out. The following code works perfectly
I added:
let guild = message.mentions.members.first();
I also included the condition for args[0] as:
if (message.mentions.members.had('put users id here without the <#>')
module.exports = {
name: 'cod',
execute(message, args){
let guild = message.mentions.members.first();
if(!args.length){
return message.channel.send({embed: {
color: '#da1801',
title: 'Activision Gamertag: Error',
description: 'You need to tag a valid user dummy.'
}})
}
if(message.mentions.members.has('772597378142306354')){
(args[0] == guild)
message.channel.send({embed: {
color: '#1243c6',
title: 'Activision Gamertag',
description: 'Here is <#772597378142306354> Activision: \n\n **WalterWhite#2396124**',
footer: {
text: 'Message #issmayo if your gamertag is not included.'
}
}});
}

How to create an embed for statistical information provided by my Discord bot (Covid Statistics)

I've created a Discord bot that will give me Covid statistics on any country on command, however it's only displayed to me in raw text, I've seen images of embeds like this:
I'm interested to have my data displayed like this in my bots reply, this is the code I am using for it:
const axios = require('axios');
const countries = require("./countries.json");
const url = 'https://api.covid19api.com/total/country/';
const WAKE_COMMAND = 'cases';
client.on('message', async (msg) => {
const content = msg.content.split(/[ ,]+/);
if(content[0] === WAKE_COMMAND){
if(content.length > 2){
msg.reply("Too many arguments...")
}
else if(content.length === 1){
msg.reply("Not enough arguments")
}
else if(!countries[content[1]]){
msg.reply("Wrong country format")
}
else{
const slug = content[1]
const payload = await axios.get(`${url}${slug}`)
const covidData = payload.data.pop();
msg.reply(`Confirmed: ${covidData.Confirmed}, Deaths: ${covidData.Deaths}, Recovered: ${covidData.Recovered}, Active: ${covidData.Active} `)
}
}
});
Any help on how I should rearrange my code to look more like the embed above would be much appreciated.
Thanks!
You can do this using the MessageEmbed class.
Here an example of it being used:
const embed = new Discord.MessageEmbed()
.setColor("#0099ff")
.setTitle("A title")
.setDescription("A description")
.setTimestamp()
message.channel.send(embed);
// You can also use the code below, in your case
msg.reply(embed);
You can find more examples of this here
If you want to create something like ProDyno has, you'll want to use the .addLine method on the MessageEmbed class, this will allow you to toggle things like inline to be true so you can put stats next to each other. For example:
.addFields(
{ name: 'Inline title', value: 'Inline text', inline: true },
{ name: 'Inline title', value: 'Inline text', inline: true },
)

How to make the bot send personalized emojis?

Well, I'm currently using the emoji :x:, but on my server I have an emoji called :superbotxemoji: I just don't know how I get my bot to use it
My code:
const Discord = require('discord.js');
module.exports = {
name: 'say',
description: 'say',
execute(message, args) {
const { prefix, token } = require('../config.json');
if (!message.member.hasPermission('ADMINISTRATOR'))
return message.channel.send({
embed: {
color: 16777201,
description: `:x: | ${message.author}, You are not allowed to use this command.`,
footer: {
text: ` | Required permission: ADMINISTRATOR`,
},
},
});
if (!args.length)
return message.channel.send({
embed: {
color: 16777201,
description: `:x: | ${message.author}, You need to put a message.`,
footer: {
text: ` | Example: !say hello`,
},
},
});
const sayMessage = args.join(' ');
message.delete({ timeout: 1 });
message.channel.send(sayMessage);
},
};
There is actually a very detailed explanation from the official discord.js guide which you can find here, although I'll try to paraphrase it.
To send a custom emoji, you must get that emoji's unique ID. To find that, you must send the emote in discord with a backslash in front of it; essentially escaping the emoji.
This will result in the emojis unique ID in this format: <:emoji-name:emoji-id>
If you paste this special string into a message, the bot will send the emoji. However, the emoji must be from a guild the bot is part of.
On the other hand, there's another very easy way to get an emoji using the client.emojis.cache collection and the .find() method.
client.emojis.cache.find(emoji => emoji.name === '<name of emoji>')
This method will also make it possible to send custom emojis, however, this time you can find them by name. Be careful, if there are more than one emojis by the given name, it will not work.
A way to bypass this problem would be looking at a guild.emojis.cache collection. This way the amount of possible duplicate emojis would be narrowed down.

How to make a list of a "#role"

I want to make a command where, if someone types "?role #testrole", the bot will send an embed with a list of users who have the aforementioned role.
An example response would be:
List of #testrole
- #Adam
- #Drago
- #Santiago
Here's my code:
if (message.content.toLowerCase() == 'teamone') {
let team1 = message.guild.member(
message.member.roles.cache.has('750579457953234994')
);
message.channel.send({
embed: {
title: 'Team 1 composition!',
description: `${team1}`,
},
});
}
I tried but it does not work, it only sends null. Any ideas?
Try this:
if (message.content.toLowerCase() == 'teamone') {
let team1 = message.guild.roles.cache
.get('750579457953234994') // get the role in question
.members.map((member) => `<#${member.id}>`) // map the results in mention format (<#${user.id}>)
.join('\n'); // join by a line break
message.channel.send({
embed: {
title: 'Team 1 composition!',
description: `${team1}`,
},
});
}
Every Role has a members property, which is a collection of all (cached) members that have the given role.
You can then use the Collection.prototype.map function to format each member to a mention. Here's an example from my server:
(I used the mod role as an example, this is my mod team)

how to initiate one-to-one chat with getstream.io?

I'm trying to create slack like application using getstream.io chat SDK. In the documentation we can find out, how to initiate a channel to start group chat. But there is no any information about one-to-one chat. Does anyone knows how to initiate a one-to-one chat?
code sample to create new channel for group chat
const client = new StreamChat('t5v25xyujjjq');
await client.setUser(
{
id: 'jlahey',
name: 'Jim Lahey',
image: 'https://i.imgur.com/fR9Jz14.png',
},
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiamxhaGV5In0.sFe9WvuHIKgQmgtEWFh708Nu_a2wy8Zj3Q8BJz7fDRY',
);
const channel = chatClient.channel('messaging', 'groupname', {
name: 'Founder Chat',
image: 'profile image url',
members: ['user1', 'user2'],
});
const response = await channel.sendMessage({
text: 'Josh I told them I was pesca-pescatarian'
});
If you want to have the guarantee that only one channel exists between N users you can instantiate the Channel without ID and the list of its members.
When you do that, the API will ensure that only one channel with the list of members exists (order of the members does not matter).
const distinctChannel = conversation.channel("messaging","", {
members: [user1.id, user2.id],
});
await distinctChannel.create();
Since the channel data is going to be the same for both members; I suggest to not store the image field the way you do in your code example. It is easier to use the "other" member profile image as the channel image when you render the conversation.
For example:
let channelImage = "https://path/to/fallback/image";
otherMembers = channel.members.filter(member => member.user.id != currentUser.id);
if otherMembers.length > 0 {
channelImage = otherMembers[0].image;
}
Check the docs securely, it's in there:
check under channel initialization
const conversation = authClient.channel('messaging', 'thierry-tommaso-1', {
name: 'Founder Chat',
image: 'bit.ly/2O35mws',
members: ['thierry', 'tommaso'],
});

Categories