Fetching guild id and other properties when guild create event is fired
I am trying to console.log and then eventually write to a db the ID of the guild the bot has just joined. I am unsure how exactly to get the id of the guild as I try to create a new Guild object but I don't know what to pass into the constructor when it comes to data. My code is below with the error.
15 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by OPHere is my code
I know passing in args does nothing, but I don't know what to pass in
(responding here because you crossposted in the main support channel)
You should use your model to create a new guild document in your database, and follow mongoose guides for how to do so (if you're using mongoose, we can't even tell)
You should not use the discord.js Guild class to construct a new discord.js Guild instance, but that is what you are currently doing for some reason
I recommend you use a linter to warn you of unused variables to call this out in the future since you import your DB model, yet you do not use it anywhere in your code
Thanks for the quick response squid
I am using mongoose but i removed the code to just test if I can even get the guild id
Here is the full code
I am able to successfully create a document and everything its just that the guild.id returns undefined and then it errors out
the schema is predefined in a separate file
You have no reason to construct a new Guild instance; the discord.js Guild constructor is only used internally to create an instance from API data, and you have no reason to be doing that
If you just want the guild that the bot has joined, I assume that would be
const guild = args;
(or just rename args
to guild
since that's all the GuildCreate event emits with anyways)
Also, you should not include once: true
since I assume you want to listen for this event multiple times instead of just the first time after your bot has startedSo something like this?
PS: I recommend using
findOneAndUpdate()
with upsert: true, new: true
and $setOnInsert
to find a document if it exists (using the id as the query), and if it doesn't exist, insert a new document with the remaining fields
That looks better but try it and see
I see you removed your client
parameter, so make sure your event listener doesn't pass in the client as the first argumentThe order of function parameters must match between definition and function call.
- mismatch! you pass an interaction where the client is expected
- mismatch! you pass the client where an interaction is expected
Oh alright, I am not as experienced as you so I am unaware of these command but I will be sure to use them
I will run it and let you know
Thanks Squid for all your help
Succesfully wrote it
oh i should probably remove that
anyways it was able to get all the ids
thanks again