首页 > 解决方案 > 如何进行不和谐机器人登录并阅读特定服务器的消息以在 Windows 窗体应用程序中使用?

问题描述

我制作了一个 Windows 窗体应用程序,我想在其中显示程序的最新更新,使用不和谐机器人阅读文本频道中的最新消息。

我也无法运行该函数,因为我之前没有使用过asyncTask使用过函数。

这是我的代码;

public async Task AnnounceAsync()
{
     string discordToken = Convert.ToString(123123123123123);
     await _discord.LoginAsync(TokenType.Bot, discordToken);
     await _discord.StartAsync();
     var chnl = _discord.GetChannel(123123123123123) as IMessageChannel;

     var message = chnl.GetMessageAsync(0, CacheMode.AllowDownload, RequestOptions.Default);
     Console.WriteLine(message);
}

如果有人能指出我正确的方向,我将不胜感激!

标签: c#discord.net

解决方案


创建一个类:

public class DiscordServiceOptions
{
    public string BotToken { get; set; }
}

public class DiscordService : IDiscordService
{
    private readonly ILogger _logger;
    private readonly DiscordSocketClient _client;
    private TaskCompletionSource<bool> _ready = new TaskCompletionSource<bool>();

    public DiscordService(IOptions<DiscordServiceOptions> options, ILogger<DiscordService> logger)
    {
        _logger = logger;
        _client = new DiscordSocketClient();
        _client.Log += LogDiscord;
        _client.Ready += OnReady;
        _client.LoginAsync(TokenType.Bot, options.Value.BotToken);
        _client.StartAsync();
    }

    public SocketTextChannel GetSocketTextChannel(ulong channelId) => _client.GetChannel(channelId) as SocketTextChannel;

    public async Task<bool> Ready()
    {
        await _ready.Task;
        return _ready.Task.Result;
    }

    private Task OnReady()
    {
        _ready.SetResult(true);
        return Task.CompletedTask;
    }

    private Task LogDiscord(LogMessage msg)
    {
        _logger.LogInformation(msg.ToString());
        return Task.CompletedTask;
    }
}

创建接口:

interface IDiscordService
{
    public Task<bool> Ready();
    public SocketTextChannel GetSocketTextChannel(ulong channelId);
}

将其添加到您的程序中:

class Program
{
    private static ServiceProvider _services;

    static async Task Main(string[] args)
    {
        _services = new ServiceCollection()
            .AddOptions()
            .Configure<DiscordServiceOptions>(options =>
            {
                options.BotToken = "Your bot token";
            })
            .AddSingleton<IDiscordService, DiscordService>()
            .AddLogging(logging =>
            {
                logging.SetMinimumLevel(LogLevel.Trace);
                logging.AddNLog(new NLogProviderOptions
                {
                    CaptureMessageTemplates = true,
                    CaptureMessageProperties = true
                });
            })
            .BuildServiceProvider();

        Task discordReady = _services.GetService<IDiscordService>()
            .Ready();

        await discordReady;

        // maybe call other methods here?
        // write a discord message?
        var channel = _services.GetService<IDiscordService>()
            .GetSocketTextChannel(discordChannelId);

        await channel.SendMessageAsync("Hello World!")

        await Task.Delay(-1);
    }
}

推荐阅读