Discord.Net 无命令的 SendMessageAsync

Discord.Net SendMessageAsync without command

我正在制作一个程序,大部分时间无需有人坐在 PC 前即可运行。我认为如果发生需要手动完成的事情时,如果该程序可以在 Discord 上向您发送 PM,那就太好了。所以用户在配置中设置他的名字和#number,如果发生什么事,程序会私信这个人。所以我设置了一切,只要 Discordbot 使用 MessageRecieved-Event,它就会工作。我尝试使用 Ready-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;
}

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

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 开发者门户上启用 "SERVER MEMBERS INTENT"。您可以在“机器人”部分下找到它。