Skip to content

Commit

Permalink
Merge pull request #196 from v-ut-d/update-packages
Browse files Browse the repository at this point in the history
Update packages
  • Loading branch information
cm-ayf authored Jul 14, 2022
2 parents 1e15ed7 + e8fb3a2 commit 466d669
Show file tree
Hide file tree
Showing 16 changed files with 1,272 additions and 2,384 deletions.
3,300 changes: 1,245 additions & 2,055 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"devDependencies": {
"@types/jest": "^27.5.0",
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"eslint": "^8.16.0",
"@types/jest": "^28.1.5",
"@typescript-eslint/eslint-plugin": "^5.30.6",
"@typescript-eslint/parser": "^5.30.6",
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"husky": "^8.0.1",
"jest": "^27.5.1",
"lint-staged": "^12.4.1",
"jest": "^28.1.3",
"lint-staged": "^13.0.3",
"npm-run-all": "^4.1.5",
"prettier": "^2.6.2",
"prisma": "^3.14.0",
"ts-jest": "^27.1.4",
"ts-node": "^10.8.0",
"ts-node-dev": "^1.1.8",
"typescript": "^4.6.4"
"prettier": "^2.7.1",
"prisma": "^4.0.0",
"ts-jest": "^28.0.6",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
},
"license": "MIT",
"main": "dist/index.js",
Expand Down Expand Up @@ -48,10 +48,10 @@
"dependencies": {
"@cm-ayf/readenv": "^1.1.0",
"@discordjs/opus": "^0.8.0",
"@discordjs/voice": "^0.9.0",
"@prisma/client": "^3.14.0",
"@discordjs/voice": "^0.10.0",
"@prisma/client": "^4.0.0",
"axios": "^0.27.2",
"discord.js": "^13.7.0",
"discord.js": "^13.8.1",
"dotenv": "^16.0.1",
"libsodium-wrappers": "^0.7.10",
"node-openjtalk-binding": "git+https://github.com/v-ut-d/node-openjtalk-binding.git",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `dictionaryWriteRole` on the `GuildSettings` table. All the data in the column will be lost.
- You are about to drop the column `moderatorRole` on the `GuildSettings` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "GuildSettings" DROP COLUMN "dictionaryWriteRole",
DROP COLUMN "moderatorRole";
2 changes: 0 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,4 @@ model GuildSettings {
readSpeakersName Boolean @default(false)
readEmojis Boolean @default(true)
omitThreshold Int @default(200)
dictionaryWriteRole String
moderatorRole String?
}
1 change: 0 additions & 1 deletion src/classes/preprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const pillow =

const defaultGuildSettings = {
guildId: '', //unused
dictionaryWriteRole: '', //unused
readSpeakersName: false, //unused
readMultiLine: true,
readEmojis: true,
Expand Down
1 change: 0 additions & 1 deletion src/classes/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export default class Room {
},
create: {
guildId: this.guildId,
dictionaryWriteRole: this.guild.roles.everyone.id,
},
update: {},
});
Expand Down
19 changes: 0 additions & 19 deletions src/commands/dict-bulk/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { GuildSettings } from '@prisma/client';
import type { ApplicationCommandData, CommandInteraction } from 'discord.js';
import type { PermissionSetterFunction } from '..';
import * as export_ from './export';
import * as import_ from './import';

Expand All @@ -14,23 +12,6 @@ export const data: ApplicationCommandData = {
options: [export_.data, import_.data],
};

/**
* `/dict-bulk` command permission data.
*/
export const permissions: PermissionSetterFunction = (
guildSettings: GuildSettings
) => {
return guildSettings.moderatorRole
? [
{
type: 'ROLE',
id: guildSettings.moderatorRole,
permission: true,
},
]
: [];
};

/**
* handles `/dict-bulk` subcommands.
*/
Expand Down
17 changes: 0 additions & 17 deletions src/commands/dict/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import type { ApplicationCommandData, CommandInteraction } from 'discord.js';
import * as get from './get';
import * as set from './set';
import * as del from './delete';
import type { PermissionSetterFunction } from '..';
import type { GuildSettings } from '@prisma/client';

/**
* `/dict` command data.
Expand All @@ -15,21 +13,6 @@ export const data: ApplicationCommandData = {
options: [get.data, set.data, del.data],
};

/**
* `/dict` command permission data.
*/
export const permissions: PermissionSetterFunction = (
guildSettings: GuildSettings
) => {
return [
{
type: 'ROLE',
id: guildSettings.dictionaryWriteRole,
permission: true,
},
];
};

/**
* handles `/dict` subcommands.
*/
Expand Down
19 changes: 0 additions & 19 deletions src/commands/emoji-bulk/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { GuildSettings } from '@prisma/client';
import type { ApplicationCommandData, CommandInteraction } from 'discord.js';
import type { PermissionSetterFunction } from '..';
import * as export_ from './export';
import * as import_ from './import';
import * as keys from './keys';
Expand All @@ -16,23 +14,6 @@ export const data: ApplicationCommandData = {
options: [export_.data, import_.data, keys.data],
};

/**
* `/emoji-bulk` command permission data.
*/
export const permissions: PermissionSetterFunction = (
guildSettings: GuildSettings
) => {
return guildSettings.moderatorRole
? [
{
type: 'ROLE',
id: guildSettings.moderatorRole,
permission: true,
},
]
: [];
};

