@@ -10,10 +10,11 @@ import {
10
10
emojis ,
11
11
getRandomInt ,
12
12
sanitizeName ,
13
- calculateBlockExpiry ,
13
+ calculateExpiry ,
14
14
isDuelPug ,
15
15
getRandomPickIndex ,
16
16
teamEmojiTypes ,
17
+ getPlayerIndexFromPlayerList ,
17
18
} from '~/utils' ;
18
19
import {
19
20
addGuildGameType ,
@@ -41,6 +42,8 @@ import store, {
41
42
disableCoinFlip ,
42
43
updateTeamEmojis ,
43
44
updatePickingOrder ,
45
+ addAutoRemoval ,
46
+ clearAutoRemoval ,
44
47
} from '~/store' ;
45
48
import {
46
49
formatPugFilledDM ,
@@ -456,6 +459,9 @@ export const handleJoinGameTypes: Handler = async (
456
459
`Remove pug ${ toBroadcast . name } at guild ${ guild . id } from store`
457
460
) ;
458
461
store . dispatch ( removePug ( { guildId : guild . id , name : toBroadcast . name } ) ) ;
462
+ store . dispatch (
463
+ clearAutoRemoval ( { guildId : guild . id , userId : author . id } )
464
+ ) ;
459
465
}
460
466
}
461
467
@@ -555,7 +561,13 @@ export const handleLeaveGameTypes: Handler = async (
555
561
556
562
const leaveMessage = formatLeaveStatus (
557
563
allLeaveStatuses ,
558
- content === 'zzz' ? 'offline' : content === 'left' ? 'left' : undefined
564
+ content === 'zzz'
565
+ ? 'offline'
566
+ : content === 'left'
567
+ ? 'left'
568
+ : content === 'arr'
569
+ ? 'autoremove'
570
+ : undefined
559
571
) ;
560
572
561
573
if ( ! returnMsg ) {
@@ -646,9 +658,11 @@ export const handleLeaveAllGameTypes: Handler = async (message) => {
646
658
647
659
const cache = store . getState ( ) ;
648
660
const pugs = cache . pugs [ guild . id ] ;
649
- if ( ! pugs ) return ;
661
+ const misc = cache . misc [ guild . id ] ;
662
+ if ( ! pugs || ! misc ) return ;
650
663
651
664
const { list } = pugs ;
665
+ const { autoremovals } = misc ;
652
666
653
667
const listToLeave = list
654
668
. filter ( ( pug ) => pug . players . find ( ( p ) => p . id === author . id ) )
@@ -661,6 +675,9 @@ export const handleLeaveAllGameTypes: Handler = async (message) => {
661
675
return ;
662
676
}
663
677
678
+ if ( autoremovals [ author . id ] )
679
+ store . dispatch ( clearAutoRemoval ( { guildId : guild . id , userId : author . id } ) ) ;
680
+
664
681
handleLeaveGameTypes ( message , listToLeave ) ;
665
682
log . info ( `Exiting handleLeaveAllGameTypes` ) ;
666
683
} ;
@@ -716,7 +733,11 @@ export const handlePickPlayer: Handler = async (
716
733
mentionedUser
717
734
) => {
718
735
log . info ( `Entering handlePickPlayer` ) ;
719
- const { guild, author } = message ;
736
+ const {
737
+ guild,
738
+ author,
739
+ mentions : { users } ,
740
+ } = message ;
720
741
if ( ! guild ) return ;
721
742
722
743
const cache = store . getState ( ) ;
@@ -757,11 +778,15 @@ export const handlePickPlayer: Handler = async (
757
778
let lastPickedPlayerIndex : number | null ;
758
779
759
780
const canPickTwice = pickingOrder [ turn + 1 ] === team ; // next turn is same team pick
781
+ const mentionedUsers = users . array ( ) ;
760
782
761
783
// +1 because few lines down we're going to subtract -1 so we still gucci 😎
784
+ const firstMentionedUser = mentionedUsers [ 0 ] ;
762
785
const playerIndex =
763
786
index === 'random'
764
787
? getRandomPickIndex ( forPug . players ) + 1
788
+ : firstMentionedUser
789
+ ? getPlayerIndexFromPlayerList ( forPug . players , firstMentionedUser ) + 1
765
790
: parseInt ( index ) ;
766
791
if ( ! playerIndex ) return ;
767
792
@@ -786,9 +811,12 @@ export const handlePickPlayer: Handler = async (
786
811
/**
787
812
* Situation when captain picks two in a row
788
813
*/
814
+ const secondMentionedUser = mentionedUsers [ 1 ] ;
789
815
const playerIndex2 =
790
816
index2 === 'random'
791
817
? getRandomPickIndex ( forPug . players ) + 1
818
+ : secondMentionedUser
819
+ ? getPlayerIndexFromPlayerList ( forPug . players , secondMentionedUser ) + 1
792
820
: parseInt ( index2 ) ;
793
821
794
822
if ( canPickTwice && playerIndex2 ) {
@@ -816,11 +844,9 @@ export const handlePickPlayer: Handler = async (
816
844
}
817
845
}
818
846
819
- const pickedPlayers = [
820
- pick1 ,
821
- pick2 ,
822
- lastPickedPlayerIndex ,
823
- ] . filter ( ( i ) : i is number => Number . isInteger ( i ) ) ;
847
+ const pickedPlayers = [ pick1 , pick2 , lastPickedPlayerIndex ] . filter (
848
+ ( i ) : i is number => Number . isInteger ( i )
849
+ ) ;
824
850
message . channel . send ( formatPickPlayerStatus ( forPug , pickedPlayers ) ?? '' ) ;
825
851
826
852
if ( ! forPug . isInPickingMode ) {
@@ -1068,13 +1094,13 @@ export const handlePromoteAvailablePugs: Handler = async (message, args) => {
1068
1094
1069
1095
export const handleDecidePromoteOrPick : Handler = async ( message , args ) => {
1070
1096
log . info ( `Entering handleDecidePromoteOrPick` ) ;
1071
- const { guild, cmd } = message ;
1097
+ const { guild, cmd, mentions } = message ;
1072
1098
if ( ! guild || ! cmd ) return ;
1073
1099
1074
1100
// just p or promote
1075
1101
if ( ! args [ 0 ] ) handlePromoteAvailablePugs ( message , args ) ;
1076
1102
else {
1077
- // p 4 or p siege5
1103
+ // p 4 or p siege5 or p @mention
1078
1104
const cache = store . getState ( ) ;
1079
1105
const pugs = cache . pugs [ guild . id ] ;
1080
1106
if ( ! pugs ) return ;
@@ -1085,7 +1111,11 @@ export const handleDecidePromoteOrPick: Handler = async (message, args) => {
1085
1111
) ;
1086
1112
1087
1113
if ( isArgGameType ) handlePromoteAvailablePugs ( message , args ) ;
1088
- else if ( ! isNaN ( parseInt ( args [ 0 ] ) ) || args [ 0 ] === 'random' )
1114
+ else if (
1115
+ mentions . users . size !== 0 ||
1116
+ ! isNaN ( parseInt ( args [ 0 ] ) ) ||
1117
+ args [ 0 ] === 'random'
1118
+ )
1089
1119
handlePickPlayer ( message , args ) ;
1090
1120
else handlePromoteAvailablePugs ( message , args ) ;
1091
1121
}
@@ -1282,6 +1312,8 @@ export const handleAdminPickPlayer: Handler = async (message, args) => {
1282
1312
}
1283
1313
1284
1314
mentionedUser . username = sanitizeName ( mentionedUser . username ) ;
1315
+ // because if picks are also mentions then the mentioned user should atleast be removed for no confusion
1316
+ message . mentions . users . delete ( mentionedUser . id ) ;
1285
1317
handlePickPlayer ( message , args . slice ( 1 ) , mentionedUser ) ;
1286
1318
log . info ( `Exiting handleAdminPickPlayer` ) ;
1287
1319
} ;
@@ -1370,7 +1402,7 @@ export const handleAdminBlockPlayer: Handler = async (message, args) => {
1370
1402
const blockLength = parseInt ( blockLengthString ) ;
1371
1403
if ( blockLength <= 0 ) return ;
1372
1404
1373
- const expiry = calculateBlockExpiry (
1405
+ const expiry = calculateExpiry (
1374
1406
blockPeriodString as 'm' | 'h' | 'd' ,
1375
1407
blockLength
1376
1408
) ;
@@ -1695,3 +1727,82 @@ export const handleAdminEnforceCustomPickingOrder: Handler = async (
1695
1727
1696
1728
log . info ( `Exiting handleAdminEnforceCustomPickingOrder` ) ;
1697
1729
} ;
1730
+
1731
+ export const handleAddAutoRemove : Handler = async ( message , args ) => {
1732
+ log . info ( `Entering handleAddAutoRemove` ) ;
1733
+ const { guild, author } = message ;
1734
+ if ( ! guild ) return ;
1735
+
1736
+ const cache = store . getState ( ) ;
1737
+ const misc = cache . misc [ guild . id ] ;
1738
+ const pugs = cache . pugs [ guild . id ] ;
1739
+ if ( ! misc || ! pugs ) return ;
1740
+
1741
+ const userId = author . id ;
1742
+ const { list } = pugs ;
1743
+
1744
+ const userHasJoinedAtleastOnePug = list . some ( ( pug ) =>
1745
+ pug . players . find ( ( p ) => p . id === userId )
1746
+ ) ;
1747
+
1748
+ if ( ! userHasJoinedAtleastOnePug ) {
1749
+ log . debug ( `User ${ userId } has not joined any pug, so no autoremoval` ) ;
1750
+ message . channel . send (
1751
+ `You need to join atleast one pug to be able to use this command`
1752
+ ) ;
1753
+ return ;
1754
+ }
1755
+
1756
+ const [ timeframe = '' ] = args ;
1757
+ const [ autoRemoveLengthString ] = timeframe . match ( / [ 0 - 9 ] + / g) ?? [ ] ;
1758
+ const [ autoRemovePeriodString ] = timeframe . match ( / [ m | h | d ] / g) ?? [ ] ;
1759
+
1760
+ if ( ! autoRemoveLengthString || ! autoRemovePeriodString ) {
1761
+ message . channel . send ( `No duration was provided` ) ;
1762
+ return ;
1763
+ }
1764
+
1765
+ const autoRemoveLength = parseInt ( autoRemoveLengthString ) ;
1766
+ if ( autoRemoveLength <= 0 ) return ;
1767
+
1768
+ const expiry = calculateExpiry (
1769
+ autoRemovePeriodString as 'm' | 'h' | 'd' ,
1770
+ autoRemoveLength
1771
+ ) ;
1772
+
1773
+ store . dispatch ( addAutoRemoval ( { guildId : guild . id , userId, expiry } ) ) ;
1774
+ log . info ( `Autoremoval added for user ${ userId } at ${ expiry . toUTCString ( ) } ` ) ;
1775
+
1776
+ message . channel . send (
1777
+ `<@${ userId } >, you will be autoremoved from every pug at **${ expiry . toUTCString ( ) } **`
1778
+ ) ;
1779
+
1780
+ log . info ( `Exiting handleAddAutoRemove` ) ;
1781
+ } ;
1782
+
1783
+ export const handleClearAutoRemove : Handler = async ( message , _ ) => {
1784
+ log . info ( `Entering handleClearAutoRemove` ) ;
1785
+ const { guild, author } = message ;
1786
+ if ( ! guild ) return ;
1787
+
1788
+ const cache = store . getState ( ) ;
1789
+ const misc = cache . misc [ guild . id ] ;
1790
+ if ( ! misc ) return ;
1791
+
1792
+ const userId = author . id ;
1793
+ const { autoremovals } = misc ;
1794
+
1795
+ const userHasAutoRemoval = autoremovals [ userId ] ;
1796
+ if ( ! userHasAutoRemoval ) {
1797
+ log . debug ( `User ${ userId } has no autoremoval` ) ;
1798
+ message . channel . send ( `You have no autoremoval added` ) ;
1799
+ return ;
1800
+ }
1801
+
1802
+ store . dispatch ( clearAutoRemoval ( { guildId : guild . id , userId } ) ) ;
1803
+ log . info ( `Autoremoval cleared for user ${ userId } ` ) ;
1804
+
1805
+ message . channel . send ( `<@${ userId } >, you will not be autoremoved anymore` ) ;
1806
+
1807
+ log . info ( `Exiting handleClearAutoRemove` ) ;
1808
+ } ;
0 commit comments