Allow banning users not in the server

This commit is contained in:
Deukhoofd 2018-10-15 18:36:45 +02:00
parent 6dcdda3e5d
commit 4357b43a6d
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 42 additions and 34 deletions

View File

@ -145,7 +145,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
var guild = (request.OriginalMessage.Channel as IGuildChannel)?.Guild;
if (guild != null)
{
user = (SocketUser) await request.Parameters[0].AsDiscordUser(guild);
user = (SocketUser) await request.Parameters[0].AsDiscordGuildUser(guild);
}
if (user == null)
{
@ -191,7 +191,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
var guild = (request.OriginalMessage.Channel as IGuildChannel)?.Guild;
if (guild != null)
{
user = (SocketUser) await request.Parameters[0].AsDiscordUser(guild);
user = (SocketUser) await request.Parameters[0].AsDiscordGuildUser(guild);
}
if (user == null)
{
@ -293,7 +293,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
if (guildChannel == null)
return;
var user = await request.Parameters[0].AsDiscordUser(guildChannel.Guild);
var user = await request.Parameters[0].AsDiscordGuildUser(guildChannel.Guild);
string message;
if (request.OriginalMessage.Author.Id == user.Id)
{

View File

@ -28,7 +28,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
return;
// get the id of the user, this parses the string to an id
var user = await request.Parameters[0].AsDiscordUser(channel.Guild);
var user = await request.Parameters[0].AsDiscordGuildUser(channel.Guild);
if (user == null)
{
await request.SendSimpleEmbed("Kick", "I can't find that user on the server");
@ -72,22 +72,22 @@ namespace DeukBot4.MessageHandlers.CommandHandler
return;
// get the id of the user, this parses the string to an id
var user = await request.Parameters[0].AsDiscordUser(channel.Guild);
if (user == null)
var user = await request.Parameters[0].AsDiscordUser();
if (user != null)
{
await request.SendSimpleEmbed("Ban", "I can't find that user on the server");
return;
// get the permissions of the user we want to kick
var userPermissions =
await PermissionValidator.GetUserPermissionLevel(request.OriginalMessage.Channel, (SocketUser) user);
// if the user has sufficient permissions, or is deukbot, warn the user that he's not allowed to do that, and stop
if (userPermissions >= PermissionLevel.Helper || user.Id == Program.Client.CurrentUser.Id)
{
await request.SendSimpleEmbed("Ban", "You are not allowed to ban that user");
return;
}
}
// get the permissions of the user we want to kick
var userPermissions =
await PermissionValidator.GetUserPermissionLevel(request.OriginalMessage.Channel, (SocketUser) user);
// if the user has sufficient permissions, or is deukbot, warn the user that he's not allowed to do that, and stop
if (userPermissions >= PermissionLevel.Helper || user.Id == Program.Client.CurrentUser.Id)
{
await request.SendSimpleEmbed("Ban", "You are not allowed to ban that user");
var userId = request.Parameters[0].AsUlong();
if (!userId.HasValue)
return;
}
// see if a reason was given, use that if so
var reason = string.Empty;
@ -96,24 +96,22 @@ namespace DeukBot4.MessageHandlers.CommandHandler
reason = request.Parameters[1].AsString();
}
string desc = "No reason was given.";
if (!string.IsNullOrWhiteSpace(reason))
if (user != null)
{
desc = "The given reason was: " + desc;
}
string desc = "No reason was given.";
if (!string.IsNullOrWhiteSpace(reason))
{
desc = "The given reason was: " + desc;
}
var eb = new EmbedBuilder();
eb.Color = Color.Gold;
eb.Title = "You were banned from " + channel.Guild.Name;
eb.Description = desc;
eb.Author = new EmbedAuthorBuilder();
eb.Author.Name = request.OriginalMessage.Author.Username;
eb.Author.IconUrl = request.OriginalMessage.Author.GetAvatarUrl();
eb.Timestamp = request.OriginalMessage.Timestamp;
user.SendMessageAsync("", embed: eb.Build());
var eb = EmbedFactory.GetStandardEmbedBuilder();
eb.Title = "Ban";
eb.Description = desc;
user.SendMessageAsync("", embed: eb.Build());
}
// and ban
await channel.Guild.AddBanAsync(user, 0, reason);
await request.SendMessageAsync($"User was banned: {user.Username}");
await channel.Guild.AddBanAsync(userId.Value, 0, reason, RequestOptions.Default);
await request.SendSimpleEmbed("Ban", $"User was banned: <@!{userId}>");
}
[Command("silence", PermissionLevel.Helper)]
@ -136,7 +134,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
return;
// get the id of the user, this parses the string to an id
var user = await request.Parameters[0].AsDiscordUser(channel.Guild);
var user = await request.Parameters[0].AsDiscordGuildUser(channel.Guild);
if (user == null)
{
await request.SendSimpleEmbed("Silence", "I can't find that user on the server");

View File

@ -39,7 +39,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
return null;
}
public async Task<IGuildUser> AsDiscordUser(IGuild guild)
public async Task<IGuildUser> AsDiscordGuildUser(IGuild guild)
{
if (ulong.TryParse(_value, out var i))
{
@ -55,5 +55,15 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
return user;
}
}
public async Task<IUser> AsDiscordUser()
{
if (ulong.TryParse(_value, out var i))
{
return Program.Client.GetUser(i);
}
return null;
}
}
}