Discord.Net SendMessageAsync 没有命令

问题描述

我正在制作一个程序,该程序大部分时间都可以在没有人坐在 PC 前的情况下运行。如果发生需要手动完成的事情,如果该程序可以在 discord 上向您发送 PM,我认为会很好。所以用户在配置中设置他的名字和#number,如果发生什么事,程序会通知这个人。所以我设置了一切,只要 discordbot 使用 MessageRecieved-Event 就可以工作。我尝试使用就绪事件,但这不起作用。

我的“开始”-代码

public async Task startdiscordBot(string discordName,string discordID,string discordMessage)
{
    _client = new discordSocketClient();

    setdiscordID(discordID); // Sets the #Number of the User
    setdiscordMessage(discordMessage); // Sets the Message to be send
    setdiscordName(discordName); // Sets the Name of the User

    await _client.LoginAsync(TokenType.Bot,Token);
    await _client.StartAsync();

    await Task.Delay(-1);
}

编辑:我目前的功能是这样的:

public Task Notification() // not working
{
    discordSocketClient _client = new discordSocketClient();

    IUser user = _client.GetUser(Name,ID); // Get the User <--- Returns null
    MessageBox.Show($"{user}"); // For debug
    IDMChannel channel = (IDMChannel)user.GetorCreateDMChannelAsync(); // Get a DM Channel
    channel.SendMessageAsync(Message); // Send a DM

    return Task.CompletedTask;
}

解决方法

我找到了 Anwser。首先感谢您的帮助。我能够大大缩减我的代码。

public async Task startDiscordBot()
{
    await _client.LoginAsync(TokenType.Bot,Token);
    await _client.StartAsync();
}

public async Task Notification(string message) // Working
{
    IUser user = _client.GetUser(Name,ID); // Get the User (wait atleast 10sec after starting)
    var channel = await user.GetOrCreateDMChannelAsync(); // Get/Create a DM Channel
    await channel.SendMessageAsync(message); // Send a DM
}

这行不通……好吧,每个解决方案都行不通,因为您需要在 Discord Developer Portal 上启用“SERVER MEMBERS INTENT”。您可以在 Bot 部分下找到它。