Discord.net 向指定用户发送私信(PM)
Discord.net sending direct message(PM) to specified user
我是新来的所以不要评判我,
我想创建一个命令 !poke 向指定用户发送直接消息 (PM),但我似乎无法找到向其提供用户 ID 的方法,而且 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! ");
});
或者,
var u = Context.Message.Author; // Or some other means of getting user_id
string msg = "your message";
await Discord.UserExtensions.SendMessageAsync(u, msg);
我想我来晚了,但我希望这能有所帮助。这适用于我的机器人。
[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");
}
}
我是新来的所以不要评判我, 我想创建一个命令 !poke 向指定用户发送直接消息 (PM),但我似乎无法找到向其提供用户 ID 的方法,而且 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! ");
});
或者,
var u = Context.Message.Author; // Or some other means of getting user_id
string msg = "your message";
await Discord.UserExtensions.SendMessageAsync(u, msg);
我想我来晚了,但我希望这能有所帮助。这适用于我的机器人。
[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");
}
}