Skip to content

Commit 3ef362f

Browse files
author
Taran Vohra
authored
Merge pull request #24 from taranvohra/dev
Dev
2 parents 24e4e13 + dccf45e commit 3ef362f

File tree

9 files changed

+235
-28
lines changed

9 files changed

+235
-28
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Shows the `last 10` logs of the mentioned user.
6565

6666
Generates an `invite` for your server with no expiry and unlimited uses. If such an invite already exists, it re-uses that.
6767

68-
#### Pugs
68+
### Pugs
6969

7070
**addgametype, agm** 🐱‍👤<br/>
7171
`Usage -> .addgametype name totalPlayers totalTeams`
@@ -181,6 +181,21 @@ Generates an image of top 10 puggers for the gametype (sorted by most number of
181181

182182
Outputs a summary of total number of pugs played, individual pug count(s) and a timestamp when the first pug was played
183183

184+
**addautoremove, aar** 😸<br/>
185+
`Usage -> .addautoremove expiry`
186+
187+
Automatically removes the user from all the pugs they have joined after expiry.
188+
189+
_Expiry Parameters_
190+
`m` for minutes or `h` for hours or `d` for days.
191+
192+
For example `.autoremove 30m` will automatically remove the user from all the pugs after `30 minutes`.
193+
194+
**clearautoremove, car** 😸<br/>
195+
`Usage -> .clearautoremove`
196+
197+
Clears your autoremoval request (if any).
198+
184199
**add** 🐱‍👤<br/>
185200
`Usage -> .add @mention#0000 gametype1 gametype2 etc`
186201

src/commands/pugs.ts

+16
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ const pugsCommandList: Command[] = [
234234
isPrivileged: true,
235235
needsRegisteredGuild: true,
236236
},
237+
{
238+
group,
239+
key: 'handleAddAutoRemove',
240+
aliases: ['addautoremove', 'aar'],
241+
type: 'args',
242+
isPrivileged: false,
243+
needsRegisteredGuild: true,
244+
},
245+
{
246+
group,
247+
key: 'handleClearAutoRemove',
248+
aliases: ['clearautoremove', 'car'],
249+
type: 'solo',
250+
isPrivileged: false,
251+
needsRegisteredGuild: true,
252+
},
237253
];
238254

239255
export default pugsCommandList;

