为什么我的 bot dm 用户不能收到欢迎消息? (Discord.net C#)

Why can't my bot dm users a welcome message? (Discord.net C#)

过去几天我一直在开发一个 discord 机器人,我设法让其中一个功能正常工作:(一个设置消息的命令,它应该在用户加入时发送给 dm 用户)。我似乎无法让机器人发送实际消息。

    private async Task Join(SocketGuildUser UID)
    {
        if (UID.IsBot || UID.IsWebhook) return;
         Welcometxt= File.ReadAllText([FILE]);
        await UID.SendMessageAsync("Your Message Was Sucessfully set!");
    }
        private async Task HandleCommandAsync(SocketMessage arg)
    {
        var message = arg as SocketUserMessage;
        var context = new SocketCommandContext(_client, message);
        if (message.Author.IsBot) return;

        int argPos = 0;
        if (message.HasStringPrefix("!", ref argPos))
        {
            var result = await _commands.ExecuteAsync(context, argPos, _services);
            if (!result.IsSuccess) Console.WriteLine(result.ErrorReason);
        }
    }

当我检查日志时,它给出了 var 消息和上下文的空引用错误,我尝试使用谷歌搜索错误并更改代码,但没有任何建议?我相信错误出在这两种方法中的一种,但我不是 100% 肯定的

我想你可以使用 UserJoined 事件来实现这一点。

为 UserJoined 定义事件处理程序

public async Task UserJoined(SocketGuildUser user)
{
   await user.SendMessageAsync("Hello");
}

注册

private readonly DiscordSocketClient _client = new DiscordSocketClient();
private readonly CommandService _commandService = new CommandService();

public async Task MainAsync()
{
   ....
   _client.UserJoined += UserJoined;
   ....
}

我不确定这是否相关,但请检查服务器成员意图是否打开