我是新来的,所以不要评判我,我想创建一个命令 !poke 向指定用户发送直接消息 (PM(,但我似乎找不到一种方法将 userid 提供给它,并且文档几乎不存在 discord.net。
commands.CreateCommand("poke")
.Parameter("target")
.Do(async (e) =>
{
ulong userID = e.User.Id;
Console.WriteLine("[" + e.Server.Name + "]" + e.User.Name + " just poked " + e.GetArg("target"));
await e.User.SendMessage("HEY, wake up! ");
});
有几种方法可以做到这一点,首先是使用命令创建专用频道:
var c = discord.CreatePrivateChannel(ulong userid);
并从中发送消息,如下所示:
await c.SendMessage("blabla");
另一种方法是将用户存储为对象,然后从中发送消息。
User u = null;
string findUser = e.GetArg("target");
u = e.Message.MentionedUsers.FirstOrDefault(); //checking mentioned users
u = e.Server.FindUsers(findUser).FirstOrDefault(); //looking for specified user in server
await u.SendMessage("HEY, wake up! ");
我的命令如下所示:
commands.CreateCommand("poke")
.Parameter("target", ParameterType.Required)
.Do(async (e) =>
{
ulong id;
User u = null;
string findUser = e.Args[0];
if (!string.IsNullOrWhiteSpace(findUser))
{
if (e.Message.MentionedUsers.Count() == 1)
u = e.Message.MentionedUsers.FirstOrDefault();
else if (e.Server.FindUsers(findUser).Any())
u = e.Server.FindUsers(findUser).FirstOrDefault();
else if (ulong.TryParse(findUser, out id))
u = e.Server.GetUser(id);
}
Console.WriteLine("[" + e.Server.Name + "]" + e.User.Name + " just poked " + u);
await u.SendMessage("HEY, wake up! ");
});
我想我迟到了,但我希望这能有所帮助。这对我的机器人有用。
[Command("Test")]
public async Task Test(string message)
{
if (message== "Send me a message")
{
await Context.User.SendMessageAsync("Here is your DM message! ;)");
}else
{
await Context.Channel.SendMessageAsync("I can't send you a message");
}
}
或者,
var u = Context.Message.Author; // Or some other means of getting user_id
string msg = "your message";
await Discord.UserExtensions.SendMessageAsync(u, msg);