♻️ add more to tsconfig types

This commit is contained in:
Axel Olausson Holtenäs 2022-04-13 20:09:27 +02:00
parent d38621c16b
commit d0dd88b02b
No known key found for this signature in database
GPG key ID: 9347A5E873995701
22 changed files with 152 additions and 104 deletions

View file

@ -11,6 +11,7 @@ inom
inställningar inställningar
inte inte
Krediter Krediter
multistream
Nivå Nivå
omdöme omdöme
Omdöme Omdöme

View file

@ -0,0 +1,5 @@
// Discord API token
export const token = "";
// Discord API id
export const clientId = "";

View file

@ -0,0 +1,17 @@
// Dependencies
import { ColorResolvable } from "discord.js";
// Color for successfully actions
export const successColor: ColorResolvable = "#22bb33";
// Color for waiting actions
export const waitColor: ColorResolvable = "#f0ad4e";
// Color for error actions
export const errorColor: ColorResolvable = "#bb2124";
// Footer text
export const footerText = "https://github.com/ZynerOrg/xyter";
// Footer icon
export const footerIcon = "https://github.com/ZynerOrg.png";

View file

@ -0,0 +1,5 @@
// Development features
export const devMode = false;
// Development guild
export const guildId = "";

View file

@ -1,55 +1,58 @@
import { ColorResolvable, CommandInteraction } from "discord.js"; // Dependencies
import config from "../../../config.json"; import { CommandInteraction, MessageEmbed } from "discord.js";
import logger from "../../logger";
import guilds from "../../database/schemas/guild";
import tools from "../../tools"; import logger from "@logger";
import * as embed from "@config/embed";
import guildSchema from "@schemas/guild";
export default async (interaction: CommandInteraction) => { export default async (interaction: CommandInteraction) => {
if (!interaction.isCommand()) return; if (!interaction.isCommand()) return;
const { client, guild } = interaction; const { client, guild, commandName, user } = interaction;
// Get command from collection const currentCommand = client.commands.get(commandName);
const command = client.commands.get(interaction.commandName); if (!currentCommand) return;
// If command do not exist // If command do not exist
if (!command) return;
// Create guild if it does not exist already // Create guild if it does not exist already
await guilds.findOne({ guildId: guild?.id }, { new: true, upsert: true }); await guildSchema.findOne(
{ guildId: guild?.id },
{ new: true, upsert: true }
);
// Defer reply // Defer reply
await interaction.deferReply({ ephemeral: true }); await interaction.deferReply({ ephemeral: true });
try { await currentCommand
// Execute command .execute(interaction)
await command.execute(interaction, tools); .then(async () => {
return logger.debug(
`Guild: ${guild?.id} (${guild?.name}) User: ${user?.tag} executed ${commandName}`
);
})
.catch(async (error: any) => {
console.log(error);
const { commandName, user } = interaction; logger.error(
`Guild: ${guild?.id} (${guild?.name}) User: ${user?.tag} There was an error executing the command: ${commandName}`
);
return logger?.verbose( logger.error(error);
`Guild: ${guild?.id} User: ${user?.tag} executed ${commandName}`
);
} catch (e) {
// Send debug message
logger.error(e);
// Send interaction reply return interaction.editReply({
await interaction.editReply({ embeds: [
embeds: [ new MessageEmbed()
{ .setTitle("Error")
author: { .setDescription(
name: client?.user?.username, `There was an error executing the command: **${currentCommand.data.name}**.`
icon_url: client?.user?.displayAvatarURL(), )
url: "https://bot.zyner.org/", .setColor(embed.errorColor)
}, .setTimestamp(new Date())
title: "Error", .setFooter({ text: embed.footerText, iconURL: embed.footerIcon }),
description: "There was an error while executing this command!", ],
color: config.colors.error as ColorResolvable, });
timestamp: new Date(),
},
],
}); });
}
}; };

View file

@ -10,7 +10,7 @@ export default {
async execute(oldMessage: Message, newMessage: Message) { async execute(oldMessage: Message, newMessage: Message) {
const { author } = newMessage; const { author } = newMessage;
logger.silly({ oldMessage, newMessage }); logger.debug({ oldMessage, newMessage });
if (author?.bot) return; if (author?.bot) return;

View file

@ -18,7 +18,7 @@ export default {
const guilds = client.guilds.cache; const guilds = client.guilds.cache;
guilds.map(async (guild) => { guilds.map(async (guild) => {
logger.silly({ name: guild.name, members: guild.memberCount }); logger.debug({ name: guild.name, members: guild.memberCount });
}); });
}, },
}; };

View file

@ -15,7 +15,7 @@ export default async (client: Client) => {
const plugin = await import(`../plugins/${pluginName}`); const plugin = await import(`../plugins/${pluginName}`);
await client?.commands?.set(plugin?.default?.data?.name, plugin?.default); await client?.commands?.set(plugin?.default?.data?.name, plugin?.default);
logger?.silly( logger?.debug(
`Successfully loaded plugin: ${plugin?.default?.data?.name} from ${plugin.default?.metadata?.author}` `Successfully loaded plugin: ${plugin?.default?.data?.name} from ${plugin.default?.metadata?.author}`
); );
}); });

