简体   繁体   English

Discord 机器人不发送图像

[英]Discord bot doesn't send an image

I want my bot to send an image from given url with caption got from String(), but with SendFileAsync() it totally doesn't work.我希望我的机器人从给定的 url 发送带有从 String() 获得的标题的图像,但是使用 SendFileAsync() 它完全不起作用。 Is there another method for sending images from internet or I just screwed up something in here?是否有另一种从互联网发送图像的方法,或者我只是在这里搞砸了一些东西?

public class Task : ModuleBase
        {
            public Task()
            {
                Thread.Sleep(6000);
                IReadOnlyCollection<SocketGuild> guilds = CommandHandler._discord.Guilds;
                foreach (var guild in guilds)
                {
                    JobManager.AddJob(() => CommandHandler._discord.GetGuild(guild.Id).DefaultChannel.SendFileAsync("HERE_GOES_URL", String()), (s) => s.ToRunEvery(5).Seconds());
                }
            }
        }
        static void Initializer()
        {
            JobManager.Initialize(new MyRegistry());
        }

To use SendFileAsync , you need to have the file locally.要使用SendFileAsync ,您需要在本地拥有该文件。 If you want to send a URL without downloading, just use SendMessageAsync .如果您想发送 URL 而不下载,只需使用SendMessageAsync Discord will preview the image from the URL (unless the user disabled that manually). Discord 将预览来自 URL 的图像(除非用户手动禁用)。

Otherwise, this would help:否则,这将有所帮助:

using System.Net;
using System.IO;
...
byte[] buffer = (new WebClient().DownloadData("myurl"));
using (MemoryStream ms = new MemoryStream(buffer))
{
   await SendFileAsync(ms, ...);
}

Wrapping this to a function is what I'd recommend.我建议将其包装到 function 上。

If you are still convinced about not dealing with downloads, check out EmbedBuilder .如果您仍然确信不处理下载,请查看EmbedBuilder

Notes not related to your question与您的问题无关的注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM