Skip to content

Commit

Permalink
refactor utils (#235)
Browse files Browse the repository at this point in the history
* refactor: rename autobump util to utils

* refactor: rename referralUtils to utils

* refactor: rename reminder-utils into utils

* refactor: rename reputation helpers to utils

* refactor: rename serverSettings utils

* refactor: make deployCommands function only take 1 single commands param
  • Loading branch information
samhwang authored Apr 13, 2024
1 parent da2f785 commit d48514f
Show file tree
Hide file tree
Showing 44 changed files with 62 additions and 66 deletions.
2 changes: 1 addition & 1 deletion bin/autobump.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ThreadChannel } from 'discord.js';
import { Result } from 'oxide.ts';
import { getDiscordClient } from '../src/clients';
import { listAllThreads } from '../src/commands/autobump-threads/util';
import { listAllThreads } from '../src/commands/autobump-threads/utils';
import { loadEnv } from '../src/utils/load-env';
import { logger } from '../src/utils/logger';

Expand Down
4 changes: 2 additions & 2 deletions bin/broadcast-reminder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChannelType } from 'discord.js';
import { Result } from 'oxide.ts';
import { getDiscordClient } from '../src/clients';
import { formatReminderMessage, getReminderByTime, removeReminders } from '../src/commands/reminder/reminder-utils';
import { getReminderChannel } from '../src/commands/serverSettings/server-utils';
import { formatReminderMessage, getReminderByTime, removeReminders } from '../src/commands/reminder/utils';
import { getReminderChannel } from '../src/commands/serverSettings/utils';
import { getCurrentUnixTime } from '../src/utils/date-utils';
import { loadEnv } from '../src/utils/load-env';
import { logger } from '../src/utils/logger';
Expand Down
2 changes: 1 addition & 1 deletion bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const main = async () => {
// For development usage, please use `pnpm deploy:command`
logger.info('[main]: Deploying global commands');
const op = await Result.safe(
deployGlobalCommands(commandList, contextMenuCommandList, {
deployGlobalCommands([...commandList, ...contextMenuCommandList], {
token,
clientId: client.user.id,
})
Expand Down
2 changes: 1 addition & 1 deletion scripts/delete-global-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const deploy = async () => {

logger.info('[delete-global-commands]: Deleting global commands');
const op = await Result.safe(
deployGlobalCommands([], [], {
deployGlobalCommands([], {
token,
clientId,
})
Expand Down
2 changes: 1 addition & 1 deletion scripts/delete-guild-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const deploy = async () => {

logger.info('[delete-guild-commands]: Deleting guild commands');
const op = await Result.safe(
deployGuildCommands([], [], {
deployGuildCommands([], {
token,
clientId,
guildId,
Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy-guild-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const deploy = async () => {

logger.info('[deploy-guild-commands]: Deploying guild commands');
const op = await Result.safe(
deployGuildCommands(commandList, contextMenuCommandList, {
deployGuildCommands([...commandList, ...contextMenuCommandList,], {
token,
clientId,
guildId,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/autobump-threads/add-thread.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ChannelType, type ChatInputCommandInteraction, type PublicThreadChannel
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { addAutobumpThreadCommand } from './add-thread';
import { addAutobumpThread } from './util';
import { addAutobumpThread } from './utils';

vi.mock('./util');
vi.mock('./utils');
const mockAddAutobumpThread = vi.mocked(addAutobumpThread);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();
const threadId = 'thread_1234';
Expand Down
2 changes: 1 addition & 1 deletion src/commands/autobump-threads/add-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChannelType, SlashCommandSubcommandBuilder } from 'discord.js';
import { Result } from 'oxide.ts';
import { logger } from '../../utils/logger';
import type { CommandHandler, Subcommand } from '../builder';
import { addAutobumpThread } from './util';
import { addAutobumpThread } from './utils';

const data = new SlashCommandSubcommandBuilder()
.setName('add')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/autobump-threads/list-threads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { ChatInputCommandInteraction } from 'discord.js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { listAutobumpThreadsCommand } from './list-threads';
import { listThreadsByGuild } from './util';
import { listThreadsByGuild } from './utils';

vi.mock('./util');
vi.mock('./utils');
const mockListAutobumpThread = vi.mocked(listThreadsByGuild);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();
const threadId = 'thread_1234';
Expand Down
2 changes: 1 addition & 1 deletion src/commands/autobump-threads/list-threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SlashCommandSubcommandBuilder } from 'discord.js';
import { Result } from 'oxide.ts';
import { logger } from '../../utils/logger';
import type { CommandHandler, Subcommand } from '../builder';
import { listThreadsByGuild } from './util';
import { listThreadsByGuild } from './utils';

const data = new SlashCommandSubcommandBuilder().setName('list').setDescription('Show list of autobump threads');

Expand Down
4 changes: 2 additions & 2 deletions src/commands/autobump-threads/remove-thread.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { ChatInputCommandInteraction, PublicThreadChannel } from 'discord.j
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { removeAutobumpThreadCommand } from './remove-thread';
import { removeAutobumpThread } from './util';
import { removeAutobumpThread } from './utils';

vi.mock('./util');
vi.mock('./utils');
const mockRemoveAutobumpThread = vi.mocked(removeAutobumpThread);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();
const mockThread = mockDeep<PublicThreadChannel>();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/autobump-threads/remove-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SlashCommandSubcommandBuilder } from 'discord.js';
import { Result } from 'oxide.ts';
import { logger } from '../../utils/logger';
import type { CommandHandler, Subcommand } from '../builder';
import { removeAutobumpThread } from './util';
import { removeAutobumpThread } from './utils';

const data = new SlashCommandSubcommandBuilder()
.setName('remove')
Expand Down
File renamed without changes.
12 changes: 4 additions & 8 deletions src/commands/deploy-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@ const registerCommands = async ({ request, token, body }: DiscordRequestPayload)
return rest.put(request, { body });
};

export const deployGuildCommands = async (commandList: Command[], contextMenuCommandList: ContextMenuCommand[], config: DiscordRequestConfig) => {
export const deployGuildCommands = async (commandList: Array<Command | ContextMenuCommand>, config: DiscordRequestConfig) => {
const { token, clientId, guildId } = config;

const commands = [...commandList, ...contextMenuCommandList].map((cmd) => cmd.data.toJSON());
const commands = commandList.map((cmd) => cmd.data.toJSON());

const request = Routes.applicationGuildCommands(clientId, guildId);
return registerCommands({ request, token, body: commands });
};

export const deployGlobalCommands = async (
commandList: Command[],
contextMenuCommandList: ContextMenuCommand[],
config: Omit<DiscordRequestConfig, 'guildId'>
) => {
export const deployGlobalCommands = async (commandList: Array<Command | ContextMenuCommand>, config: Omit<DiscordRequestConfig, 'guildId'>) => {
const { token, clientId } = config;

const commands = [...commandList, ...contextMenuCommandList].map((cmd) => cmd.data.toJSON());
const commands = commandList.map((cmd) => cmd.data.toJSON());

const request = Routes.applicationCommands(clientId);
return registerCommands({ request, token, body: commands });
Expand Down
8 changes: 4 additions & 4 deletions src/commands/referral/referralNew.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { addDays } from 'date-fns';
import type { AutocompleteInteraction, ChatInputCommandInteraction, Guild } from 'discord.js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { captor, mockDeep, mockReset } from 'vitest-mock-extended';
import { getOrCreateUser } from '../reputation/_helpers';
import { getOrCreateUser } from '../reputation/utils';
import { DEFAULT_EXPIRY_DAYS_FROM_NOW, autocomplete, execute } from './referralNew';
import { type CreateReferralInput, createReferralCode, findExistingReferralCode } from './referralUtils';
import { services } from './services';
import { type CreateReferralInput, createReferralCode, findExistingReferralCode } from './utils';

vi.mock('./referralUtils');
vi.mock('./utils');
const mockFindExistingReferralCode = vi.mocked(findExistingReferralCode);
const mockCreateReferralCode = vi.mocked(createReferralCode);

vi.mock('../reputation/_helpers');
vi.mock('../reputation/utils');
const mockGetOrCreateUser = vi.mocked(getOrCreateUser);

const mockAutocompleteInteraction = mockDeep<AutocompleteInteraction>();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/referral/referralNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Result } from 'oxide.ts';
import { logger } from '../../utils/logger';
import type { AutocompleteHandler, CommandHandler } from '../builder';
import { parseDate } from './parseDate';
import { createReferralCode, findExistingReferralCode } from './referralUtils';
import { searchServices, services } from './services';
import { createReferralCode, findExistingReferralCode } from './utils';

export const DEFAULT_EXPIRY_DAYS_FROM_NOW = 30;

Expand Down
4 changes: 2 additions & 2 deletions src/commands/referral/referralRandom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { captor, mockDeep, mockReset } from 'vitest-mock-extended';
import { getDbClient } from '../../clients';
import { autocomplete, execute } from './referralRandom';
import { type GetAllReferralCodesForServiceInput, getAllReferralCodesForService } from './referralUtils';
import { type GetAllReferralCodesForServiceInput, getAllReferralCodesForService } from './utils';

vi.mock('../../clients');
const mockGetDbClient = vi.mocked(getDbClient);

vi.mock('./referralUtils');
vi.mock('./utils');
const mockGetAllReferralCodesForService = vi.mocked(getAllReferralCodesForService);

const mockAutocompleteInteraction = mockDeep<AutocompleteInteraction>();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/referral/referralRandom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Result } from 'oxide.ts';
import { logger } from '../../utils/logger';
import { getRandomIntInclusive } from '../../utils/random';
import type { AutocompleteHandler, CommandHandler } from '../builder';
import { getAllReferralCodesForService } from './referralUtils';
import { searchServices } from './services';
import { getAllReferralCodesForService } from './utils';

export const data = new SlashCommandSubcommandBuilder()
.setName('random')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getDbClient } from '../../clients';
import { getOrCreateUser } from '../reputation/_helpers';
import { getOrCreateUser } from '../reputation/utils';

export type CreateReferralInput = {
userId: string;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reminder/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { convertDateToEpoch } from '../../utils/date-utils';
import { execute } from './list';
import { getUserReminders } from './reminder-utils';
import { getUserReminders } from './utils';

vi.mock('./reminder-utils');
vi.mock('./utils');
const mockGetReminders = vi.mocked(getUserReminders);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/reminder/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Reminder } from '@prisma/client';
import { SlashCommandSubcommandBuilder } from 'discord.js';
import { Result } from 'oxide.ts';
import type { CommandHandler, Subcommand } from '../builder';
import { getUserReminders } from './reminder-utils';
import { getUserReminders } from './utils';

export const data = new SlashCommandSubcommandBuilder().setName('list').setDescription('Get a list of your reminders');

Expand Down
4 changes: 2 additions & 2 deletions src/commands/reminder/remind-duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { DAY_MONTH_YEAR_HOUR_MINUTE_FORMAT } from '../../utils/date-utils';
import { execute } from './remind-duration';
import { saveReminder } from './reminder-utils';
import { saveReminder } from './utils';

vi.mock('./reminder-utils');
vi.mock('./utils');
const mockSaveReminder = vi.mocked(saveReminder);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/reminder/remind-duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SlashCommandSubcommandBuilder } from 'discord.js';
import { Result } from 'oxide.ts';
import parseDuration from 'parse-duration';
import type { CommandHandler, Subcommand } from '../builder';
import { saveReminder } from './reminder-utils';
import { saveReminder } from './utils';

export const data = new SlashCommandSubcommandBuilder()
.setName('in')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reminder/remind-on-date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { convertDateToEpoch } from '../../utils/date-utils';
import { execute } from './remind-on-date';
import { saveReminder } from './reminder-utils';
import { saveReminder } from './utils';

vi.mock('./reminder-utils');
vi.mock('./utils');
const mockSaveReminder = vi.mocked(saveReminder);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/reminder/remind-on-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SlashCommandSubcommandBuilder } from 'discord.js';
import { Result } from 'oxide.ts';
import { convertDateToEpoch } from '../../utils/date-utils';
import type { CommandHandler, Subcommand } from '../builder';
import { saveReminder } from './reminder-utils';
import { saveReminder } from './utils';

export const data = new SlashCommandSubcommandBuilder()
.setName('on')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reminder/remove.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ChatInputCommandInteraction } from 'discord.js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { removeReminder } from './reminder-utils';
import { execute } from './remove';
import { removeReminder } from './utils';

vi.mock('./reminder-utils');
vi.mock('./utils');
const mockRemoveReminder = vi.mocked(removeReminder);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/reminder/remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SlashCommandSubcommandBuilder } from 'discord.js';
import { Result } from 'oxide.ts';
import type { CommandHandler, Subcommand } from '../builder';
import { removeReminder } from './reminder-utils';
import { removeReminder } from './utils';

export const data = new SlashCommandSubcommandBuilder()
.setName('delete')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reminder/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type { ChatInputCommandInteraction } from 'discord.js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { convertDateToEpoch } from '../../utils/date-utils';
import { updateReminder } from './reminder-utils';
import { execute } from './update';
import { updateReminder } from './utils';

vi.mock('./reminder-utils');
vi.mock('./utils');
const mockUpdateReminder = vi.mocked(updateReminder);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/reminder/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SlashCommandSubcommandBuilder } from 'discord.js';
import { Result } from 'oxide.ts';
import { convertDateToEpoch } from '../../utils/date-utils';
import type { CommandHandler, Subcommand } from '../builder';
import { updateReminder } from './reminder-utils';
import { updateReminder } from './utils';

export const data = new SlashCommandSubcommandBuilder()
.setName('update')
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/commands/reputation/checkReputation.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ChatInputCommandInteraction } from 'discord.js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { getOrCreateUser } from './_helpers';
import { checkReputation } from './checkReputation';
import { getOrCreateUser } from './utils';

vi.mock('./_helpers');
vi.mock('./utils');
const mockGetOrCreateUser = vi.mocked(getOrCreateUser);
const mockInteraction = mockDeep<ChatInputCommandInteraction>();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/reputation/checkReputation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ChatInputCommandInteraction, SlashCommandSubcommandBuilder } from 'discord.js';
import { logger } from '../../utils/logger';
import type { Subcommand } from '../builder';
import { getOrCreateUser } from './_helpers';
import { getOrCreateUser } from './utils';

const data = new SlashCommandSubcommandBuilder().setName('check').setDescription('Check your current rep');

Expand Down
4 changes: 2 additions & 2 deletions src/commands/reputation/giveReputation.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type ChatInputCommandInteraction, Collection, type Message, type User } from 'discord.js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { getOrCreateUser, updateRep } from './_helpers';
import { giveRepSlashCommand, thankUserInMessage } from './giveReputation';
import { getOrCreateUser, updateRep } from './utils';

vi.mock('./_helpers');
vi.mock('./utils');
const mockCreateUpdateUser = vi.mocked(getOrCreateUser);
const mockUpdateRep = vi.mocked(updateRep);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/reputation/giveReputation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ChatInputCommandInteraction, type Message, SlashCommandSubcommandBuilder } from 'discord.js';
import { logger } from '../../utils/logger';
import type { Subcommand } from '../builder';
import { getOrCreateUser, updateRep } from './_helpers';
import { getOrCreateUser, updateRep } from './utils';

const plusRep = async (fromUserId: string, toUserId: string) => {
const author = await getOrCreateUser(fromUserId);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reputation/leaderboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { faker } from '@faker-js/faker';
import { type ChatInputCommandInteraction, Collection, type GuildMember } from 'discord.js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { getRepLeaderboard } from './_helpers';
import { DEFAULT_LEADERBOARD, MAX_LEADERBOARD, getLeaderboard as getLeaderboardCommand } from './leaderboard';
import { getRepLeaderboard } from './utils';

vi.mock('./_helpers');
vi.mock('./utils');
const mockGetRepLeaderboard = vi.mocked(getRepLeaderboard);

const mockInteraction = mockDeep<ChatInputCommandInteraction<'cached'>>();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/reputation/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ChatInputCommandInteraction, SlashCommandSubcommandBuilder } from 'discord.js';
import { logger } from '../../utils/logger';
import type { Subcommand } from '../builder';
import { getRepLeaderboard } from './_helpers';
import { getRepLeaderboard } from './utils';

export const DEFAULT_LEADERBOARD = 10;
export const MAX_LEADERBOARD = 25;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reputation/setReputation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { ChatInputCommandInteraction, GuildMember, User } from 'discord.js'
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
import { isAdmin } from '../../utils/permission';
import { getOrCreateUser, updateRep } from './_helpers';
import { setReputation } from './setReputation';
import { getOrCreateUser, updateRep } from './utils';

vi.mock('./_helpers');
vi.mock('./utils');
const mockCreateUpdateUser = vi.mocked(getOrCreateUser);
const mockUpdateRep = vi.mocked(updateRep);

Expand Down
Loading

0 comments on commit d48514f

Please sign in to comment.