View file

@ -1,42 +1,54 @@
import { devMode, bot } from "../../config.json"; // Dependencies
import { token, clientId } from "@config/discord";
import { devMode, guildId } from "@config/other";
import logger from "../logger"; import logger from "../logger";
import fs from "fs"; import fs from "fs";
import { REST } from "@discordjs/rest"; import { REST } from "@discordjs/rest";
import { Routes } from "discord-api-types/v9"; import { Routes } from "discord-api-types/v9";
export default async () => { export default async () => {
const commands = []; fs.readdir("./src/plugins", async (error: any, plugins: any) => {
const commandFiles = fs.readdirSync("./src/plugins"); if (error) {
return logger?.error(new Error(error));
}
for (const file of commandFiles) { const pluginList = [{} as any];
// eslint-disable-next-line global-require
const command = require(`../plugins/${file}`);
commands.push(command.default.data.toJSON());
}
const rest = new REST({ version: "9" }).setToken(bot?.token); await plugins?.map(async (pluginName: any) => {
const plugin = await import(`../plugins/${pluginName}`);
await rest pluginList.push(plugin.default.data.toJSON());
.put(Routes.applicationCommands(bot?.clientId), {
body: commands, logger?.debug(
}) `Successfully deployed plugin: ${plugin?.default?.data?.name} from ${plugin.default?.metadata?.author}`
.then(async () => );
logger.info("Successfully registered application commands.")
)
.catch(async (error) => {
logger.error(error);
}); });
if (devMode) { const rest = new REST({ version: "9" }).setToken(token);
await rest await rest
.put(Routes.applicationGuildCommands(bot?.clientId, bot?.guildId), { .put(Routes.applicationCommands(clientId), {
body: commands, body: pluginList,
}) })
.then(async () => .then(async () =>
logger.info("Successfully registered guild application commands.") logger.info("Successfully registered application commands.")
) )
.catch(async (error) => { .catch(async (err: any) => {
logger.error(error); logger.error(err);
}); });
}
if (devMode) {
await rest
.put(Routes.applicationGuildCommands(clientId, guildId), {
body: pluginList,
})
.then(async () =>
logger.info("Successfully registered guild application commands.")
)
.catch(async (err: any) => {
logger.error(err);
});
}
});
}; };

View file

@ -1,17 +1,16 @@
// Dependencies // Dependencies
import "tsconfig-paths/register"; // Allows using tsconfig.json paths during runtime
import { Client, Intents } from "discord.js"; // discord.js import { Client, Intents } from "discord.js"; // discord.js
import "tsconfig-paths/register";
import locale from "@locale";
import database from "@database";
import schedules from "@schedules";
import events from "@handlers/events";
import commands from "@handlers/commands";
// Configurations // Configurations
import { bot } from "../config.json"; import { token } from "@config/discord";
import database from "@root/database";
import schedules from "./schedules";
// Handlers
import events from "./handlers/events";
import commands from "./handlers/commands";
import locale from "./handlers/locale";
const client = new Client({ const client = new Client({
intents: [ intents: [
@ -21,11 +20,11 @@ const client = new Client({
], ],
}); });
database();
locale(); locale();
database();
schedules(client); schedules(client);
commands(client); commands(client);
events(client); events(client);
client?.login(bot?.token); client?.login(token);

View file

@ -214,7 +214,7 @@ export default {
], ],
}) })
.catch(async () => .catch(async () =>
logger.verbose(`Can not send DM to user ${optionUser?.id}`) logger.debug(`Can not send DM to user ${optionUser?.id}`)
); );
// Send debug message // Send debug message

View file

@ -54,7 +54,7 @@ export default {
userDB.credits += creditsEarned; userDB.credits += creditsEarned;
await userDB?.save()?.then(async () => { await userDB?.save()?.then(async () => {
logger?.verbose(`Credits added to user: ${user?.id}`); logger?.debug(`Credits added to user: ${user?.id}`);
return interaction.editReply({ return interaction.editReply({
embeds: [ embeds: [
@ -78,7 +78,7 @@ export default {
}); });
setTimeout(async () => { setTimeout(async () => {
logger?.verbose( logger?.debug(
`Guild: ${guild?.id} User: ${ `Guild: ${guild?.id} User: ${
user?.id user?.id
} has not worked within the last ${ } has not worked within the last ${

View file

@ -69,7 +69,7 @@ export default {
}); });
}); });
logger?.verbose( logger?.debug(
`Guild: ${guild?.id} User: ${user?.id} removed ${discordChannel?.id} as a counter.` `Guild: ${guild?.id} User: ${user?.id} removed ${discordChannel?.id} as a counter.`
); );
}, },

View file

@ -130,7 +130,7 @@ export default {
// Save toUser // Save toUser
await toUser?.save()?.then(async () => { await toUser?.save()?.then(async () => {
logger?.verbose( logger?.debug(
`Guild: ${guild?.id} User: ${user?.id} gave ${ `Guild: ${guild?.id} User: ${user?.id} gave ${
discordReceiver?.id discordReceiver?.id
} ${pluralize(creditAmount, "credit")}.` } ${pluralize(creditAmount, "credit")}.`

View file

@ -118,7 +118,7 @@ export default {
// Save toUser // Save toUser
await toUser?.save()?.then(async () => { await toUser?.save()?.then(async () => {
logger?.verbose( logger?.debug(
`Guild: ${guild?.id} User: ${user?.id} set ${ `Guild: ${guild?.id} User: ${user?.id} set ${
discordUser?.id discordUser?.id
} to ${pluralize(creditAmount, "credit")}.` } to ${pluralize(creditAmount, "credit")}.`

View file

@ -136,7 +136,7 @@ export default {
// Save toUser // Save toUser
await toUser?.save()?.then(async () => { await toUser?.save()?.then(async () => {
logger?.verbose( logger?.debug(
`Guild: ${guild?.id} User: ${user?.id} set ${ `Guild: ${guild?.id} User: ${user?.id} set ${
optionUser?.id optionUser?.id
} to ${pluralize(optionAmount, "credit")}.` } to ${pluralize(optionAmount, "credit")}.`

View file

@ -1,8 +1,11 @@
// Dependencies
import { Client } from "discord.js";
import schedule from "node-schedule"; import schedule from "node-schedule";
import logger from "../logger"; import logger from "@logger";
import { Client } from "discord.js";
import shopRoles from "./jobs/shopRoles"; // Jobs
import shopRoles from "@root/schedules/jobs/shopRoles";
export default async (client: Client) => { export default async (client: Client) => {
const expression = "*/5 * * * *"; const expression = "*/5 * * * *";

View file

@ -1,32 +1,34 @@
// Dependencies
import { Client } from "discord.js"; import { Client } from "discord.js";
import logger from "../../logger"; import logger from "@logger";
import users from "../../database/schemas/user"; // Schemas
import shopRoleSchema from "../../database/schemas/shopRole"; import userSchema from "@schemas/user";
import guilds from "../../database/schemas/guild"; import shopRoleSchema from "@schemas/shopRole";
import guildSchema from "@schemas/guild";
export default async (client: Client) => { export default async (client: Client) => {
shopRoleSchema.find().then(async (shopRoles: any) => { await shopRoleSchema?.find()?.then(async (shopRoles: any) => {
shopRoles.map(async (shopRole: any) => { shopRoles?.map(async (shopRole: any) => {
const payed = new Date(shopRole?.lastPayed); const payed = new Date(shopRole?.lastPayed);
const oneHourAfterPayed = payed?.setHours(payed?.getHours() + 1); const oneHourAfterPayed = payed?.setHours(payed?.getHours() + 1);
if (new Date() > new Date(oneHourAfterPayed)) { if (new Date() > new Date(oneHourAfterPayed)) {
logger.silly( logger.debug(
`Role: ${shopRole?.roleId} Expires: ${ `Role: ${shopRole?.roleId} Expires: ${
new Date() < new Date(oneHourAfterPayed) new Date() < new Date(oneHourAfterPayed)
} Last Payed: ${shopRole?.lastPayed}` } Last Payed: ${shopRole?.lastPayed}`
); );
// Get guild object // Get guild object
const guild = await guilds?.findOne({ const guild = await guildSchema?.findOne({
guildId: shopRole?.guildId, guildId: shopRole?.guildId,
}); });
if (guild === null) return; if (guild === null) return;
const userDB = await users?.findOne({ const userDB = await userSchema?.findOne({
userId: shopRole?.userId, userId: shopRole?.userId,
guildId: shopRole?.guildId, guildId: shopRole?.guildId,
}); });
@ -41,7 +43,7 @@ export default async (client: Client) => {
shopRoleSchema shopRoleSchema
?.deleteOne({ _id: shopRole?._id }) ?.deleteOne({ _id: shopRole?._id })
?.then(async () => ?.then(async () =>
logger?.verbose(`Removed ${shopRole?._id} from collection.`) logger?.debug(`Removed ${shopRole?._id} from collection.`)
); );
return rMember?.roles?.remove(`${shopRole?.roleId}`); return rMember?.roles?.remove(`${shopRole?.roleId}`);

View file

@ -1,5 +0,0 @@
import helpers from "@root/helpers";
import config from "../../config.json";
import schemas from "../database/schemas";
export default { helpers, config, schemas };

View file

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

View file

@ -20,7 +20,13 @@
"@interface/*": ["Interfaces/*"], "@interface/*": ["Interfaces/*"],
"@root/*": ["*"], "@root/*": ["*"],
"@config/*": ["config/*"], "@config/*": ["config/*"],
"@logger": ["logger"] "@logger": ["logger"],
"@database": ["database"],
"@schedules": ["schedules"],
"@handlers/*": ["handlers/*"],
"@helpers/*": ["helpers/*"],
"@locale": ["locale"],
"@schemas/*": ["database/schemas/*"]
} }
}, },
"include": ["./src"], "include": ["./src"],