Skip to content

Commit

Permalink
chore: refactor & clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyvy-vi committed Oct 5, 2022
1 parent a6bf534 commit 9ec1a66
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/discord-bot/src/handlers/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EventLogTypeKey } from 'api/src/eventlog/types';
import { logEvent } from 'api/src/eventlog/utils';
import randomstring from 'randomstring';
import { CommandHandler } from 'src/interfaces/CommandHandler';
import { alreadyActivatedError } from '../utils/praiseEmbeds';
import { alreadyActivatedError } from '../utils/embeds/praiseEmbeds';

/**
* Executes command /activate
Expand Down
2 changes: 1 addition & 1 deletion packages/discord-bot/src/handlers/announce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { dmTargetMenu } from '../utils/menus/dmTargetmenu';
import { selectTargets } from '../utils/dmTargets';
import { periodSelectMenu } from '../utils/menus/periodSelectMenu';
import { notActivatedError } from '../utils/praiseEmbeds';
import { notActivatedError } from '../utils/embeds/praiseEmbeds';

/**
* Executes command /announce
Expand Down
30 changes: 12 additions & 18 deletions packages/discord-bot/src/handlers/forward.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PraiseModel } from 'api/dist/praise/entities';
import { GuildMember, cleanContent } from 'discord.js';
import { GuildMember } from 'discord.js';
import { UserModel } from 'api/dist/user/entities';
import { EventLogTypeKey } from 'api/src/eventlog/types';
import { logEvent } from 'api/src/eventlog/utils';
Expand All @@ -19,10 +18,11 @@ import {
forwardSuccess,
giverNotActivatedError,
selfPraiseWarning,
} from '../utils/praiseEmbeds';
} from '../utils/embeds/praiseEmbeds';
import { assertPraiseGiver } from '../utils/assertPraiseGiver';
import { assertPraiseAllowedInChannel } from '../utils/assertPraiseAllowedInChannel';
import { CommandHandler } from '../interfaces/CommandHandler';
import { createPraise } from '../utils/createPraise';

/**
* Execute command /firward
Expand Down Expand Up @@ -70,8 +70,8 @@ export const forwardHandler: CommandHandler = async (

const receivers = interaction.options.getString('receivers');
const receiverData = {
validReceiverIds: receivers?.match(/<@!([0-9]+)>/g),
undefinedReceivers: receivers?.match(/@([a-z0-9]+)/gi),
validReceiverIds: receivers?.match(/<@!?([0-9]+)>/g),
undefinedReceivers: receivers?.match(/[^<]@([a-z0-9]+)/gi),
roleMentions: receivers?.match(/<@&([0-9]+)>/g),
};

Expand Down Expand Up @@ -117,8 +117,6 @@ export const forwardHandler: CommandHandler = async (
(u) => u
);

const guildChannel = await guild.channels.fetch(channel?.id || '');

for (const receiver of Receivers) {
const receiverAccount = await getUserAccount(receiver);

Expand All @@ -131,17 +129,13 @@ export const forwardHandler: CommandHandler = async (
);
}
}
const praiseObj = await PraiseModel.create({
reason: reason,
reasonRealized: cleanContent(reason, channel),
giver: giverAccount._id,
forwarder: forwarderAccount._id,
sourceId: `DISCORD:${guild.id}:${interaction.channelId}`,
sourceName: `DISCORD:${encodeURIComponent(
guild.name
)}:${encodeURIComponent(guildChannel?.name || '')}`,
receiver: receiverAccount._id,
});
const praiseObj = await createPraise(
interaction,
giverAccount,
receiverAccount,
reason,
forwarderAccount
);

await logEvent(
EventLogTypeKey.PRAISE,
Expand Down
39 changes: 19 additions & 20 deletions packages/discord-bot/src/handlers/praise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PraiseModel } from 'api/dist/praise/entities';
import { EventLogTypeKey } from 'api/src/eventlog/types';
import { logEvent } from 'api/src/eventlog/utils';
import logger from 'jet-logger';
import { GuildMember, User, cleanContent } from 'discord.js';
import { GuildMember, User } from 'discord.js';
import { settingValue } from 'api/dist/shared/settings';
import {
dmError,
Expand All @@ -16,11 +16,12 @@ import {
undefinedReceiverWarning,
selfPraiseWarning,
firstTimePraiserInfo,
} from '../utils/praiseEmbeds';
} from '../utils/embeds/praiseEmbeds';
import { assertPraiseGiver } from '../utils/assertPraiseGiver';
import { assertPraiseAllowedInChannel } from '../utils/assertPraiseAllowedInChannel';
import { CommandHandler } from '../interfaces/CommandHandler';
import { getUserAccount } from '../utils/getUserAccount';
import { createPraise } from '../utils/createPraise';

/**
* Execute command /praise
Expand Down Expand Up @@ -53,6 +54,7 @@ export const praiseHandler: CommandHandler = async (
undefinedReceivers: receivers?.match(/[^<]@([a-z0-9]+)/gi),
roleMentions: receivers?.match(/<@&([0-9]+)>/g),
};

if (
!receivers ||
receivers.length === 0 ||
Expand All @@ -69,12 +71,12 @@ export const praiseHandler: CommandHandler = async (
return;
}

const userAccount = await getUserAccount(member as GuildMember);
const giverAccount = await getUserAccount(member as GuildMember);
const praiseItemsCount = await PraiseModel.countDocuments({
giver: userAccount._id,
giver: giverAccount._id,
});

if (!userAccount.user) {
if (!giverAccount.user) {
await interaction.editReply(await notActivatedError());
return;
}
Expand All @@ -91,15 +93,14 @@ export const praiseHandler: CommandHandler = async (
)) as boolean;

let warnSelfPraise = false;
if (!selfPraiseAllowed && receiverIds.includes(userAccount.accountId)) {
if (!selfPraiseAllowed && receiverIds.includes(giverAccount.accountId)) {
warnSelfPraise = true;
receiverIds.splice(receiverIds.indexOf(userAccount.accountId), 1);
receiverIds.splice(receiverIds.indexOf(giverAccount.accountId), 1);
}
const Receivers = (await guild.members.fetch({ user: receiverIds })).map(
(u) => u
);

const guildChannel = await guild.channels.fetch(channel?.id || '');
for (const receiver of Receivers) {
const receiverAccount = await getUserAccount(receiver);

Expand All @@ -112,22 +113,20 @@ export const praiseHandler: CommandHandler = async (
);
}
}
const praiseObj = await PraiseModel.create({
reason: reason,
reasonRealized: cleanContent(reason, channel),
giver: userAccount._id,
sourceId: `DISCORD:${guild.id}:${interaction.channelId}`,
sourceName: `DISCORD:${encodeURIComponent(
guild.name
)}:${encodeURIComponent(guildChannel?.name || '')}`,
receiver: receiverAccount._id,
});

const praiseObj = await createPraise(
interaction,
giverAccount,
receiverAccount,
reason
);

if (praiseObj) {
await logEvent(
EventLogTypeKey.PRAISE,
'Created a new praise from discord',
{
userAccountId: userAccount._id,
userAccountId: giverAccount._id,
}
);

Expand All @@ -141,7 +140,7 @@ export const praiseHandler: CommandHandler = async (
praised.push(receiverAccount.accountId);
} else {
logger.err(
`Praise not registered for [${userAccount.accountId}] -> [${receiverAccount.accountId}] for [${reason}]`
`Praise not registered for [${giverAccount.accountId}] -> [${receiverAccount.accountId}] for [${reason}]`
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/discord-bot/src/handlers/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { UserAccountModel } from 'api/dist/useraccount/entities';
import { CommandInteraction, GuildMember } from 'discord.js';
import { UserState } from '../interfaces/UserState';
import { getUserAccount } from '../utils/getUserAccount';
import { getStateEmbed } from '../utils/stateEmbed';
import { getStateEmbed } from '../utils/embeds/stateEmbed';
import { assertPraiseGiver } from '../utils/assertPraiseGiver';
import { dmError } from '../utils/praiseEmbeds';
import { dmError } from '../utils/embeds/praiseEmbeds';

/**
* Execute command /whoami
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { settingValue } from 'api/dist/shared/settings';
const getChannelId = (channel: TextBasedChannel): string => {
return channel.type === ChannelType.PublicThread ||
channel.type === ChannelType.PrivateThread ||
channel.type === ChannelType.GuildAnnouncement
channel.type === ChannelType.AnnouncementThread
? channel?.parent?.id || channel.id
: channel.id;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/discord-bot/src/utils/assertPraiseGiver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CacheType, CommandInteraction, GuildMember } from 'discord.js';
import { settingValue } from 'api/dist/shared/settings';
import { dmError, praiseRoleError } from '../utils/praiseEmbeds';
import { dmError, praiseRoleError } from './embeds/praiseEmbeds';

/**
* Check if user has discord role PRAISE_GIVER_ROLE_ID if required,
Expand Down
39 changes: 39 additions & 0 deletions packages/discord-bot/src/utils/createPraise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { PraiseModel } from 'api/dist/praise/entities';
import { PraiseDocument } from 'api/dist/praise/types';
import { UserAccountDocument } from 'api/dist/useraccount/types';
import {
ChannelType,
ChatInputCommandInteraction,
cleanContent,
} from 'discord.js';

export const createPraise = async (
interaction: ChatInputCommandInteraction,
giverAccount: UserAccountDocument,
receiverAccount: UserAccountDocument,
reason: string,
forwarderAccount?: UserAccountDocument
): Promise<PraiseDocument | undefined> => {
const { channel, guild } = interaction;
if (!channel || !guild || channel.type === ChannelType.DM) return;

const channelName =
(channel.type === ChannelType.PublicThread ||
channel.type === ChannelType.AnnouncementThread ||
channel.type === ChannelType.PrivateThread) &&
channel.parent
? `${channel.parent.name}/${channel.name}`
: channel.name;
const praiseData = {
reason: reason,
reasonRealized: cleanContent(reason, channel),
giver: giverAccount._id,
forwarder: forwarderAccount?._id || undefined,
sourceId: `DISCORD:${guild.id}:${interaction.channelId}`,
sourceName: `DISCORD:${encodeURIComponent(guild.name)}:${encodeURIComponent(
channelName
)}`,
receiver: receiverAccount._id,
};
return await PraiseModel.create(praiseData);
};

0 comments on commit 9ec1a66

Please sign in to comment.