Okay so I know I was on here earlier but now I messed something up trying to add another thing. Basically, I am getting an undefined error in the console because I am not sure how to write this in code for Discord.JS
Code:
const id = message.id.members.first();
Console Error:
(node:16412) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'first' of undefined
at Object.execute (C:\Users\Saman\Desktop\gban-bot\commands\test.js:12:39)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:16412) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16412) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.```
You seem a little confused as to what you want to do. There are two things that are in a message that contain some form of member.
The member who send the message is always present. You can to access it, and the corresponding ID, like this:
let memberID = message.member.id;
Then there is the member who got tagged in the message. Obviously that one isn't always present so you may need to add additional checks for that. You can access it like this:
let memberID = message.mentions.members.first().id;
message.id.members.first() isn't a thing. message.id returns a snowflake .i.e. the id of the message that was sent. message.member.id also returns a snowflake/the id of the messaged member. In your case, if you want to get the member object itself, from a mention that is in a message,then you are looking for mentions. So it's message.mentions.members.first() and would return the GuildMember object itself. Using this, you can access the properties of GuildMember.
EDIT:
Well what I am looking for is a way to ban by ID and I know this is a simple command but for some reason I can't figure out how to make it ban by ID and not a mention.
In order to ban them by id, first you need to find the GuildMember. There are 3 ways by which you can find a member. find() and get() using the collection of GuildMemberManager. Or you can use fetch() using the GuildMemberManager directly (if the member is not in the collection). By doing this, you will get the GuildMember object. And you can ban him, by doing <GuildMember>.ban()
get() and fetch() requires a Snowflake. i.e. an ID itself as a string. Whereas you need to provide a function inside find(). I'll link you to the djs documents for these methods if you still have any doubts.
Related
I am trying to get a bot to respond to people with a ping in the message, for example: "#user", but everything I've tried have given me not a function error or undefined error. All the stuff I can find about it is either outdated for discord.js v14 or it is for discord.py
Here is my code:
client.on("messageCreate", (message) => {
if (message.content.startsWith("test")) {
const user = message.author.userId();
message.channel.reply(`Hello <#${user}>`)
}
});
I've also attempted variations of the .userId() part - such as .tag, .user.id, and .username but all of them have come back with some sort of undefined error. I know it says userId is a snowflake on discord.js but I am unsure on how to use that for I am fairly new to javascript and discord.js. Also, please know that I am using Replit to host the bot and have discord.js#14.5.0 installed.
So, there are a couple issues here.
Issue 1
message.author.userId() is not a function. When trying to get a user's ID, you want to know what each property is returning.
message -> returns the Message object, which contains the data for the message.
message.author -> returns the User object, which contains the data for the user profile.
message.member -> returns the Member object, which contains the data for the guild member profile.
And so on.
In this case, you're going to want to get the User object: being message.author. You've already figured this out.
Now, the User Object has it's own set of properties (which you can find in the documentation at https://discord.js.org/).
The property that you are looking for is: .id, or in your use-case, message.author.id.
Your other attempts, message.author.tag returns the tag, message.author.user.id will throw an error, and message.author.username returns the user's username.
Issue 2
Your second issue is the .reply() method that you're using. The Channel Object doesn't have a .reply() method, but the message does.
So, how you would write this code:
client.on("messageCreate", async(message) => {
if(message.content.toLowerCase().startsWith("test")) {
// Note that I added the .toLowerCase() method to ensure you can do tEST and it still works!
message.reply({ content: `Hello, ${message.author}!` });
}
});
Also, a cool feature that discord.js has is that you can supply the User or Member Object to the content of a message, and it will automatically mention the desired person.
Hope this helps!
I'm relatively new to discord.js, and I've started building a bot project that allows a user to create a message via command, have that message stored in a hidden channel on my private server, and then said message can be extracted through the message ID.
I have the write working and it returns the message ID of the message sent in the hidden channel, but I'm completely stumped on the get command. I've tried searching around online but every method I tried would return errors like "Cannot read property 'fetch' of undefined" or "'channel' is not defined". Here are some examples of what I tried, any help would be appreciated. Note that my args is already accurate, and "args[0]" is the first argument after the command. "COMMAND_CHANNEL" is the channel where the command is being executed while "MESSAGE_DATABASE" is the channel where the targeted message is stored.
let msgValue = channel.messages.cache.get(args[0])
client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content)
let msgValue = msg.channel.message.fetch(args[0])
.then(message => client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content))
.catch(console.error);
I even tried using node-fetch to call the discord API itself
const api = require("node-fetch")
let msgValue = api(`https://discordapp.com/api/v8/channels/${MESSAGE_DATABASE}/messages/${args[0]}`)
.then(message => client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content))
.catch(console.error);
Am I missing something or am I making some sort of mistake?
Edit: Thanks for the help! I finished my bot, it's just a little experimental bot that allows you to create secret messages that can only be viewed through their ID upon executing the command :get_secret_message <message_id>. I posted it on top.gg but it hasn't been approved yet, so in the meantime if anyone wants to mess around with it here is the link: https://discord.com/api/oauth2/authorize?client_id=800368784484466698&permissions=76800&scope=bot
List of commands:
:write_secret_message - Write a secret message, upon execution the bot will DM you the message ID.
:get_secret_message <message_id> - Get a secret message by its ID, upon execution the bot will DM you the message content.
:invite - Get the bot invite link.
NOTE: Your DMs must be turned on or the bot won't be able to DM any of the info.
My test message ID: 800372849155637290
fetch returns the result as promise so you need to use the then to access that value instead of assigning it to a variable (msgValue). Also you made a typo (channel.message -> channel.messages).
I would recommend using something like this:
msg.channel.messages
.fetch(args[0])
.then(message => {
client.channels
.fetch(COMMAND_CHANNEL)
.then(channel => channel.send(message.content))
.catch(console.error)
})
.catch(console.error)
I think you were quite close with the second attempt you posted, but you made one typo and the way you store the fetched message is off.
The typo is you wrote msg.channel.message.fetch(args[0]) but it should be msg.channel.messages.fetch(args[0]) (the typo being the missing s after message). See the messages property of a TextChannel.
Secondly, but this is only a guess really as I can't be sure since you didn't provide much of your code, when you try to fetch the message, you are doing so from the wrong channel. You are trying to fetch the message with a given ID from in the channel the command was executed from (the msg.channel). Unless this command was executed from the "MESSAGE_DATABASE" channel, you would need to fetch the message by ID from the "MESSAGE_DATABASE" channel instead of the msg.channel.
Thirdly, if you fetch a message, the response from the Promise can be used in the .then method. You tried to assign the response to a variable msgValue with let msgValue = msg.channel.message.fetch(args[0]) but that won't do what you'll expect it to do. This will actual assign the entire Promise to the variable. What I think you want to do is just use the respone from the Promise directly in the .then method.
So taking all that, please look at the snippet of code below, with inspiration taken from the MessageManager .fetch examples. Give it a try and see if it works.
// We no longer need to store the value of the fetch in a variable since that won't work as you expect it would.
// Also we're now fetching the message from the MESSAGE_DATABASE channel.
client.channels.cache.get(MESSAGE_DATABASE).fetch(args[0])
// The fetched message will be given as a parameter to the .then method.
.then(fetchedMessage => client.channels.cache.get(COMMAND_CHANNEL).send(fetchedMessage.content))
.catch(console.error);
The title's pretty much everything. I have tried .addField(`**Nickname (If Applicable):**`, `${usr.nickname}`) but it says undefined in the embed... For the presence though, I've tried .addField(`**Status:**`, `${usr.presence}`). That gives me [object Object] in the embed. Can anyone help?
First of all, you're not able to get a GuildMember object's status but would have to first turn it into a user object.
This can be easily done as follows:
const memberObject = <guild>.members.cache.get('place id here') // Gives you the member object
// or alternatively you could search for him using his name using the .find() function
const userObject = memberObject.user // Gives you the user object of the member
From there on, we could simply check the user's nickname and status using:
console.log(userObject.presence.status)
console.log(memberObject.nickname)
Hopefully this helps!
We have configured REST proxy service that accepts JSON input. If the input is not a well formed JSON OSB is throwing Translation error with HTTP 500 Staus code. Is that possible we can send Customized error message in this scenario
You need to create a global error handler for your pipeline and set the desired error message using a replace action here, followed by a "Reply" action.
Keep in mind that if you try to "read" the original request body in the global error handler, and if the original request was malformed, it will get thrown up to the system error handler and you will get the system error message again.
Here's a sample OSB 12.2.1.1 project you can use to try this: https://github.com/jvsingh/SOATestingWithCitrus/tree/develop/OSB/Samples/ServiceBusApplication1
The accompanying soapui project contains two requests. The malformed request should return this:
(I have only set the response here. You would also need to set the proper content type and decide whether you want to treat this as "success" or "failure" etc. in the reply action)
In the Gmail API quite frequently drafts.list will return a draft id
but when I pass it to drafts.get it returns a 404 error. This is
repeatable for certain draft ids, I can call drafts.list again and
they're still there and I can then call drafts.get and get the same
error again. I can also call messages.get with the given message id
and get a response as expected and I can see the draft in the gmail client.
Seems to be a bug in the GMail API. Any ideas for workarounds? And does anyone know the right way to report bugs to Google?
I'm getting the same issue, here is how to reproduce:
1) Start writing a new message in the Gmail web client. Don't add anything in the To field or the Body. Then close the message to save it as a Draft.
2) Retrieve a list of threads with the following code:
threads = gmail_client.users().threads().list(userId='me', maxResults=15, pageToken=pageToken, q='-in:(chats OR draft) in:all').execute()
4) Then retrieve each of these threads in a batch request:
batch.add(gmail_client.users().threads().get(userId='me', id=thread['id'], format='metadata', metadataHeaders=['subject','date','to','from']), callback=threadCallback)
The error that gets returned is:
https://www.googleapis.com/gmail/v1/users/me/threads/154e44a4c80ec7e4?format=metadata&metadataHeaders=subject&metadataHeaders=date&metadataHeaders=to&metadataHeaders=from&alt=json returned "Not Found">
In order to use drafts.get, you should use immutable id of the draft, not the message id of the draft which will change after each update in draft. Drafts.list supplies both immutable id and the message id of the messages in the draft.