discord bot c# 读取收到的直接消息
discord bot c# read received direct message
我目前正在尝试为我的 Discord 服务器创建我自己的机器人。我希望用户能够键入命令 **event,然后机器人会直接向该用户发送消息,并向它询问有关事件的一些问题,例如标题、时间等。
我可以让机器人直接向用户发送消息,但我找不到如何让机器人读取用户发回的消息。
到目前为止,这是我的代码:
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:");
}
}
我想通了。主要 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
}
我目前正在尝试为我的 Discord 服务器创建我自己的机器人。我希望用户能够键入命令 **event,然后机器人会直接向该用户发送消息,并向它询问有关事件的一些问题,例如标题、时间等。
我可以让机器人直接向用户发送消息,但我找不到如何让机器人读取用户发回的消息。
到目前为止,这是我的代码:
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:");
}
}
我想通了。主要 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
}