Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reorder .assignrole args for easier aliasing #498

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions Izzy-Moonbot/Modules/ModCoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "")
{
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions Izzy-MoonbotTests/Tests/ModCoreModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <t:1286669100:R>.");
TestUtils.AssertListsAreEqual(new List<ulong> { alicornId }, guild.UserRoles[pippId]);
Expand All @@ -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 <t:1286669400:R>.");
TestUtils.AssertListsAreEqual(new List<ulong> { alicornId }, guild.UserRoles[pippId]);
Expand Down Expand Up @@ -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 <t:1286669100:R>.");
TestUtils.AssertListsAreEqual(new List<ulong> { alicornId }, guild.UserRoles[pippId]);
Expand Down
Loading