Luca | LeCarbonator
Explore posts from servers✅ Binding Nested values in RequestBody to Model
Consider the following model I use inside the MVC structure:
However, when responding to requests, this is instead formatted as
application/hal+json
, which would look like this:
As you can see, the property Url
has been moved into _links:external:href
.
Now I would like to receive a Collection in a [HttpPut]
request, but I'm having trouble figuring out how to properly parse the incoming application/hal+json
to my Model.
I've read online that Model Binding is probably the solution to this, but the vast majority of examples and explanations I could find were centered around Routes / Query parameters, not RequestBody formatting.
The part I did find mentioned ValueProviders, but I couldn't find out how to obtain the JSON data from the body and store it as keys there.
Any suggestions are appreciated, as I'm rather new to MVC architecture and C# in general.5 replies
Specifying 4xx Details
I have a
POST
request that may cause a duplicate entry if it were to execute. As a response, I send back 409 Conflict
to make clear what happened.
However, I have some trouble adding some detail to explain what exactly happened.
First, I tried the following, which is not RFC 9110 compliant because it returns a simple text/plain
:
return Conflict("A string explaining which property is already reserved");
When only returning Conflict();
, the response body is application/problem+json
as expected:
How can I add the "detail"
property when returning Conflict()
without altering the return type?8 replies
✅ Is it possible to access a Controller's URI at runtime?
I'm not well versed in MVC structures, so this very well could be a XY problem. Let me know if it is a bad idea to begin with.
I'm trying to create a GET endpoint for
/collections
which returns Collections. One Collection
always contains a nested Model User
.
On its own, this is not a problem. However, I am creating my API based around application/hal+json
, which requires Resources to have a link to point to their exact location.
So when accessing a returned Collection
, I could use its related link if I wish to access it directly instead.
It looks sort of like this:
Endpoints:
/collections
/collections/{uuid}
This is all well and good, but I have some concerns with generating the embedded collection's link.
If I hardcode it as $"http://foo-api/collections/{uuid}"
, then I would have to change the domain in every location where I did it.
Is it possible to access the CollectionController#GetById(string uuid)
somehow and extract its URI at runtime?20 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 6/26/2023 in #djs-questions
awaitMessageComponent breaks if separate message is deleted
I have the following code that awaits either a message, a string select menu or a button:
I also have the following
while
loop that determines what was chosen:
The response should either be parsed as String Select Menu, Button, or a Message that was sent in the same channel.
If it's a message, it should be deleted and the content
stored.
When first starting it up, it works fine. I can change the button / select menu as often as I want.
When sending a message, it gets properly parsed the first time. However, anything after that returns the following error:
Does channel.awaitMessages()
conflict with message.awaitMessageComponent()
?9 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 2/8/2023 in #djs-questions
InteractionCollectorError ignores try/catch block
3 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 10/31/2022 in #djs-questions
createMessageCollector doesn't work, but only in one guild
15 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 10/30/2022 in #djs-questions
Calling promise function twice causes error
I have the following function that should await either a message from the user, or await a button click.
The first time works as intended. Either button clicks or message inputs cause the customId or message content to be available and used.
However, if I try to call the function twice, I get this error:
The code has stopped working ever since implementing this function, so I think it's likely I am missing some problem with it.
However, it's also possible that it's not the function that causes the problem and it's something else instead.
I'd appreciate it if someone else could look at the function and tell me if there's any problems with the execution.
The message variable is the following:
11 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 10/30/2022 in #djs-questions
Is there a way to simultaneously use awaitMessages and awaitMessageComponent?
I'm currently using a do while loop to modify entries in a list. If the user enters "done", it exits that loop. However, I am curious if there's a way to perform that action with a button instead. So the process would be as follows:
- Display list
- Await message and append entry to the list
- ... but if a button is pressed instead, exit the loop.
Up until now, I have worked exclusively with the two methods mentioned in the title. The problem with collector.on listeners is that they do not work in a while loop, since they can't be awaited.
If collector.on listeners are the way to go, I'd like to know if it's possible to place them in a promise.
10 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 10/29/2022 in #djs-questions
awaitMessages filter firing off even though it returns false
11 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 10/27/2022 in #djs-questions
awaitMessageComponent 'Interaction has already been acknowledged' Error
13 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 10/27/2022 in #djs-questions
Client or message undefined
I'm currently trying to code a bot that does the following:
- Awaits slash command, executes the corresponding command file
- File displays entered data in an embed
- User presses a button either to edit provided or add missing info.
The problem is that (as far as I understood), you need a collector to handle it.
The guide lists the following command:
const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, time: 15000 });
However, I'm not sure what to replace the message object with in this case. Replacing it with interaction
seems to cause an error.
A couple hours ago, I had the same problem with client, where I wanted to use the bot's profile picture in the slash command, but it claimed that client isn't defined
.
The answer I received back then was that "client's available even in command files", but it doesn't seem to be the case.
How can I access
- the bot itself
- the bot's embed message for button interactions
from a commands/foo.js
file?15 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 10/27/2022 in #djs-questions
Accessing .addUserOption Pfp and bot pfp
I'm currently working alongside the online guide, and I've come across an issue I couldn't find.
The current project structure contains a folder "commands", in which each file represents a command. I'm working on one file and try to obtain a target user's profile picture, as well as the bot's profile picture to display in an Embed.
While
interaction.user.displayAvatarURL()
works as expected, I can't seem to figure out how to get the same URL from interaction.options.getUser("user")
. Additionally, I don't know where I can locate the bot's profile picture to begin with. I assumed it's in the client object, but how do I access that from a separate file?
Thanks in advance.11 replies
DIAdiscord.js - Imagine an app
•Created by Luca | LeCarbonator on 10/27/2022 in #djs-questions
Is it possible for an embed field to span multiple rows?
11 replies