/**
* handles `/emoji-bulk` subcommands.
*/
Expand Down
114 changes: 0 additions & 114 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { GuildSettings } from '@prisma/client';
import type {
ApplicationCommandData,
ApplicationCommandManager,
ApplicationCommandPermissions,
Client,
CommandInteraction,
Guild,
GuildApplicationCommandManager,
} from 'discord.js';
import { Collection } from 'discord.js';
import { prisma } from '../database';
import { env } from '../utils';
import * as cancel from './cancel';
import * as dictBulk from './dict-bulk';
Expand All @@ -23,45 +20,10 @@ import * as help from './help';

interface CommandDefinition {
data: ApplicationCommandData;
permissions?: PermissionSetterFunction;
handle(interaction: CommandInteraction<'cached'>): Promise<void>;
}

export type PermissionSetterFunction = (
guildSettings: GuildSettings,
guild: Guild
) => ApplicationCommandPermissions[];

class CommandManager<Production extends boolean> {
static async #resolveGuildSettings(guild: Guild) {
let guildSettings = await prisma.guildSettings.upsert({
where: {
guildId: guild.id,
},
create: {
guildId: guild.id,
dictionaryWriteRole: guild.roles.everyone.id,
},
update: {},
});

if (
guildSettings.moderatorRole &&
!guild.roles.cache.has(guildSettings.moderatorRole)
) {
guildSettings = await prisma.guildSettings.update({
where: {
guildId: guild.id,
},
data: {
moderatorRole: null,
},
});
}

return guildSettings;
}

#commandDefinitions = new Collection<string, CommandDefinition>();

constructor(
Expand All @@ -83,13 +45,6 @@ class CommandManager<Production extends boolean> {
} else {
await (this as CommandManager<false>).#registerDevelopment(client);
}

await Promise.all(
client.guilds.cache.map(async (guild) => {
const guildSettings = await CommandManager.#resolveGuildSettings(guild);
await this.setPermission(guildSettings, guild);
})
);
}

async handle(interaction: CommandInteraction<'cached'>) {
Expand All @@ -98,32 +53,10 @@ class CommandManager<Production extends boolean> {
?.handle(interaction);
}

async setPermission(guildSettings: GuildSettings, guild: Guild) {
if (this.production) {
await (this as CommandManager<true>).#setPermissionProduction(
guildSettings,
guild
);
} else {
await (this as CommandManager<false>).#setPermissionDevelopment(
guildSettings,
guild
);
}
}

async addGuild(guild: Guild) {
if (!this.production) {
await (this as CommandManager<false>).#addGuildDevelopment(guild);
}

const guildSettings = await CommandManager.#resolveGuildSettings(guild);
await this.setPermission(guildSettings, guild);
}

async checkRole(guild: Guild) {
const guildSettings = await CommandManager.#resolveGuildSettings(guild);
await this.setPermission(guildSettings, guild);
}

async #hasCommandChanged(
Expand Down Expand Up @@ -171,55 +104,8 @@ class CommandManager<Production extends boolean> {
}
}

async #setPermissionDevelopment(
this: CommandManager<false>,
guildSettings: GuildSettings,
guild: Guild
) {
const commands = guild.commands.cache;
if (!commands) return;

await Promise.all(
commands.map(async (command) => {
// commandDefinition always exists; command comes from this source code
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { permissions } = this.#commandDefinitions.get(command.name)!;
if (!permissions) return undefined;

return command.permissions.set({
permissions: permissions(guildSettings, guild),
});
})
);
}

async #setPermissionProduction(
this: CommandManager<true>,
guildSettings: GuildSettings,
guild: Guild
) {
const { application } = guild.client;
if (!application) return;
await Promise.all(
application.commands.cache.map(async (command) => {
// commandDefinition always exists; command comes from this source code
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { permissions } = this.#commandDefinitions.get(command.name)!;
if (!permissions) return undefined;

return command.permissions.set({
guild,
permissions: permissions(guildSettings, guild),
});
})
);
}

async #addGuildDevelopment(this: CommandManager<false>, guild: Guild) {
await guild.commands.set(this.#commandDefinitions.map(({ data }) => data));

const guildSettings = await CommandManager.#resolveGuildSettings(guild);
await this.#setPermissionDevelopment(guildSettings, guild);
}
}

Expand Down
17 changes: 0 additions & 17 deletions src/commands/setting/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,10 @@ export async function handle(interaction: CommandInteraction<'cached'>) {
},
create: {
guildId: interaction.guildId,
dictionaryWriteRole: interaction.guild.roles.everyone.id,
},
update: {},
});

const moderatorRoleName = setting.moderatorRole
? `${
(await interaction.guild.roles.fetch(setting.moderatorRole)) ??
'Not Found'
}`
: 'Not set';

const dictRoleName = setting.dictionaryWriteRole
? `${
(await interaction.guild.roles.fetch(setting.dictionaryWriteRole)) ??
'Not Found'
}`
: '@everyone';

const numberOfEmojis = await prisma.emoji.count({
where: {
guildId: interaction.guildId,
Expand All @@ -60,8 +45,6 @@ export async function handle(interaction: CommandInteraction<'cached'>) {
embeds: [
new SettingMessageEmbed('get', {
setting: setting,
moderatorRoleName,
dictRoleName,
numberOfEmojis,
numberOfDictEntries,
}),
Expand Down
Loading

0 comments on commit 466d669

Please sign in to comment.