U
U
Unchpokable2022-01-17 02:15:32
C++ / C#
Unchpokable, 2022-01-17 02:15:32

Discord.NET Bot completely ignores it when calling IMessageChannel.SendFileAsync. What to do?

I'm making a bot for my video game Discord server. One of its tasks is to "Approve" a user's post, which is created as a message of the format "Text" + Attached .mp3 file, by sending a copy of this message to a dedicated channel. For this I use the following code:

[Command("approve")]
        public async Task Approve([Remainder] ulong id)
        {
            var sourceMsg = await Context.Channel.GetMessageAsync(id);
            if (sourceMsg == null)
                return;

            var targetChannel = _client.GetChannel(ulong.Parse(_config["rankedSongsId"])) as IMessageChannel;
            if (sourceMsg.Attachments.Count > 0)
            {
                var httpClient = new HttpClient();
                var file = await httpClient.GetByteArrayAsync(sourceMsg.Attachments.ElementAt(0).Url).ConfigureAwait(true);
                using (System.IO.MemoryStream stream = new System.IO.MemoryStream(file))
                {
                    var fileAtt = new FileAttachment(stream, sourceMsg.Attachments.ElementAt(0).Filename);
                    await targetChannel.SendFileAsync(fileAtt, text: sourceMsg.Content);
                }
                return;
            }
            await Context.Channel.SendMessageAsync("Can not approve offer without attached songfile").ConfigureAwait(true);
        }

The problem is that when the execution reaches the line "await targetChannel.SendFileAsync(fileAtt, text: sourceMsg.Content);", the execution just stops and then nothing happens in the context of this method.
For the sake of experiment, I removed the await operator, in which case this line is simply ignored and execution continues.

In Google there is nothing even approximately similar to this problem, plus everything is complicated by the fact that no error occurs, and nothing is written in the logs.
The bot has all the necessary access rights and privileges, the necessary channels on the server are configured so that the bot has full permissiveness on them.

I thought that maybe the problem is that I am pre-downloading the attached file through HttpClient, and I tried to send the local file from the disk instead of the attached one, but in this case the problem is the same.

I also tried to change the method of obtaining the channel I needed, use other overloads of the SendFileAsync method, but in all cases the same unknown nothing happened.
Adding .ConfigureAwait(true) also made absolutely no difference.

With all this, the bot itself does not freeze and calmly continues to respond to other commands

I use Discord.NET v3.1.0, .NET Core 5.0

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question