Search code examples
c#filediscord.net

Discord.net: Is it possible to send multiple files at once?


I am using Discord.net to log images in a channel on a Discord server. Most of the time I need to send a couple of images (4 or 5 on average) at once.

Since the Discord Desktop app allows you to send up to 10 files at once I was wondering if this can also be done using C#.

Currently, I am sending one message per image.

The code looks like this:

DiscordSocketClient client = new DiscordSocketClient();
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();

When the Bot has started, this code runs:

DiscordSocketClient channel = client.GetChannel(channelId) as SocketTextChannel;

Finally, to send a file I run this:

channel.SendFileAsync(filepath, message); // Both filepath and message are strings.

This works, but when it sends, for example, five images, five messages are sent on discord. It would be nice if I could send one message that has all of those images attached. (Just like in the Desktop app)

I know that I could use an Embed to send two images at once, but that way I would still be limited to two images per message. (One as Embed-image and one as Embed-thumbnail)
This is still not close to the 10 files that are possible in the app and, in addition to that, the images would not be displayed equally.

Another option would be to create a new, bigger image that combines all of the images I want to send. Then I could send this single image by saving it as a .png and then using the method that i have already implemented.

The problem with this is that this is a workaround, rather than a real solution to the problem, as I dont really like the idea of not having the images uploaded as individual files.

Tl dr:
Is there a way to send multiple files or images in a single message using Discord.net?

Thanks in advance!


Solution

  • You can use SendFilesAsync! You must update to the latest version of Discord.net, when installing via Nuget make sure you uninstall all Discord.net extensions such as Discord.Net.Commands before updating.

    List<FileAttachment> filestosend = new List<FileAttachment>();
    filestosend.Add(new FileAttachment(@"c:\file.png", "file.png", "file description"));
    filestosend.Add(new FileAttachment(@"c:\file2.png", "file2.png", "file description"));
    await channel.SendFilesAsync(filestosend, "text");
    

    Documentation can be found here

    Edit for clarity: do not confuse SendFileAsync with SendFilesAsync