我正在创建一个不和谐的机器人,而且我正在努力弄清楚如何踢用户。我无法找到相关的代码或文档。这是代码
private void MuteUserCommand()
{
commands.CreateCommand("stfu")
.Parameter("PersonToKick", ParameterType.Required)
.Do(async (e) =>
{
string persontomute = e.GetArg("PersonToKick");
if (e.User.ServerPermissions.MuteMembers == false)
{
await e.Channel.SendMessage("Yoo cahnot tells " + persontomute + " to shut up, iz not enuff poweh!");
}
else
{
await e.Channel.SendMessage("Shut up " + persontomute + "!");
//How do I kick persontomute here?
}
});
}
更新:我使用的是Discord.NET 0.9.6
答案 0 :(得分:2)
这是我用来踢人,检查正确权限等的命令。(确保机器人本身有踢权限)
_client.GetService<CommandService>().CreateGroup("user", cgb =>
{
cgb.CreateCommand("kick")
.Description("Kick a user from the Server.")
.Parameter("User", ParameterType.Required)
.AddCheck((command, user, channel) => !paused)
.Do(async e =>
{
try
{
if (e.User.ServerPermissions.KickMembers)
{
User user = null;
try
{
// try to find the user
user = e.Server.FindUsers(e.GetArg("User")).First();
}
catch (InvalidOperationException)
{
await e.Channel.SendMessage($"Couldn't kick user {e.GetArg("User")} (not found).");
return;
}
// double safety check
if (user == null) await e.Channel.SendMessage($"Couldn't kick user {e.GetArg("User")} (not found).");
await user.Kick();
await e.Channel.SendMessage($"{user.Name} was kicked from the server!");
}
else
{
await e.Channel.SendMessage($"{e.User.Name} you don't have the permission to kick.");
}
}
catch (Exception ex)
{
// needs a better error handling haven't changed it since i tested it xD
await e.Channel.SendMessage(ex.Message);
}
});
答案 1 :(得分:1)
您似乎正在使用Discord.Net。您需要从服务器获取用户然后踢它们,如documentation中所述 编辑:我的不好,我从朋友的机器人拿走了这个片段,忘记了它使用了自定义扩展程序。我添加了一种“香草”方式,采用 straight from the sample bot 。
var user = await _client.FindUser(e, e.Args[0], e.Args[1]); //args[0]would be the username, args[1] would be the discriminator (the random number that follows the discord id)
if (user == null) return;
await user.Kick();
旧
string err;
User user = e.Server.Users.FindUser(persontomute, out err);
if(user != null)
{
await user.Kick();
await e.Channel.SendMessage("Shut up " + persontomute + "!");
}