Better handling of parameter casting

This commit is contained in:
Deukhoofd 2018-03-30 19:03:25 +02:00
parent 4e09fc1de0
commit 349382301e
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 19 additions and 8 deletions

View File

@ -26,6 +26,11 @@ namespace DeukBot4.MessageHandlers.CommandHandler
// 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)
{
await request.SendMessageAsync("I can't find that user on the server");
return;
}
// get the permissions of the user we want to kick
var userPermissions =
@ -58,6 +63,11 @@ namespace DeukBot4.MessageHandlers.CommandHandler
// 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)
{
await request.SendMessageAsync("I can't find that user on the server");
return;
}
// get the permissions of the user we want to kick
var userPermissions =

View File

@ -72,14 +72,15 @@ namespace DeukBot4.MessageHandlers.CommandHandler
return;
}
if (!ulong.TryParse(request.Parameters[0].AsString(), out var roleId))
var roleId = request.Parameters[0].AsUlong();
if (!roleId.HasValue)
{
await request.SendMessageAsync(
$"You did not give a valid role ID. Use ``!roles`` to list all current server roles, along with their ids");
return;
}
var role = serverChannel.Guild.GetRole(roleId);
var role = serverChannel.Guild.GetRole(roleId.Value);
if (role == null)
{
await request.SendMessageAsync("No role with that id exists on this server");
@ -88,8 +89,8 @@ namespace DeukBot4.MessageHandlers.CommandHandler
try
{
await DatabaseRolePermissions.SetRolePermission(serverChannel.GuildId, roleId, permissionLevel);
PermissionValidator.UpdateCache(serverChannel.GuildId, roleId, permissionLevel);
await DatabaseRolePermissions.SetRolePermission(serverChannel.GuildId, roleId.Value, permissionLevel);
PermissionValidator.UpdateCache(serverChannel.GuildId, roleId.Value, permissionLevel);
}
catch(Exception e)
{

View File

@ -14,13 +14,13 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
_value = value;
}
public int AsInt()
public int? AsInt()
{
if (int.TryParse(_value, out var i))
{
return i;
}
throw new ArgumentException();
return null;
}
public string AsString()
@ -28,13 +28,13 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
return _value;
}
public ulong AsUlong()
public ulong? AsUlong()
{
if (ulong.TryParse(_value, out var i))
{
return i;
}
throw new ArgumentException();
return null;
}
public async Task<IGuildUser> AsDiscordUser(IGuild guild)