Skip to content

Commit 9fb881f

Browse files
author
Taran Vohra
authored
Merge pull request #25 from taranvohra/dev
fix autoremove bugs and messaging
2 parents 3ef362f + ed78658 commit 9fb881f

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

src/formatting.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ 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';
125+
else if (reason === 'autoremove') reasonMsg = 'because of autoremove';
127126
else reasonMsg = '';
128127

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

src/handlers/pugHandlers.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { formatDistance } from 'date-fns';
55
import { User } from 'discord.js';
66
import { Pug, Users, Pugs, GuildStats } from '~/models';
77
import {
8+
Period,
89
computePickingOrder,
910
CONSTANTS,
1011
emojis,
@@ -15,6 +16,7 @@ import {
1516
getRandomPickIndex,
1617
teamEmojiTypes,
1718
getPlayerIndexFromPlayerList,
19+
getHumanReadablePeriodName,
1820
} from '~/utils';
1921
import {
2022
addGuildGameType,
@@ -424,6 +426,9 @@ export const handleJoinGameTypes: Handler = async (
424426
toBroadcast.players.forEach((player) => {
425427
const user = message.client.users.cache.get(player.id);
426428
user?.send(DM);
429+
store.dispatch(
430+
clearAutoRemoval({ guildId: guild.id, userId: player.id })
431+
);
427432
});
428433

429434
// If 1v1/mix/deathmatch pug, there wont be live picking
@@ -459,9 +464,6 @@ export const handleJoinGameTypes: Handler = async (
459464
`Remove pug ${toBroadcast.name} at guild ${guild.id} from store`
460465
);
461466
store.dispatch(removePug({ guildId: guild.id, name: toBroadcast.name }));
462-
store.dispatch(
463-
clearAutoRemoval({ guildId: guild.id, userId: author.id })
464-
);
465467
}
466468
}
467469

@@ -1402,10 +1404,7 @@ export const handleAdminBlockPlayer: Handler = async (message, args) => {
14021404
const blockLength = parseInt(blockLengthString);
14031405
if (blockLength <= 0) return;
14041406

1405-
const expiry = calculateExpiry(
1406-
blockPeriodString as 'm' | 'h' | 'd',
1407-
blockLength
1408-
);
1407+
const expiry = calculateExpiry(blockPeriodString as Period, blockLength);
14091408

14101409
const newBlock = {
14111410
blockedOn: new Date(),
@@ -1742,7 +1741,7 @@ export const handleAddAutoRemove: Handler = async (message, args) => {
17421741
const { list } = pugs;
17431742

17441743
const userHasJoinedAtleastOnePug = list.some((pug) =>
1745-
pug.players.find((p) => p.id === userId)
1744+
pug.players.some((p) => p.id === userId)
17461745
);
17471746

17481747
if (!userHasJoinedAtleastOnePug) {
@@ -1753,6 +1752,17 @@ export const handleAddAutoRemove: Handler = async (message, args) => {
17531752
return;
17541753
}
17551754

1755+
const userIsPartOfFilledPug = list.some(
1756+
(pug) => pug.isInPickingMode && pug.players.some((p) => p.id === userId)
1757+
);
1758+
if (userIsPartOfFilledPug) {
1759+
log.debug(`User ${userId} is part of filled pug, so no autoremoval`);
1760+
message.channel.send(
1761+
`You cannot use autoremove while you're in a filled pug`
1762+
);
1763+
return;
1764+
}
1765+
17561766
const [timeframe = ''] = args;
17571767
const [autoRemoveLengthString] = timeframe.match(/[0-9]+/g) ?? [];
17581768
const [autoRemovePeriodString] = timeframe.match(/[m|h|d]/g) ?? [];
@@ -1766,15 +1776,17 @@ export const handleAddAutoRemove: Handler = async (message, args) => {
17661776
if (autoRemoveLength <= 0) return;
17671777

17681778
const expiry = calculateExpiry(
1769-
autoRemovePeriodString as 'm' | 'h' | 'd',
1779+
autoRemovePeriodString as Period,
17701780
autoRemoveLength
17711781
);
17721782

17731783
store.dispatch(addAutoRemoval({ guildId: guild.id, userId, expiry }));
17741784
log.info(`Autoremoval added for user ${userId} at ${expiry.toUTCString()}`);
17751785

17761786
message.channel.send(
1777-
`<@${userId}>, you will be autoremoved from every pug at **${expiry.toUTCString()}**`
1787+
`<@${userId}>, you will be automatically removed from every pug in **${autoRemoveLength} ${getHumanReadablePeriodName(
1788+
autoRemovePeriodString as Period
1789+
)}${autoRemoveLength > 1 ? 's' : ''}** (${expiry.toUTCString()})`
17781790
);
17791791

17801792
log.info(`Exiting handleAddAutoRemove`);

src/utils/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export const CONSTANTS = {
2626
commandGroups: ['pugs', 'queries'],
2727
};
2828

29+
export type Period = 'm' | 'h' | 'd';
30+
31+
export const PERIOD_NAMES: Record<Period, string> = {
32+
m: 'minute',
33+
h: 'hour',
34+
d: 'day',
35+
};
36+
2937
export const isGuildRegistered = (guildId: string) => {
3038
const cache = store.getState();
3139
return cache.misc[guildId] !== undefined;
@@ -186,14 +194,17 @@ export const isCommandConstraintSatified = (command: Command, cmd: string) =>
186194
? command.aliases.some((a) => command.rgx!(a).test(cmd))
187195
: command.aliases.includes(cmd);
188196

189-
export const calculateExpiry = (period: 'm' | 'h' | 'd', length: number) => {
197+
export const calculateExpiry = (period: Period, length: number) => {
190198
const expiry = new Date();
191199
if (period === 'm') expiry.setMinutes(expiry.getMinutes() + length);
192200
else if (period === 'h') expiry.setHours(expiry.getHours() + length);
193201
else expiry.setHours(expiry.getHours() + length * 24);
194202
return expiry;
195203
};
196204

205+
export const getHumanReadablePeriodName = (period: Period) =>
206+
PERIOD_NAMES[period];
207+
197208
export const getHostPortPasswordFromAddress = (
198209
address: string
199210
): { host: string; port: number; password: string } => {

0 commit comments

Comments
 (0)