Refactor random assignment and make it only assign staff with read access
This commit is contained in:
parent
12337101b7
commit
dac221246c
3 changed files with 43 additions and 57 deletions
|
@ -160,13 +160,13 @@ public class NewCommand
|
||||||
"!\nIs the channel limit reached in the server or ticket category?");
|
"!\nIs the channel limit reached in the server or ticket category?");
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong staffID = 0;
|
DiscordMember assignedStaff = null;
|
||||||
if (Config.randomAssignment)
|
if (Config.randomAssignment)
|
||||||
{
|
{
|
||||||
staffID = Database.GetRandomActiveStaff(0)?.userID ?? 0;
|
assignedStaff = await RandomAssignCommand.GetRandomVerifiedStaffMember(ticketChannel, userID, 0, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
long id = Database.NewTicket(member.Id, staffID, ticketChannel.Id);
|
long id = Database.NewTicket(member.Id, assignedStaff?.Id ?? 0, ticketChannel.Id);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await ticketChannel.ModifyAsync(modifiedAttributes => modifiedAttributes.Name = "ticket-" + id.ToString("00000"));
|
await ticketChannel.ModifyAsync(modifiedAttributes => modifiedAttributes.Name = "ticket-" + id.ToString("00000"));
|
||||||
|
@ -209,20 +209,19 @@ public class NewCommand
|
||||||
await Interviewer.StartInterview(ticketChannel);
|
await Interviewer.StartInterview(ticketChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (staffID != 0)
|
if (assignedStaff != null)
|
||||||
{
|
{
|
||||||
await ticketChannel.SendMessageAsync(new DiscordEmbedBuilder
|
await ticketChannel.SendMessageAsync(new DiscordEmbedBuilder
|
||||||
{
|
{
|
||||||
Color = DiscordColor.Green,
|
Color = DiscordColor.Green,
|
||||||
Description = "Ticket was randomly assigned to <@" + staffID + ">."
|
Description = "Ticket was randomly assigned to " + assignedStaff.Mention + "."
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Config.assignmentNotifications)
|
if (Config.assignmentNotifications)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DiscordMember staffMember = await category.Guild.GetMemberAsync(staffID);
|
await assignedStaff.SendMessageAsync(new DiscordEmbedBuilder
|
||||||
await staffMember.SendMessageAsync(new DiscordEmbedBuilder
|
|
||||||
{
|
{
|
||||||
Color = DiscordColor.Green,
|
Color = DiscordColor.Green,
|
||||||
Description = "You have been randomly assigned to a newly opened support ticket: " +
|
Description = "You have been randomly assigned to a newly opened support ticket: " +
|
||||||
|
|
|
@ -31,9 +31,14 @@ public class RandomAssignCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a random staff member who is verified to have the correct role if applicable
|
// Get a random staff member who is verified to have the correct role if applicable
|
||||||
DiscordMember staffMember = await GetRandomVerifiedStaffMember(command, role, ticket);
|
DiscordMember staffMember = await GetRandomVerifiedStaffMember(command.Channel, ticket.creatorID, ticket.assignedStaffID, role);
|
||||||
if (staffMember == null)
|
if (staffMember == null)
|
||||||
{
|
{
|
||||||
|
await command.RespondAsync(new DiscordEmbedBuilder
|
||||||
|
{
|
||||||
|
Color = DiscordColor.Red,
|
||||||
|
Description = "Error: Could not find an applicable staff member with access to this channel."
|
||||||
|
}, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +71,7 @@ public class RandomAssignCommand
|
||||||
Description = "You have been randomly assigned to a support ticket: " + command.Channel.Mention
|
Description = "You have been randomly assigned to a support ticket: " + command.Channel.Mention
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (UnauthorizedException) { }
|
catch (UnauthorizedException) { /* ignore */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -89,62 +94,50 @@ public class RandomAssignCommand
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<DiscordMember> GetRandomVerifiedStaffMember(SlashCommandContext command, DiscordRole targetRole, Database.Ticket ticket)
|
internal static async Task<DiscordMember> GetRandomVerifiedStaffMember(DiscordChannel channel, ulong creatorID, ulong currentStaffID, DiscordRole targetRole)
|
||||||
{
|
{
|
||||||
if (targetRole != null) // A role was provided
|
List<Database.StaffMember> staffMembers;
|
||||||
|
ulong[] ignoredUserIDs = [creatorID, currentStaffID];
|
||||||
|
|
||||||
|
if (targetRole == null)
|
||||||
|
{
|
||||||
|
// No role was specified, any active staff will be picked
|
||||||
|
staffMembers = Database.GetActiveStaff(ignoredUserIDs);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
// Check if role rassign should override staff's active status
|
// Check if role rassign should override staff's active status
|
||||||
List<Database.StaffMember> staffMembers = Config.randomAssignRoleOverride
|
staffMembers = Config.randomAssignRoleOverride
|
||||||
? Database.GetAllStaff(ticket.assignedStaffID, ticket.creatorID)
|
? Database.GetAllStaff(ignoredUserIDs)
|
||||||
: Database.GetActiveStaff(ticket.assignedStaffID, ticket.creatorID);
|
: Database.GetActiveStaff(ignoredUserIDs);
|
||||||
|
}
|
||||||
|
|
||||||
// Randomize the list before checking for roles in order to reduce number of API calls
|
// Randomize the list before checking for roles in order to reduce number of API calls
|
||||||
staffMembers.Shuffle();
|
staffMembers.Shuffle();
|
||||||
|
|
||||||
// Get the first staff member that has the role
|
// Get the first staff member that has the role
|
||||||
foreach (Database.StaffMember sm in staffMembers)
|
foreach (Database.StaffMember staffMember in staffMembers)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DiscordMember verifiedMember = await command.Guild.GetMemberAsync(sm.userID);
|
DiscordMember verifiedMember = await channel.Guild.GetMemberAsync(staffMember.userID);
|
||||||
if (verifiedMember?.Roles?.Any(role => role.Id == targetRole.Id) ?? false)
|
|
||||||
|
// If a role is set filter to only members with that role
|
||||||
|
if (targetRole == null || verifiedMember.Roles.Any(role => role.Id == targetRole.Id))
|
||||||
|
{
|
||||||
|
// Only assign staff members with access to this channel
|
||||||
|
if (verifiedMember.PermissionsIn(channel).HasFlag(DiscordPermissions.AccessChannels))
|
||||||
{
|
{
|
||||||
return verifiedMember;
|
return verifiedMember;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
command.Client.Logger.Log(LogLevel.Information, e, "Error occured trying to find a staff member in the rassign command.");
|
Logger.Error("Error occured trying to find a staff member for random assignment. User ID: " + staffMember.userID, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else // No role was specified, any active staff will be picked
|
|
||||||
{
|
|
||||||
Database.StaffMember staffEntry = Database.GetRandomActiveStaff(ticket.assignedStaffID, ticket.creatorID);
|
|
||||||
if (staffEntry == null)
|
|
||||||
{
|
|
||||||
await command.RespondAsync(new DiscordEmbedBuilder
|
|
||||||
{
|
|
||||||
Color = DiscordColor.Red,
|
|
||||||
Description = "Error: There are no other staff members to choose from."
|
|
||||||
}, true);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the staff member from discord
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return await command.Guild.GetMemberAsync(staffEntry.userID);
|
|
||||||
}
|
|
||||||
catch (NotFoundException) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send a more generic error if we get to this point and still haven't found the staff member
|
|
||||||
await command.RespondAsync(new DiscordEmbedBuilder
|
|
||||||
{
|
|
||||||
Color = DiscordColor.Red,
|
|
||||||
Description = "Error: Could not find an applicable staff member."
|
|
||||||
}, true);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -466,12 +466,6 @@ public static class Database
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static StaffMember GetRandomActiveStaff(params ulong[] ignoredUserIDs)
|
|
||||||
{
|
|
||||||
List<StaffMember> staffMembers = GetActiveStaff(ignoredUserIDs);
|
|
||||||
return staffMembers.Any() ? staffMembers[RandomNumberGenerator.GetInt32(staffMembers.Count)] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<StaffMember> GetActiveStaff(params ulong[] ignoredUserIDs)
|
public static List<StaffMember> GetActiveStaff(params ulong[] ignoredUserIDs)
|
||||||
{
|
{
|
||||||
using MySqlConnection c = GetConnection();
|
using MySqlConnection c = GetConnection();
|
||||||
|
|
Loading…
Reference in a new issue