2022-04-18 10:52:03 +00:00
using System ;
2022-02-21 08:40:09 +00:00
using System.Collections.Generic ;
2024-12-26 05:36:20 +00:00
using System.ComponentModel ;
2022-02-21 08:40:09 +00:00
using System.Linq ;
using System.Threading.Tasks ;
2024-12-26 05:36:20 +00:00
using DSharpPlus.Commands ;
using DSharpPlus.Commands.ContextChecks ;
using DSharpPlus.Commands.Processors.SlashCommands ;
2022-02-21 08:40:09 +00:00
using DSharpPlus.Entities ;
using DSharpPlus.Exceptions ;
using Microsoft.Extensions.Logging ;
2022-08-21 07:34:11 +00:00
namespace SupportChild.Commands ;
2024-12-26 05:36:20 +00:00
public class RandomAssignCommand
2022-02-21 08:40:09 +00:00
{
2024-12-26 05:36:20 +00:00
[RequireGuild]
[Command("rassign")]
[Description("Randomly assigns a staff member to a ticket.")]
public async Task OnExecute ( SlashCommandContext command , [ Parameter ( "role" ) ] [ Description ( "(Optional) Limit the random assignment to a specific role." ) ] DiscordRole role = null )
2024-10-29 09:10:37 +00:00
{
// Check if ticket exists in the database
if ( ! Database . TryGetOpenTicket ( command . Channel . Id , out Database . Ticket ticket ) )
{
2024-12-26 05:36:20 +00:00
await command . RespondAsync ( new DiscordEmbedBuilder
2024-10-29 09:10:37 +00:00
{
Color = DiscordColor . Red ,
Description = "Error: This channel is not a ticket."
} , true ) ;
return ;
}
2022-02-21 08:40:09 +00:00
2024-10-29 09:10:37 +00:00
// Get a random staff member who is verified to have the correct role if applicable
2024-12-27 06:43:06 +00:00
DiscordMember staffMember = await GetRandomVerifiedStaffMember ( command . Channel , ticket . creatorID , ticket . assignedStaffID , role ) ;
2024-10-29 09:10:37 +00:00
if ( staffMember = = null )
{
2024-12-27 06:43:06 +00:00
await command . RespondAsync ( new DiscordEmbedBuilder
{
Color = DiscordColor . Red ,
Description = "Error: Could not find an applicable staff member with access to this channel."
} , true ) ;
2024-10-29 09:10:37 +00:00
return ;
}
2022-02-21 08:40:09 +00:00
2024-10-29 09:10:37 +00:00
// Attempt to assign the staff member to the ticket
if ( ! Database . AssignStaff ( ticket , staffMember . Id ) )
{
2024-12-26 05:36:20 +00:00
await command . RespondAsync ( new DiscordEmbedBuilder
2024-10-29 09:10:37 +00:00
{
Color = DiscordColor . Red ,
Description = "Error: Failed to assign " + staffMember . Mention + " to ticket."
} , true ) ;
return ;
}
2022-02-21 08:40:09 +00:00
2024-10-29 09:10:37 +00:00
// Respond that the command was successful
2024-12-26 05:36:20 +00:00
await command . RespondAsync ( new DiscordEmbedBuilder
2024-10-29 09:10:37 +00:00
{
Color = DiscordColor . Green ,
Description = "Randomly assigned " + staffMember . Mention + " to ticket."
} ) ;
2022-02-21 08:40:09 +00:00
2024-10-29 09:10:37 +00:00
// Send a notification to the staff member if applicable
if ( Config . assignmentNotifications )
{
try
{
await staffMember . SendMessageAsync ( new DiscordEmbedBuilder
{
Color = DiscordColor . Green ,
Description = "You have been randomly assigned to a support ticket: " + command . Channel . Mention
} ) ;
}
2024-12-27 06:43:06 +00:00
catch ( UnauthorizedException ) { /* ignore */ }
2024-10-29 09:10:37 +00:00
}
2022-02-21 08:40:09 +00:00
2024-12-27 07:17:14 +00:00
await LogChannel . Success ( staffMember . Mention + " was randomly assigned to " + command . Channel . Mention + " by " + command . User . Mention + "." , ticket . id ) ;
2024-10-29 09:10:37 +00:00
}
2022-02-21 08:40:09 +00:00
2024-12-27 06:43:06 +00:00
internal static async Task < DiscordMember > GetRandomVerifiedStaffMember ( DiscordChannel channel , ulong creatorID , ulong currentStaffID , DiscordRole targetRole )
2024-10-29 09:10:37 +00:00
{
2024-12-27 06:43:06 +00:00
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
2024-10-29 09:10:37 +00:00
{
// Check if role rassign should override staff's active status
2024-12-27 06:43:06 +00:00
staffMembers = Config . randomAssignRoleOverride
? Database . GetAllStaff ( ignoredUserIDs )
: Database . GetActiveStaff ( ignoredUserIDs ) ;
}
2022-02-21 08:40:09 +00:00
2024-12-27 06:43:06 +00:00
// Randomize the list before checking for roles in order to reduce number of API calls
staffMembers . Shuffle ( ) ;
2022-02-21 08:40:09 +00:00
2024-12-27 06:43:06 +00:00
// Get the first staff member that has the role
foreach ( Database . StaffMember staffMember in staffMembers )
{
try
2024-10-29 09:10:37 +00:00
{
2024-12-27 06:43:06 +00:00
DiscordMember verifiedMember = await channel . Guild . GetMemberAsync ( staffMember . userID ) ;
// If a role is set filter to only members with that role
if ( targetRole = = null | | verifiedMember . Roles . Any ( role = > role . Id = = targetRole . Id ) )
2024-10-29 09:10:37 +00:00
{
2024-12-27 06:43:06 +00:00
// Only assign staff members with access to this channel
if ( verifiedMember . PermissionsIn ( channel ) . HasFlag ( DiscordPermissions . AccessChannels ) )
2024-10-29 09:10:37 +00:00
{
return verifiedMember ;
}
}
}
2024-12-27 06:43:06 +00:00
catch ( Exception e )
2024-10-29 09:10:37 +00:00
{
2024-12-27 06:43:06 +00:00
Logger . Error ( "Error occured trying to find a staff member for random assignment. User ID: " + staffMember . userID , e ) ;
2024-10-29 09:10:37 +00:00
}
}
2022-08-21 07:34:11 +00:00
2024-10-29 09:10:37 +00:00
return null ;
}
2022-05-19 11:38:59 +00:00
}