How to make the bot send personalized emojis? - javascript

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.

Related

Discord.js | Multiple channel permission - Everyone & created role

I would like to create a channel with specific permissions for #everyone and a created role.
So I defined the role as " rolee " and the #everyone at " everyoneRole ", I would like #everyone not to have permission to see the room, and the rolee created before to have permission to see, talk, log in and talk in the room (category type room, but permission is the same).
My code will surely be more concrete: by the way, discord.js is a bit tricky because it only accepts the code if everything is perfectly aligned, which in itself is not a problem, but is when the script doesn't work because of a single misplaced space.
let everyoneRole = message.guild.roles.cache.find(role => role.name === '#everyone');
let rolee = message.guild.roles.cache.find(role => role.name === rolename);
let crole = (
message.guild.roles.create({
data: {
name: rolename,
color: '3498DB',
},
});
);
let cat = (
message.guild.channels.create(category, {
type: "category",
position: 999,
permissionOverwrites: [
{
id: everyoneRole.id,
deny: [
'VIEW_CHANNEL',
'SEND_MESSAGES',
],
},
]
});
);
And here is the error message.
My problem is that it refuses to add multiple permission to a single role (allow : ['VIEW_CHANNEL', 'SEND_MESSAGES']).
Note: This answer is for Discord.js v13.3.0
The TypeError is occurring because when you're getting the role from the list of roles, #everyone is technically not a role. While it is a role, you can't get it by requesting it from the role cache.
// When you try to get it from this, Discord.js returns null
let everyoneRole = message.guild.roles.cache.find(role => role.name === '#everyone');
This means that you're handing Discord an empty ID which then causes the issue. The proper way to get the role for everyone, as pointed out by Niet the Dark Absol, is to use <Guild>.roles.cache.everyone (Replace <Guild> with whatever your Guild variable is). Here's what that would look like.
let everyoneRole = message.guild.roles.cache.everyone

(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.'
}
}});
}

Disord js bot user-info issue

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

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)

Bot doesn't add message member to the channel - Discord.JS

So, I'm currently working on a "temporary channels" module for my bot. When a user with a certain rank does !newvc, the bot creates a private voice channel that they can use, add people, and when everyone leaves, it gets automatically deleted after some time.
Everything was working fine, but I've noticed a bug that I can't find the reason behind why it happens. Basically, when you first use the command all works fine, the channel is made, you get added and it's moved to the category. But if you use it again, let's say a minute later you don't get added. The channel is made, set as private but you message.member doesn't get added. Then it again does and doesn't, You get the point right?
I honestly can't find a reason why it does it and the only thing I can think of is something to do with Discord's API.
Here's my code
let member = message.member
user = member.user
message.delete()
message.guild.createChannel(`⭐${member.user.username}'s Room`, 'voice', [{
id: message.guild.id,
deny: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']
}]).then(channel => {
channel.overwritePermissions(member, {
CONNECT: true,
USE_VAD: true,
PRIORITY_SPEAKER: true
})
channel.setParent('567718414454358026')
})
let privatevc = new Discord.RichEmbed()
.setDescription(':white_check_mark: Successfully created a voice channel!')
.setColor(config.green)
message.channel.send({ embed: privatevc }).then(msg => msg.delete(10000))
FYI: My Discord.JS version is 11.4 (didn't had time to update it, due to work)
Firstly, the first 2 lines should be changed to:
let member = message.member,
user = message.author;
// or
const { member, author: user } = message;
as whilst this isn't a problem, in strict mode it will cause an error as you technically do not have a variable keyword in front of user = member.user. You should try and use const if you aren't going to be changing the value of the variables. Note that message.author is the same as message.member.user.
Secondly, using the permissionOverwrites arg in Guild#createChannel is deprecated (see https://discord.js.org/#/docs/main/v11/class/Guild?scrollTo=createChannel). I am aware that Discord.JS has abolished many things despite them saying "deprecated". Try using the typeOrOptions argument to create the channel with the appropriate overrides instead.
Here's my suggested code:
(async () => {
message.delete();
message.guild.createChannel(`⭐ ${message.author.username}'s Room`, {
type: 'voice',
parent: '567718414454358026',
permissionOverwrites: [{
id: message.guild.id, // #everyone has the ID of the guild
deny: ['VIEW_CHANNEL', 'CONNECT'],
}, {
id: message.author.id, // attach the permission overrides for the user directly here
allow: ['VIEW_CHANNEL', 'CONNECT', 'USE_VAD', 'PRIORITY_SPEAKER']
}]
});
const embed = new Discord.RichEmbed()
.setDescription(':white_check_mark: Successfully created a voice channel!')
.setColor(config.green);
const sentMessage = await message.channel.send(embed);
sentMessage.delete(10 * 1000);
})();
I found the issue. Basically, because the user was added after the channel is created, Discord API was losing it (or something, these are only my guess atm).
After changing it to this:
message.guild.createChannel(`⭐${member.user.username}'s Room`, 'voice', [{
id: message.guild.id,
deny: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']
}, {
id: message.author.id,
allow: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']
}])
Everything works again. Thank you PiggyPlex.

Categories