src/formatting.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const formatJoinStatus = (statuses: Array<JoinStatus>) => {
8787

8888
export const formatLeaveStatus = (
8989
statuses: Array<LeaveStatus>,
90-
reason?: 'offline' | 'left'
90+
reason?: 'offline' | 'left' | 'autoremove'
9191
) => {
9292
const { left, nf, nj, username } = statuses.reduce(
9393
(acc, { name, result, pug, user }) => {
@@ -122,6 +122,8 @@ export const formatLeaveStatus = (
122122
reasonMsg = `because the user went offline ${emojis.residentsleeper}${emojis.pupcurn}`;
123123
else if (reason === 'left')
124124
reasonMsg = 'because the user left this discord server';
125+
else if (reason === 'autoremove')
126+
reasonMsg = 'because the user had autoremoval added';
125127
else reasonMsg = '';
126128

127129
return `${left.length > 0 ? `${username} left ${left} ${reasonMsg}` : ``}${

src/handlers/generalHandlers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export const handleRegisterServer: Handler = async (message, _) => {
6666
guildId: guild.id,
6767
cooldowns: {},
6868
ignoredCommandGroup: [],
69+
autoremovals: {},
6970
})
7071
);
7172
store.dispatch(initBlocks({ guildId: guild.id, list: [] }));

src/handlers/pugHandlers.ts

+124-13
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import {
1010
emojis,
1111
getRandomInt,
1212
sanitizeName,
13-
calculateBlockExpiry,
13+
calculateExpiry,
1414
isDuelPug,
1515
getRandomPickIndex,
1616
teamEmojiTypes,
17+
getPlayerIndexFromPlayerList,
1718
} from '~/utils';
1819
import {
1920
addGuildGameType,
@@ -41,6 +42,8 @@ import store, {
4142
disableCoinFlip,
4243
updateTeamEmojis,
4344
updatePickingOrder,
45+
addAutoRemoval,
46+
clearAutoRemoval,
4447
} from '~/store';
4548
import {
4649
formatPugFilledDM,
@@ -456,6 +459,9 @@ export const handleJoinGameTypes: Handler = async (
456459
`Remove pug ${toBroadcast.name} at guild ${guild.id} from store`
457460
);
458461
store.dispatch(removePug({ guildId: guild.id, name: toBroadcast.name }));
462+
store.dispatch(
463+
clearAutoRemoval({ guildId: guild.id, userId: author.id })
464+
);
459465
}
460466
}
461467

@@ -555,7 +561,13 @@ export const handleLeaveGameTypes: Handler = async (
555561

556562
const leaveMessage = formatLeaveStatus(
557563
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
559571
);
560572

561573
if (!returnMsg) {
@@ -646,9 +658,11 @@ export const handleLeaveAllGameTypes: Handler = async (message) => {
646658

647659
const cache = store.getState();
648660
const pugs = cache.pugs[guild.id];
649-
if (!pugs) return;
661+
const misc = cache.misc[guild.id];
662+
if (!pugs || !misc) return;
650663

651664
const { list } = pugs;
665+
const { autoremovals } = misc;
652666

653667
const listToLeave = list
654668
.filter((pug) => pug.players.find((p) => p.id === author.id))
@@ -661,6 +675,9 @@ export const handleLeaveAllGameTypes: Handler = async (message) => {
661675
return;
662676
}
663677

678+
if (autoremovals[author.id])
679+
store.dispatch(clearAutoRemoval({ guildId: guild.id, userId: author.id }));
680+
664681
handleLeaveGameTypes(message, listToLeave);
665682
log.info(`Exiting handleLeaveAllGameTypes`);
666683
};
@@ -716,7 +733,11 @@ export const handlePickPlayer: Handler = async (
716733
mentionedUser
717734
) => {
718735
log.info(`Entering handlePickPlayer`);
719-
const { guild, author } = message;
736+
const {
737+
guild,
738+
author,
739+
mentions: { users },
740+
} = message;
720741
if (!guild) return;
721742

722743
const cache = store.getState();
@@ -757,11 +778,15 @@ export const handlePickPlayer: Handler = async (
757778
let lastPickedPlayerIndex: number | null;
758779

759780
const canPickTwice = pickingOrder[turn + 1] === team; // next turn is same team pick
781+
const mentionedUsers = users.array();
760782

761783
// +1 because few lines down we're going to subtract -1 so we still gucci 😎
784+
const firstMentionedUser = mentionedUsers[0];
762785
const playerIndex =
763786
index === 'random'
764787
? getRandomPickIndex(forPug.players) + 1
788+
: firstMentionedUser
789+
? getPlayerIndexFromPlayerList(forPug.players, firstMentionedUser) + 1
765790
: parseInt(index);
766791
if (!playerIndex) return;
767792

@@ -786,9 +811,12 @@ export const handlePickPlayer: Handler = async (
786811
/**
787812
* Situation when captain picks two in a row
788813
*/
814+
const secondMentionedUser = mentionedUsers[1];
789815
const playerIndex2 =
790816
index2 === 'random'
791817
? getRandomPickIndex(forPug.players) + 1
818+
: secondMentionedUser
819+
? getPlayerIndexFromPlayerList(forPug.players, secondMentionedUser) + 1
792820
: parseInt(index2);
793821

794822
if (canPickTwice && playerIndex2) {
@@ -816,11 +844,9 @@ export const handlePickPlayer: Handler = async (
816844
}
817845
}
818846

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+
);
824850
message.channel.send(formatPickPlayerStatus(forPug, pickedPlayers) ?? '');
825851

826852
if (!forPug.isInPickingMode) {
@@ -1068,13 +1094,13 @@ export const handlePromoteAvailablePugs: Handler = async (message, args) => {
10681094

10691095
export const handleDecidePromoteOrPick: Handler = async (message, args) => {
10701096
log.info(`Entering handleDecidePromoteOrPick`);
1071-
const { guild, cmd } = message;
1097+
const { guild, cmd, mentions } = message;
10721098
if (!guild || !cmd) return;
10731099

10741100
// just p or promote
10751101
if (!args[0]) handlePromoteAvailablePugs(message, args);
10761102
else {
1077-
// p 4 or p siege5
1103+
// p 4 or p siege5 or p @mention
10781104
const cache = store.getState();
10791105
const pugs = cache.pugs[guild.id];
10801106
if (!pugs) return;
@@ -1085,7 +1111,11 @@ export const handleDecidePromoteOrPick: Handler = async (message, args) => {
10851111
);
10861112

10871113
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+
)
10891119
handlePickPlayer(message, args);
10901120
else handlePromoteAvailablePugs(message, args);
10911121
}
@@ -1282,6 +1312,8 @@ export const handleAdminPickPlayer: Handler = async (message, args) => {
12821312
}
12831313

12841314
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);
12851317
handlePickPlayer(message, args.slice(1), mentionedUser);
12861318
log.info(`Exiting handleAdminPickPlayer`);
12871319
};
@@ -1370,7 +1402,7 @@ export const handleAdminBlockPlayer: Handler = async (message, args) => {
13701402
const blockLength = parseInt(blockLengthString);
13711403
if (blockLength <= 0) return;
13721404

1373-
const expiry = calculateBlockExpiry(
1405+
const expiry = calculateExpiry(
13741406
blockPeriodString as 'm' | 'h' | 'd',
13751407
blockLength
13761408
);
@@ -1695,3 +1727,82 @@ export const handleAdminEnforceCustomPickingOrder: Handler = async (
16951727

16961728
log.info(`Exiting handleAdminEnforceCustomPickingOrder`);
16971729
};
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

Comments
 (0)