Edit:
I figured it out, you could find another bot's embeds's author field
message.embeds[0].author.name
Original question:
To be clear about 'author', this is the 'author' I'm mentioning.
I already wrote a regex to detect if an embed's author include [username] searched the [place]:
const matches = message.embeds[0].author.match(new RegExp("\\*\\*<#!?\\d{1,}> searched the:\\*\\*"));
but there's an error:
TypeError: cannot read property 'author' of undefined
Would appreciate some help, thank you!
As the error message suggests, undefined has no property author. This means embeds[0] is undefined. It is quite likely the message sent has no embeds, so you should probably put that line inside if(typeof message.embeds[0] !== 'undefined') { to stop the code running if there is no embed.
Related
I'm working on reactJs with laravel CHAT. I want to display juste the last message from the sender. But I usually get all his messages. I tried lot of attempts But I didn't get the solution yet. this is my last attempt:
$types= chat::select('id','senderId')->where('userId', $id)->get();
foreach ($types as $chats) {
$chats->chat = chat::select('id','unseenMsgs', 'senderId')->where('senderId', $chats->senderId)
->get();
foreach ($chats->chat as $child){
$child->lastMessage = chat::select('userId','message', 'time', 'senderId')
->where('id', $child->id)
->orderBy('id', 'asc')->skip(0)->take(1)
->get();
}
}
return ['chatsContacts' => $types];
The userId is the receiver one and senderId is the one who send the message. At first query I tried to get all the messages that are sent to the reciever. The question is how to ignore the duplication and get juste the last message from the sender one. and Thanks in advance for your help
Ps: I'm using An MVP and the reponse should be like this:
this picture is for the response in the MVP
That's why in my server side I should create an api which return exactly this response
It is hard to recreate the issue with the amount of information provided.
From what I understand about the problem description, following seems like a possible solution:
$latestChats = chat::select('id', 'userId', 'message', 'time', 'senderId')
->where('userId', $id)
->orderBy('id', 'desc')
->get()
->unique('senderId');
return ['chatsContacts' => $latestChats];
This should give you a collection containing the last messages sent by every senderId to the specified userId.
If this does not resolve your issue please provide further information.
What does the table look like?
What is the exact format you're looking for in chatContacts ?
So, I'm trying to make a serverInfo command as you can see below
let embed = new Discord.MessageEmbed()
.setColor("GREEN")
.setTitle("Server Information")
.setDescription(`Server Name: **${message.guild.name}** \n ────────────────── \n Member Count: **${message.guild.memberCount}** \n ────────────────── \n Server ID: **${message.guild.id}** \n ──────────────────`)
.setTimestamp()
.setFooter(`Ran by: ${message.author.username.id}`)
message.channel.send(embed)
For my result, I get "undefiened"
anyone know the solution to this? (.setFooter)
message.author.tag for get the user with tag (JohnDoe#0000)
message.author.user for get the user
message.author.user.username for get the Username
message.author.user.id for get the ID
Simple (:
To get the complete tag of a user, you can just use .tag after message.author.
In your code, you're trying to get the username but you put .id after it so this is why you get "undefined".
The ID isn't the numbers with the hashtag, it's the user ID and the tag is the username plus the numbers with the hashtag.
⠀⠀⠀↱ John Doe#3040 ↰
Username⠀⠀⠀⠀ ⠀⠀Numbers
↳⠀⠀⠀⠀⠀⠀⠀⠀Tag⠀⠀⠀⠀⠀⠀⠀ ↲
So, to get the username and tag, just do this:
//say it’s called msg instead of message
var tag = msg.author.tag;
var username = msg.author.id;
//tag would return the user's tag, and as someone else stated in a comment in a previous answer, author returns a user, which itself doesn't have a user property because it is the user object
Also just a quick tip: since it’s server info command, you might want to put some information about the user that’s exclusive to that guild (nickname, roles, permissions), and for that, you can use msg.member which returns a GuildMember, which has a user property, and many more, like member.displayName and member.roles
Error:
TypeError: Cannot read property 'roles' of null
at Object.process (/home/bots/fortnite-bot/commands/stats/stats.js:19:33)
Line (stats.js):
let platform = message.member.roles.find(x => (x.id === psnRoleId) || (x.id === xboxRoleId))
Help please....
Since Discord.JS v12 came out, instead of message.member.roles.find(), you must instead use message.member.roles.cache.find(). The function works in exactly the same way in the context you're using it, you just need to add the .cache in there.
I want to change the permissions of a role, but I keep getting an error saying TypeError: Cannot read property 'setPermissions' of undefined. As far as I can tell, all of my syntax and logic are fine:
let role = message.guild.roles.cache.find(role => role.name === "name");
role.setPermissions(["SEND_MESSAGES"])
Any suggestions?
Check & ensure that role is not undefined.
If role were a proper role object, this should go all as initially planned given you're using the appropriate version of discord.js.
Regarding reasons why role would be undefined, it seems there was a failure in finding a role that has a name property equal to "name".
I'm fairly new to node.js and I'm working on a discord bot with discord.js, I'm am trying to do assigned roles with commands. When I do the code and type in the command it works successfully but pops up with "DeprecationWarning: Collection#find: pass a function instead" in the console, how can I get rid of this?
https://i.imgur.com/agKFNsF.png
This warning is caused by the following line:
var role = message.guild.roles.find('name', 'Epic Gamer');
On an earlier version of Discord.js, this would be valid, but they have now redone the find function. Instead of taking in a property and a value, you pass in a filtering function instead. This should work:
var role = message.guild.roles.find(role => role.name === "Epic Gamer")
Instead of passing in 'name' (the property), and 'Epic Gamer' (the value we want to search for/filter out), we pass in the arrow function role => role.name === 'Epic Gamer'. This is like mapping. find passes every role from message.guild.roles into the function as role, and then checks if the property we want equals the value we want.
If you would like to learn more about the find function, please check out the official documentation.
Pass a predicate function in find method, take a look at the discord.js document on the find function.
Change the find statement to
var role = message.guild.roles.find(role => role.name === 'Epic Gamer');
Hope this will help!