if (message.content === '!registrar') {
// pegue o nome do usuário digitado após o comando "!registrar"
var newName = message.content.split(' ')[1];
console.log(newName);
}
The idea is to capture what the user types after the !register and store it in the newName variable and display it in the console for testing, but it is not storing it and it also does not print any results in the console, nor does it print the message "undefined", it seems to me that the split is not working, however when I put only message.content without the split also it does not store what was typed by the user.
I tried to store in the variable only what the user typed without using split and it still didn't store it, I also tried to just show that the command is working after trying to store the information in the variable but it also doesn't go to the next line, it doesn't display no errors in the console, it just doesn't display what the user typed after the "!register"
In your if condition you check if the message equals '!registrar'. It won't work with a command like '!registrar Name'.
You need to check if the entered String starts with the given command prefix.
So you have to change it like that:
const registerCmd = '!registrar';
// check if message starts with register command
if(message.content.startsWith(registerCmd)) {
// get name from message
let args = message.content.slice(registerCmd.length).trim().split(/ +/g);
let newName = args.shift().toLowerCase();
// print name
console.log(newName);
}
For more information check the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith?retiredLocale=en
Related
In Discord.js, I am passing a client into the following:
async function get_remindmes(client) {
const outstanding_remindmes = await Remindmes.get_outstanding();
for (let x of outstanding_remindmes) {
let user = client.users.cache.find(y => String(y.username)+'#'+String(y.discriminator) === x.username);
}
return;
}
I can console.log the outstanding_remindmes array from within this function, and client.users.cache.find seems to work in other ways from within this function, but I can't seem to get user here to be anything other than undefined.
I assume the x variable is an object with a username and discriminator, but you're only using x.username to compare to y's username and discriminator. Should you be comparing the strings like this?
let user = client.users.cache.find(y => (y.username + "#" + y.discriminator) === (x.username + "#" + x.discriminator))
However, I'm not sure about the circumstances but it's much easier to compare users by using the user id (and it's better practice - users can change their names and even discriminators, but id will always be the same). Actually, if you have the user ids you can simply use the get function on client.users.cache, because the cache is a collection where the keys of all the elements are the user ids. For example:
let user = client.users.cache.get(x.id);
All you need to do is store the user ids in each of the entries of the outstanding_remindmes object.
I'm throwing together a poll command for my discord bot, and need emojis as options. However, when I try to react with default emojis I got from the message, it fails. I won't get into too many specifics, but here is almost all the relevant code:
// This just takes the <prefix> poll off then splits it by newlines
const args = message.content.slice(message.content.indexOf("poll")+"poll ".length).split("\n");
const fields = [];
const reactions = [];
// First arg is title
for(let i = 1; i < args.length; i++){
const splits = args[i].split("-"); // Split each arg by -
fields.push(splits[0] + ": " + splits[1]); // Field title
fields.push(splits[2]); // Field description
reactions.push(splits[0]); // This should be an emoji
}
// This function takes in message, embed title, [field title, field description, ...]
const msg = await embedGenerator.sendNormalEmbed(message, args[0], "", fields);
// React
for(let i = 0; i < reactions.length; i++){
await msg.react(reactions[i])
}
This works perfectly fine with custom emojis, however, it fails with default Unicode emojis:
The second poll gives me this error:
After some looking the put request makes a call that should react, however, the ID of the emoji is the Unicode code for 🚁. Is there any way to get a normal emoji instead of this Unicode code from the text, and if not is there a way to force discord to react with the Unicode code?
I was bit confused in your question, So maybe I am not correct. if it is, so please comment and ping me. You can also ask me for other help.
Reason:-
So discord does not use id for reacting of default emojis which are already in discord. If you want to implement ":helicopter:" as reaction, You need to replace it with this "🚁".
Solution:-
You can still solve it by telling user to insert "\" before ":helicopter:" so that emoji becomes unicode or there's an npm package for that, this.
Also you can refer to this site to copy and paste them or this for help on emojis.
Heyo! Tried everything but can't figure this out. I have a command where you mention an user and from that mention will bot take his ID. But the ID looks like mention (<#!id>) and I need replace that mention into ID only. Here's code.
const mentionUser = message.mentions.users.first();
if(args[0] == mentionUser) {
let object = mentionUser.id;
id = object.replace('#', '');
} else {
id = args[0];
}
console.log('ID:', id);
I can't figure out if its value, object or array? So I tried every single function to convert it into a string so I can replace certain letters. String(), stringify, regexp etc... Nothing seem to be working.
Console return:
ID: <#!191243099123482625>
Well first of all, your if condition is not even being met. You're doing if (args[0] == mentionUser) and that is never true. As you can see from your console return, args[0] looks like: <#!id>. Whereas this is what mentionUser looks like as a String: <#id>. The exclamation point in the former will make it never match up with the latter, even if they are the same mention.
The exclamation point actually indicates that this is a guild member mention (meaning the user was mentioned in a guild). So the code you have currently would be very close to working if the message were a DM message instead of a guild message.
What you need to do is you need to check if the mentionUser even exists (in the event that no user was mentioned at all), and if it does exist you simply need to do mentionUser.id to get the user's ID. Replacing is only necessary to ensure that the first arg is a mention, by removing the exclamation point from the first arg. Here's an example:
const mentionUser = message.mentions.users.first();
if(mentionUser && args[0].replace('!', '').match(mentionUser)) {
id = mentionUser.id;
} else {
//Mention was not in the first arg, treat first arg as an ID instead
id = args[0];
}
console.log('ID:', id);
I am trying to get my discord bot to remove a role from a user, but I am having trouble. When I try to run the command it gives the error: TypeError: Cannot read property 'removeRole' of undefined, and I don't know how to fix it, as verify[1] is a user.
Code:
if (msg.content.toLowerCase().startsWith('!accept')) {
let verify = msg.content.split(", ");
let person = verify[1]; //Person is an # like #Llama's Tester#7151
person.member.removeRole('744427032783355965');
person.send(accept);
};
Update: as mentioned by #Lioness100
You can use message.mentions.users.first() – Lioness100 1 hour ago
I am not sure if your code is correct.
Firstly, you are splitting message (string): let verify = msg.content.split(", ");
After that you are accessing 2nd item in the array of strings:
let person = verify[1];
Now you are accessing member on string: person.member.removeRole('744427032783355965');
definitely you are going to get this error as string does not have member property.
My code is as follows:
async run(message, args)
{
if(message.content.split(" ").length === 3 && message.content.split(" ")
[0] == '!suspend' && message.content.split(" ")[1].startsWith("<") &&
message.content.split(" ")[1].endsWith(">"))
{
var nab = message.content.split(" ")[1]; // 2nd item in array when text is split by spaces, (ex. `#user`)
var nabwoo = message.content.split(" ")[2]; //3rd item in array when the text is split by spaces, (ex. `12h`)
let role = message.guild.roles.find(r => r.name === "Suspended");
//nab is the users id
let member = message.mentions.members.first();
member.addRole(role);
message.channel.sendMessage('Done.');
}
}
My code is supposed to apply a role called 'Suspended' to someone after a message like !suspend #user blank. Everything works great, except I'm unsure how to remove a role after a certain amount of time. There is the method member.removeRole(role) but how would I do this automatically after a specified amount of time? I want there to be a 3 command input. !suspend #person timeInHours (with or without units). As I said, I want it done automatically so a user doesn't have to remove the role with a command. I also want to be able to input other commands (basically suspend other people) while the bot is counting the time until the unsuspension. How would I go about doing this? An idea I have is to perhaps check every minute if it's been > x number of minutes since the suspension.
I'm not completely sure as I'm new to this as well, BUT since you delete a message after sometime using .then(msg => {msg.delete(600)}) I would assume, you could do something similar, like replace the msg.delete(600), with a method to get rid of roles sorry if this wasn't useful I was just suggesting a way you could take this.