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

问题描述 投票:1回答:1

我正在尝试为我的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:");
    }
}
c# bots discord discord.net
1个回答
1
投票

我能够弄清楚这一点。在主程序.s

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
}
© www.soinside.com 2019 - 2024. All rights reserved.