discord bot c#读取收到的直接消息

时间:2017-12-16 20:57:35

标签: c# bots discord discord.net

我正在尝试为我的Discord服务器创建自己的机器人。我希望用户能够键入命令**事件,然后机器人将向用户发送消息并向其询问事件的一些问题,例如标题,时间等。

我能够让机器人向用户发送消息,但我找不到如何让机器人读取用户发回的消息。

到目前为止,这是我的代码:

public class Event : ModuleBase<SocketCommandContext> {

    private static IUser currentUser;
    private DiscordSocketClient _client;

    [Command("event")]
    public async Task EventAsync() {
        _client = new DiscordSocketClient();

        var id = Context.User.Mention;

        if(currentUser == null) {
            foreach(var user in Context.Guild.Users) {
                if(("<@!" + user.Id.ToString() + ">") == id) {
                    currentUser = user;
                    id = user.Mention;
                    break;
                }
            }
        }

        await currentUser.SendMessageAsync("Enter event title:");
    }
}

1 个答案:

答案 0 :(得分:1)

我能够弄清楚这一点。在主program.cs中

static void Main(string[] args)
    => new Program().RunBotAsync().GetAwaiter().GetResult();

public async Task RunBotAsync() {
    _client.MessageReceived += MessageReceived;
}

private async Task MessageReceived(SocketMessage msg) {
    //Code to direct message here
}