discord.io
discord.io copied to clipboard
Find nickname of a user?
I'm trying to figure out how to get the nickname of a user from one of his messages (getMessages() method).
I get an author, then from that I can get an ID. I would assume that it would be easy with channelID and userID to get the member, then the nickname, but I'm stuck.
bot.servers[bot.channels[channelID].guild_id].members[userID].id
is equal to userID, so I would assume that I got the member object correctly. But when I try to get the following property, as seen in the documentation:
bot.servers[bot.channels[channelID].guild_id].members[userID].nick
I get null.
I know for a fact that this specific user has a nickname in the current server, since the name I see is different from his username:
In this case, I get PinkPanther in the username of the author, but I can't get "iMpACT" anywhere.
What version are you using? I just tested it myself and it worked perfectly fine.
I'm on version 2.5.1
Would you show me the code you used to test it?
I think it would be better if you showed us your code than to ask for us to show our code.
This is most likely a user-caused error, unless you can provide the code you are using to test this please close this issue.
} else if (message.startsWith("s ")) {
bot.deleteMessage({
channelID: channelID,
messageID: evt['d']['id']
}, function(error) {
if (error != null) {
logger.info('error deleting message: ' + error);
}
var parts = message.split(" ");
var numMessages = 0;
var user = '';
if (parts.length == 3) {
numMessages = parseInt(parts[2]);
}
if (parts.length >= 2) {
user = parts[1].toLowerCase();
}
logger.info('user: ' + user);
logger.info('numMessages: ' + numMessages);
bot.getMessages({
channelID: channelID,
limit: 20
}, function(error, response) {
for (var i in response) {
var chaine = response[i]['content'];
logger.info(response[i]['author'].id + ': ' + response[i]['author'].username)
var authorUsername = response[i]['author'].username.toLowerCase();
var authorNick = '';
for (var m in bot.channels[channelID].members) {
logger.info(m + ': ' + bot.channels[channelID].members[m] + ': ' + bot.channels[channelID].members[m].id + ': ' + bot.channels[channelID].members[m].nick);
if (bot.channels[channelID].members[m].id == response[i]['author'].id) {
authorNick = bot.channels[channelID].members[m].nick.toLowerCase();
}
}
logger.info("Analysing message: " + chaine + '\n Sent by: ' + authorUsername + ' (' + authorNick + ')');
if ((authorUsername == user || authorNick == user || user == '') && numMessages > -10) {
numMessages--;
logger.info("This is the right author, numMessages: " + numMessages);
if (numMessages <= 0 && numMessages > -10) {
logger.info('On envoie');
doSarcasm(channelID, evt['d']['id'], chaine)
numMessages = -30;
}
}
}
})
});
}
Sorry if some of the code is in french, it should be pretty easy to understand either way.
This works well (finding the nth message starting from the most recent sent by the person put in the parameters, default 1st) if we use their actual name, but not at all for their nickname. I always compare to null (because the nick is null), even for people I am 100% sure are using a nickname.
I noticed the same issue. I printed every property of a member and they don't match the api documentation.
My code is:
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
...
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
...
var channels = bot.channels;
for (var c in channels) {
var channel = channels[c];
for (var m in channel.members) {
var member = channel.members[m];
var propVal;
for (var prop in member) {
propVal = member[prop];
logger.info(prop + ' : ' + propVal);
}
logger.info('\n member.nick: ' + member.nick);
}
}
Output:
user_id : 115587852824191495
suppress : false
session_id : b2e99622ab73a409b937449ee97dd5f3
self_video : false
self_mute : false
self_deaf : false
mute : false
deaf : false
channel_id : 134557049374729152
member.nick : undefined
How the posters above are able to get nick is beyond me. I tried version 2.5.3 and version 2.5.1.