首页 文章

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

提问于
浏览
1

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

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

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

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 回答

  • 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
    }
    

相关问题