在 Discord.Net 中踢用户
Kicking a user in Discord.Net
我编写此代码是为了将用户踢出 discord 服务器的公会
bot 控制台没有给我任何错误,但是当我尝试踢用户时这段代码无法正常工作。
我的意思是它不会踢用户,它不会在频道中输出任何东西。
我还检查了机器人权限,并更新了这些权限,所以这不是问题所在。
discord.net的版本是1.0.2
[Command("kick"), Alias("Kick"), Summary("Kicks a user from the server")]
public async Task Kick(SocketGuildUser userAccount, string reason)
{
var user = Context.User as SocketGuildUser;
var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Admin");
if (!userAccount.Roles.Contains(role))
{
if (user.GuildPermissions.KickMembers)
{
await userAccount.KickAsync(reason);
await Context.Channel.SendMessageAsync($"The user `{userAccount}` has been kicked, for {reason}");
}
else
{
await Context.Channel.SendMessageAsync("No permissions for kicking a user.");
}
}
else
{
await Context.Channel.SendMessageAsync("This User can't be kicked, because the user has a admin role.");
}
}
我想出来了,而不是使用
async Task Kick(SocketGuildUser userAccount, string reason)
我需要使用
async Task Kick(IGuildUser userAccount, string reason)
[Command("kick")]
[RequireBotPermission(GuildPermission.KickMembers)]
public async Task KickAsync(IGuildUser user)
{
var GuildUser = Context.Guild.GetUser(Context.User.Id);
if (!GuildUser.GuildPermissions.KickMembers)
{
await Context.Message.DeleteAsync();
await ReplyAsync(":warning: `No permissions to kick players`");
return;
}
else
{
await user.KickAsync();
await Context.Channel.SendMessageAsync($":eye: `{user.Username} has been kicked from the server`");
var guild = Context.Guild;
var channel = guild.GetTextChannel(609086251978457098); //582790350620327952
EmbedBuilder builder = new EmbedBuilder();
builder.WithTitle("Logged Information")
.AddField("User", $"{user.Mention}")
.AddField("Moderator", $"{Context.User.Username}")
.AddField("Other Information", "Can be invited back")
.AddField("Command", $"``.kick {user.Username}``")
.WithDescription($"This player has been kicked from {Context.Guild.Name} by {Context.User.Username}")
.WithFooter($"{Context.User.Username}", Context.User.GetAvatarUrl())
.WithCurrentTimestamp()
.WithColor(new Color(54, 57, 62));
}
}
我编写此代码是为了将用户踢出 discord 服务器的公会
bot 控制台没有给我任何错误,但是当我尝试踢用户时这段代码无法正常工作。 我的意思是它不会踢用户,它不会在频道中输出任何东西。
我还检查了机器人权限,并更新了这些权限,所以这不是问题所在。
discord.net的版本是1.0.2
[Command("kick"), Alias("Kick"), Summary("Kicks a user from the server")]
public async Task Kick(SocketGuildUser userAccount, string reason)
{
var user = Context.User as SocketGuildUser;
var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Admin");
if (!userAccount.Roles.Contains(role))
{
if (user.GuildPermissions.KickMembers)
{
await userAccount.KickAsync(reason);
await Context.Channel.SendMessageAsync($"The user `{userAccount}` has been kicked, for {reason}");
}
else
{
await Context.Channel.SendMessageAsync("No permissions for kicking a user.");
}
}
else
{
await Context.Channel.SendMessageAsync("This User can't be kicked, because the user has a admin role.");
}
}
我想出来了,而不是使用
async Task Kick(SocketGuildUser userAccount, string reason)
我需要使用
async Task Kick(IGuildUser userAccount, string reason)
[Command("kick")]
[RequireBotPermission(GuildPermission.KickMembers)]
public async Task KickAsync(IGuildUser user)
{
var GuildUser = Context.Guild.GetUser(Context.User.Id);
if (!GuildUser.GuildPermissions.KickMembers)
{
await Context.Message.DeleteAsync();
await ReplyAsync(":warning: `No permissions to kick players`");
return;
}
else
{
await user.KickAsync();
await Context.Channel.SendMessageAsync($":eye: `{user.Username} has been kicked from the server`");
var guild = Context.Guild;
var channel = guild.GetTextChannel(609086251978457098); //582790350620327952
EmbedBuilder builder = new EmbedBuilder();
builder.WithTitle("Logged Information")
.AddField("User", $"{user.Mention}")
.AddField("Moderator", $"{Context.User.Username}")
.AddField("Other Information", "Can be invited back")
.AddField("Command", $"``.kick {user.Username}``")
.WithDescription($"This player has been kicked from {Context.Guild.Name} by {Context.User.Username}")
.WithFooter($"{Context.User.Username}", Context.User.GetAvatarUrl())
.WithCurrentTimestamp()
.WithColor(new Color(54, 57, 62));
}
}