Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attaching embeds alongside a file upload. #978

Closed
wants to merge 4 commits into from
Closed

Allow attaching embeds alongside a file upload. #978

wants to merge 4 commits into from

Conversation

WamWooWam
Copy link
Contributor

Abstract

This PR adds support for attaching embeds alongside file uploads, hopefully fixing #796.

Additions

Added an Embed parameter to IMessageChannel#SendFileAsync and modified the rest of the code handling this to allow it, as such it should work now in most cases.

Drawbacks

This is, naturally, a breaking change, which may cause issues for people passing RequestOptions to these methods, but shouldn't effect anyone else.

God damn Discord is a mess.
Didn't actually need to change this, whoops.
@@ -29,6 +37,17 @@ public UploadFileParams(Stream file)
d["tts"] = IsTTS.Value.ToString();
if (Nonce.IsSpecified)
d["nonce"] = Nonce.Value;
if (Embed.IsSpecified)
{
var sb = new StringBuilder(256);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any cleaner approach here? The DefaultRestClient seems to support serializing Streams here, could use a MemoryStream+StreamWriter instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, the embed data needs to be URL encoded, something I'm not sure I can do from a MemoryStream without even more mess


namespace Discord.API.Rest
{
internal class UploadFileParams
{
// Fuckin Discord man.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd replace the sentimental comments with something more informational, like "needed to serialize embeds" or something

using (TextWriter text = new StringWriter(sb, CultureInfo.InvariantCulture))
using (JsonWriter writer = new JsonTextWriter(text))
{
// I apologise for the mess I've created here, god damn is this annoying.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't apologize, that's what git blame is for

using (JsonWriter writer = new JsonTextWriter(text))
{
// I apologise for the mess I've created here, god damn is this annoying.
_serializer.Serialize(writer, new Dictionary<string, object>() { { "embed", Embed.Value } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we generally prefer Dictionary-initializer style (["key"] = "value")

foxbot added a commit that referenced this pull request Mar 13, 2018
commit a8bafb9
Merge: f38dd4c 7e04285
Author: WamWooWam <wamwoowam@gmail.com>
Date:   Mon Mar 12 08:05:52 2018 +0000

    Merge branch 'dev' of https://github.com/WamWooWam/Discord.Net into dev

commit f38dd4c
Author: WamWooWam <wamwoowam@gmail.com>
Date:   Mon Mar 12 08:05:49 2018 +0000

    Cleaned up & fixed code style.

commit 7e04285
Author: Thomas May <wamwoowam@gmail.com>
Date:   Sun Mar 11 14:11:28 2018 +0000

    Revert changes to DefaultRestClient

    Didn't actually need to change this, whoops.

commit 3f5b2c8
Author: WamWooWam <wamwoowam@gmail.com>
Date:   Sat Mar 10 19:30:44 2018 +0000

    Enabled embeds alongside uploaded files.

    God damn Discord is a mess.

Co-authored-by: WamWooWam <wamwoowam@gmail.com>
@foxbot
Copy link
Member

foxbot commented Mar 13, 2018

Merged via e9f9b48, thanks!

@foxbot foxbot closed this Mar 13, 2018
FiniteReality pushed a commit to FiniteReality/Discord.Net that referenced this pull request May 5, 2018
commit a8bafb9
Merge: f38dd4c 7e04285
Author: WamWooWam <wamwoowam@gmail.com>
Date:   Mon Mar 12 08:05:52 2018 +0000

    Merge branch 'dev' of https://github.com/WamWooWam/Discord.Net into dev

commit f38dd4c
Author: WamWooWam <wamwoowam@gmail.com>
Date:   Mon Mar 12 08:05:49 2018 +0000

    Cleaned up & fixed code style.

commit 7e04285
Author: Thomas May <wamwoowam@gmail.com>
Date:   Sun Mar 11 14:11:28 2018 +0000

    Revert changes to DefaultRestClient

    Didn't actually need to change this, whoops.

commit 3f5b2c8
Author: WamWooWam <wamwoowam@gmail.com>
Date:   Sat Mar 10 19:30:44 2018 +0000

    Enabled embeds alongside uploaded files.

    God damn Discord is a mess.

Co-authored-by: WamWooWam <wamwoowam@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants