Merge pull request #343 from servous/main

TypeScript improvements
This commit is contained in:
Axel Olausson Holtenäs 2022-05-28 12:35:20 +02:00 committed by GitHub
commit 604e3a19a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 31 additions and 36 deletions

View file

@ -65,6 +65,7 @@
"husky": "8.0.1",
"jest": "28.0.0",
"lint-staged": "^12.3.7",
"nodemon": "^2.0.16",
"prettier": "^2.6.0"
},
"lint-staged": {

View file

@ -1,22 +0,0 @@
// 3rd party dependencies
import mongoose from "mongoose";
// Dependencies
import logger from "@logger";
// Configuration
import { url } from "@config/database";
export default async () => {
await mongoose.connect(url).then(async (connection) => {
logger.info(`Connected to database: ${connection.connection.name}`);
});
mongoose.connection.on("error", async (error) => {
logger.error(`${error}`);
});
mongoose.connection.on("warn", async (warning) => {
logger.warn(warning);
});
};

View file

@ -83,7 +83,7 @@ export default async (interaction: CommandInteraction) => {
`Command: ${commandName} executed in guild: ${guild?.name} (${guild?.id}) by user: ${user?.tag} (${user?.id})`
);
})
.catch(async (error: any) => {
.catch(async (error: string) => {
logger?.error(`${error}`);
return interaction.editReply({

View file

@ -1,17 +1,24 @@
// Dependencies
// @ts-ignore
import { token, clientId } from "@config/discord";
// @ts-ignore
import { devMode, guildId } from "@config/other";
import logger from "../logger";
import { Client } from "@root/types/common/discord";
import { REST } from "@discordjs/rest";
import { Routes } from "discord-api-types/v9";
import { SlashCommandBuilder } from "@discordjs/builders";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
export default async (client: Client) => {
const pluginList = [] as string[];
const pluginList: Array<RESTPostAPIApplicationCommandsJSONBody> = [];
interface IPluginData {
builder: SlashCommandBuilder;
}
await Promise.all(
client.commands.map(async (pluginData: any) => {
client.commands.map(async (pluginData: IPluginData) => {
pluginList.push(pluginData.builder.toJSON());
logger.verbose(
`Plugin is ready for deployment: ${pluginData.builder.name}`

View file

@ -1,12 +1,17 @@
import crypto from "crypto";
// @ts-ignore
import { secretKey, algorithm } from "@config/encryption";
const iv = crypto.randomBytes(16);
const encrypt = (text: any): { iv: any; content: any } => {
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
interface IEncrypt {
iv: string;
content: string;
}
const encrypt = (text: crypto.BinaryLike): IEncrypt => {
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return {
@ -15,7 +20,7 @@ const encrypt = (text: any): { iv: any; content: any } => {
};
};
const decrypt = (hash: any) => {
const decrypt = (hash: IEncrypt) => {
const decipher = crypto.createDecipheriv(
algorithm,
secretKey,

View file

@ -1,6 +1,6 @@
import logger from "@logger";
export default function sleep(milliseconds: any) {
export default function sleep(milliseconds: number) {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
logger?.silly(`Sleeping for ${milliseconds} milliseconds`);

View file

@ -45,8 +45,9 @@ export default {
const { options, guild } = interaction;
// Get options
const url = options?.getString("url");
const token = encryption.encrypt(options?.getString("token"));
const tokenData = options.getString("token");
const url = options.getString("url");
const token = tokenData && encryption.encrypt(tokenData);
// Update API credentials
await apiSchema

View file

@ -147,10 +147,12 @@ export default {
guildId: guild?.id,
});
if (!apiCredentials) return;
const api = axios?.create({
baseURL: apiCredentials?.url,
baseURL: apiCredentials.url,
headers: {
Authorization: `Bearer ${encryption.decrypt(apiCredentials?.token)}`,
Authorization: `Bearer ${encryption.decrypt(apiCredentials.token)}`,
},
});
@ -235,7 +237,7 @@ export default {
});
})
.catch(async (error: any) => {
.catch(async (error) => {
logger?.silly(`Error creating voucher. - ${error}`);
return interaction?.editReply({

View file

@ -1,7 +1,8 @@
import { Collection, Client as DJSClient } from "discord.js";
declare module "discord.js" {
export interface Client extends DJSClient {
commands: Collection<unknown, any>;
commands: Collection;
}
}