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

View File

@ -28,7 +28,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
return; return;
// get the id of the user, this parses the string to an id // 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) if (user == null)
{ {
await request.SendSimpleEmbed("Kick", "I can't find that user on the server"); await request.SendSimpleEmbed("Kick", "I can't find that user on the server");
@ -72,13 +72,9 @@ namespace DeukBot4.MessageHandlers.CommandHandler
return; return;
// get the id of the user, this parses the string to an id // 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].AsDiscordUser();
if (user == null) 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 // get the permissions of the user we want to kick
var userPermissions = var userPermissions =
await PermissionValidator.GetUserPermissionLevel(request.OriginalMessage.Channel, (SocketUser) user); await PermissionValidator.GetUserPermissionLevel(request.OriginalMessage.Channel, (SocketUser) user);
@ -88,6 +84,10 @@ namespace DeukBot4.MessageHandlers.CommandHandler
await request.SendSimpleEmbed("Ban", "You are not allowed to ban that user"); await request.SendSimpleEmbed("Ban", "You are not allowed to ban that user");
return; return;
} }
}
var userId = request.Parameters[0].AsUlong();
if (!userId.HasValue)
return;
// see if a reason was given, use that if so // see if a reason was given, use that if so
var reason = string.Empty; var reason = string.Empty;
@ -96,24 +96,22 @@ namespace DeukBot4.MessageHandlers.CommandHandler
reason = request.Parameters[1].AsString(); reason = request.Parameters[1].AsString();
} }
if (user != null)
{
string desc = "No reason was given."; string desc = "No reason was given.";
if (!string.IsNullOrWhiteSpace(reason)) if (!string.IsNullOrWhiteSpace(reason))
{ {
desc = "The given reason was: " + desc; desc = "The given reason was: " + desc;
} }
var eb = new EmbedBuilder(); var eb = EmbedFactory.GetStandardEmbedBuilder();
eb.Color = Color.Gold; eb.Title = "Ban";
eb.Title = "You were banned from " + channel.Guild.Name;
eb.Description = desc; 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()); user.SendMessageAsync("", embed: eb.Build());
}
// and ban // and ban
await channel.Guild.AddBanAsync(user, 0, reason); await channel.Guild.AddBanAsync(userId.Value, 0, reason, RequestOptions.Default);
await request.SendMessageAsync($"User was banned: {user.Username}"); await request.SendSimpleEmbed("Ban", $"User was banned: <@!{userId}>");
} }
[Command("silence", PermissionLevel.Helper)] [Command("silence", PermissionLevel.Helper)]
@ -136,7 +134,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
return; return;
// get the id of the user, this parses the string to an id // 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) if (user == null)
{ {
await request.SendSimpleEmbed("Silence", "I can't find that user on the server"); 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; return null;
} }
public async Task<IGuildUser> AsDiscordUser(IGuild guild) public async Task<IGuildUser> AsDiscordGuildUser(IGuild guild)
{ {
if (ulong.TryParse(_value, out var i)) if (ulong.TryParse(_value, out var i))
{ {
@ -55,5 +55,15 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
return user; return user;
} }
} }
public async Task<IUser> AsDiscordUser()
{
if (ulong.TryParse(_value, out var i))
{
return Program.Client.GetUser(i);
}
return null;
}
} }
} }