diff --git a/Izzy-Moonbot/Modules/ModCoreModule.cs b/Izzy-Moonbot/Modules/ModCoreModule.cs index 8b63409f..3ef4cdf0 100644 --- a/Izzy-Moonbot/Modules/ModCoreModule.cs +++ b/Izzy-Moonbot/Modules/ModCoreModule.cs @@ -401,9 +401,9 @@ public async Task TestableBanAllCommandAsync( [ModCommand(Group = "Permissions")] [DevCommand(Group = "Permissions")] [Parameter("role", ParameterType.Role, "The role to assign.")] - [Parameter("user", ParameterType.UnambiguousUser, "The user to assign the role.")] [Parameter("duration", ParameterType.DateTime, "How long the role should last, e.g. \"2 weeks\" or \"6 months\". Omit for an indefinite role assignment.", true)] - [Example(".assignrole @Best Pony @Izzy Moonbot 24 hours")] + [Parameter("user", ParameterType.UnambiguousUser, "The user to assign the role.")] + [Example(".assignrole @Best Pony 24 hours @Izzy Moonbot")] public async Task AssignRoleCommandAsync( [Remainder] string argsString = "") { @@ -430,29 +430,39 @@ public async Task TestableAssignRoleCommandAsync( } var role = context.Guild?.GetRole(roleId); - if (ParseHelper.TryParseUnambiguousUser(argsAfterRole, out var userErrorString) is not var (userId, argsAfterUser)) - { - await Context.Channel.SendMessageAsync($"Failed to get a user id from the second argument: {userErrorString}"); - return; - } - var maybeMember = context.Guild?.GetUser(userId); - ParseDateTimeResult? time = null; - if (argsAfterUser.Trim() != "") + string? argsAfterTime = null; + if (argsAfterRole.Trim() != "") { - time = ParseHelper.TryParseDateTime(argsAfterUser, out var parseError)?.Item1; - if (time is null) + var timeResult = ParseHelper.TryParseDateTime(argsAfterRole, out var parseError); + if (timeResult is null) { - await Context.Channel.SendMessageAsync($"Failed to comprehend time: {parseError}"); - return; + argsAfterTime = argsAfterRole; } - if (time.RepeatType is not ScheduledJobRepeatType.None) + else { - await context.Channel.SendMessageAsync("I can't assign a role repeatedly! Please give me a time that isn't repeating."); - return; + time = timeResult?.Item1; + argsAfterTime = timeResult?.Item2; + if (time?.RepeatType is not ScheduledJobRepeatType.None) + { + await context.Channel.SendMessageAsync("I can't assign a role repeatedly! Please give me a time that isn't repeating."); + return; + } } } + if (argsAfterTime is null) + { + await context.Channel.SendMessageAsync("I need a user to assign the role to."); + return; + } + if (ParseHelper.TryParseUnambiguousUser(argsAfterTime, out var userErrorString) is not var (userId, argsAfterUser)) + { + await Context.Channel.SendMessageAsync($"Failed to get a user id from the last argument: {userErrorString}"); + return; + } + var maybeMember = context.Guild?.GetUser(userId); + if (maybeMember is IIzzyGuildUser member) { if (role?.Position >= context.Guild?.GetUser(context.Client.CurrentUser.Id)?.Hierarchy) diff --git a/Izzy-MoonbotTests/Tests/ModCoreModuleTests.cs b/Izzy-MoonbotTests/Tests/ModCoreModuleTests.cs index e7f64500..c7ef6102 100644 --- a/Izzy-MoonbotTests/Tests/ModCoreModuleTests.cs +++ b/Izzy-MoonbotTests/Tests/ModCoreModuleTests.cs @@ -148,8 +148,8 @@ public async Task AssignRole_Command_Tests() // .assignrole with duration - context = await client.AddMessageAsync(guild.Id, generalChannel.Id, sunny.Id, $".assignrole <@&{alicornId}> <@{pippId}> 5 minutes"); - await mcm.TestableAssignRoleCommandAsync(context, $"<@&{alicornId}> <@{pippId}> 5 minutes"); + context = await client.AddMessageAsync(guild.Id, generalChannel.Id, sunny.Id, $".assignrole <@&{alicornId}> 5 minutes <@{pippId}>"); + await mcm.TestableAssignRoleCommandAsync(context, $"<@&{alicornId}> 5 minutes <@{pippId}>"); Assert.AreEqual(generalChannel.Messages.Last().Content, $"I've given <@&{alicornId}> to <@{pippId}>. I've scheduled a removal ."); TestUtils.AssertListsAreEqual(new List { alicornId }, guild.UserRoles[pippId]); @@ -158,8 +158,8 @@ public async Task AssignRole_Command_Tests() // changing an existing role assignment from indefinite to finite - context = await client.AddMessageAsync(guild.Id, generalChannel.Id, sunny.Id, $".assignrole <@&{alicornId}> {hitchId} 10 minutes"); - await mcm.TestableAssignRoleCommandAsync(context, $"<@&{alicornId}> {hitchId} 10 minutes"); + context = await client.AddMessageAsync(guild.Id, generalChannel.Id, sunny.Id, $".assignrole <@&{alicornId}> 10 minutes {hitchId}"); + await mcm.TestableAssignRoleCommandAsync(context, $"<@&{alicornId}> 10 minutes {hitchId}"); StringAssert.Contains(generalChannel.Messages.Last().Content, $"<@{hitchId}> already has that role. I've scheduled a removal ."); TestUtils.AssertListsAreEqual(new List { alicornId }, guild.UserRoles[pippId]); @@ -197,8 +197,8 @@ public async Task AssignRole_ExtraSpaces_Tests() DateTimeHelper.FakeUtcNow = TestUtils.FiMEpoch; Assert.IsFalse(guild.UserRoles.ContainsKey(pippId)); - var context = await client.AddMessageAsync(guild.Id, generalChannel.Id, sunny.Id, $".assignrole <@&{alicornId}> <@{pippId}> 5 minutes"); - await mcm.TestableAssignRoleCommandAsync(context, $"<@&{alicornId}> <@{pippId}> 5 minutes"); + var context = await client.AddMessageAsync(guild.Id, generalChannel.Id, sunny.Id, $".assignrole <@&{alicornId}> 5 minutes <@{pippId}>"); + await mcm.TestableAssignRoleCommandAsync(context, $"<@&{alicornId}> 5 minutes <@{pippId}>"); Assert.AreEqual(generalChannel.Messages.Last().Content, $"I've given <@&{alicornId}> to <@{pippId}>. I've scheduled a removal ."); TestUtils.AssertListsAreEqual(new List { alicornId }, guild.UserRoles[pippId]);