Discord.js User Presence Customize - javascript

When I use a discord bot command, for the purpose of showing the specific person's discord presence, let's say his presence is set on 'Do Not Disturb' a.k.a dnd.
I want this line :
**• Discord Username:** ${user.username},
lets say that specific person is on online, I want it to say 'Online' with a capital 'O', but it shows it as 'online'. Or the same thing for 'dnd', i want it as 'Do Not Disturb'.
I've defined user as : message.mentions.users.first() || message.author
Any ways to help?

const Presence = {
"online": "Online",
"dnd": "Do Not Disturb",
"idle": "Idle",
"offline": "Offline"
}
client.on("message", message => {
if (message.author.bot) return false;
if (message.content.toLowerCase() == "-presence") {
const User = message.mentions.users.first() || message.author;
message.channel.send(Presence[User.presence.status]);
};
});

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

How can i add Error Message in Discord.js?

I Create a Truth and dare Bot. My Prefix is + Now I want to add an error message to it. I have two variables "t" "d" If anyone types +something which does not match my variable to send a Message that "Invalid Command +help for Help" Can you guys help me?
const Discord = require('discord.js');
const client = new Discord.Client();
const keepAlive = require("./server")
const prefix = "+";
// ======== Ready Log ========
client.on ("ready", () => {
console.log('The Bot Is Ready!');
client.user.setPresence({
status: 'ONLINE', // Can Be ONLINE, DND, IDLE, INVISBLE
activity: {
name: 'Truth Or Dare | +help',
type: 'PLAYING', // Can Be WHATCHING, LISTENING
}
})
});
// ======== Code ========
client.on('message', message => {
const help = new Discord.MessageEmbed()
.setColor('#72dfa3')
.setTitle(`Truth Or Dare`)
.addFields(
{ name: '``+help``', value: 'For help'},
{ name: '``+t``', value: 'For Truth'},
{ name: '``+d``', value: 'For Your Dare'},
{ name: '``Created By``', value: 'AlpHa Coder [Labib Khan]'},
)
.setTimestamp()
.setFooter(`${message.author.username}`, message.author.displayAvatarURL());
if (message.content === `${prefix}help`) {
message.channel.send(help);
}
});
// ========= Truth =========
client.on('message', message => {
const t = [
"If you could be invisible, what is the first thing you would do?",
"What's the strangest dream you've ever had?",
"What are the top three things you look for in a boyfriend/girlfriend?",
"What is your worst habit?",
"How many stuffed animals do you own?",
"What is your biggest insecurity?"
]
const truth = t[Math.floor(Math.random() * t.length)];
if (message.content === `${prefix}t`) {
message.channel.send(truth);
}
});
// ========= Dare =========
client.on('message', message => {
const d = [
"Do a free-style rap for the next minute.",
"Let another person post a status on your behalf.",
"Hand over your phone to another player who can send a single text saying anything they want to anyone they want.",
"Let the other players go through your phone for one minute.",
"Smell another player's armpit",
"Smell another player's barefoot.",
"Tell everyone your honest opinion of the person who sent this command."
]
const dare = d[Math.floor(Math.random() * d.length)];
if (message.content === `${prefix}d`) {
message.channel.send(dare);
}
});
const token = process.env.TOKEN;
keepAlive()
client.login(token);
Please explain clearly so that I can understand. Advance Thank you
Don't use separate message event handlers, use one. You can take advantage of that by using if else chain. You are trying to match the command through the chain, if no match was found, in else (meaning every previous check in the chain failed) you reply to the user by saying:
"Invalid command, type +help for help.".
Also check for the prefix at the beginning. If there is no prefix, return from the function. That way you don't have to write it to the if statements when matching the message content.
// Array of possible truth replies
const t = [
"If you could be invisible, what is the first thing you would do?",
"What's the strangest dream you've ever had?",
"What are the top three things you look for in a boyfriend/girlfriend?",
"What is your worst habit?",
"How many stuffed animals do you own?",
"What is your biggest insecurity?"
];
// Array of possible dare replies
const d = [
"Do a free-style rap for the next minute.",
"Let another person post a status on your behalf.",
"Hand over your phone to another player who can send a single text saying anything they want to anyone they want.",
"Let the other players go through your phone for one minute.",
"Smell another player's armpit",
"Smell another player's barefoot.",
"Tell everyone your honest opinion of the person who sent this command."
];
// Handle all commands here
client.on('message', message => {
// Don't reply to itself
if (message.author.id === client.user.id) return;
// If there is no + (prefix) at the beginning of the message, exit function
if (!message.content.startsWith(prefix)) return;
// Remove the prefix from the message -> our command
const command = message.content.substring(prefix.length);
// Match the command
if (command === "t") { // Truth
const truth = t[Math.floor(Math.random() * t.length)];
message.channel.send(truth);
} else if (command === "d") { // Dare
const dare = d[Math.floor(Math.random() * d.length)];
message.channel.send(dare);
} else if (command === "help") { // Help
const help = new Discord.MessageEmbed()
.setColor('#72dfa3')
.setTitle(`Truth Or Dare`)
.addFields(
{ name: '``+help``', value: 'For help' },
{ name: '``+t``', value: 'For Truth' },
{ name: '``+d``', value: 'For Your Dare' },
{ name: '``Created By``', value: 'AlpHa Coder [Labib Khan]' },
)
.setTimestamp()
.setFooter(`${message.author.username}`, message.author.displayAvatarURL());
message.channel.send(help);
} else { // No match found, invalid command
message.channel.send("Invalid command, type `+help` for help.");
}
});

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

Give roles permission to interact with newly created channel Discord.JS

I'm using Node.JS to create a discord bot in which a new role and channel are created with the same command. For the sake of simplicity, both the name of the role and the name of the channel are the first argument of the command:
let chanName = args[0];
let roleName = args[0];
Following the name of the role/channel is a list of mentions - these are the users that receive the new role. The role is created just fine, the channel is created just fine, and the members are added to the role just fine, but I'm having trouble setting up permissions where only users with the new role OR the 'administrator' role are able to see the channel.
The channel is created like so:
message.guild.channels.create(chanName, {
type: "text"
})
.then((channel) => {
//Each new channel is created under the same category
const categoryID = '18-digit-category-id';
channel.setParent(categoryID);
})
.catch(console.error);
It appears as though the channel is not added to the guild cache
let chan = message.guild.channels.cache.find(channel => channel.name === chanName);
because this code returns 'undefined' to the console.
Is there any way I can allow the mentioned users to interact (view, send messages, add reactions...) with this new channel?
Permissions are very simple to set up upon creating a channel!
Thanks to the multiple options GuildChannelManager#create() gives us, we're very simply able to add permission overwrites to our desired roles and members.
We could very simply take your code and add a permissionOverwrites section to set the channel's permissions, as follows:
message.guild.channels.create(chanName, {
type: "text",
permissionOverwrites: [
// keep in mind permissionOverwrites work off id's!
{
id: role.id, // "role" obviously resembles the role object you've just created
allow: ['VIEW_CHANNEL']
},
{
id: administratorRole.id, // "administratorRole" obviously resembles your Administrator Role object
allow: ['VIEW_CHANNEL']
},
{
id: message.guild.id,
deny: ['VIEW_CHANNEL']
}
]
})
.then((channel) => {
//Each new channel is created under the same category
const categoryID = '18-digit-category-id';
channel.setParent(categoryID);
})

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