From ac6834e4673c5c84b1c3c7847f7aa074381705dc Mon Sep 17 00:00:00 2001 From: Jonas Date: Sun, 7 Apr 2024 12:29:35 +0200 Subject: [PATCH] Added Dist for Hosting --- .gitignore | 1 - dist/config.json | 4 ++ dist/src/clearSlashCommands.js | 20 ++++++ dist/src/commands/economy/balance.js | 38 +++++++++++ dist/src/commands/economy/daily.js | 42 ++++++++++++ dist/src/commands/misc/ping.js | 12 ++++ dist/src/commands/ticket/ticket.js | 27 ++++++++ .../interactionCreate/handleCommands.js | 68 +++++++++++++++++++ dist/src/events/ready/01registerCommands.js | 47 +++++++++++++ dist/src/events/ready/changeStatus.js | 18 +++++ dist/src/events/ready/consoleLog.js | 5 ++ dist/src/handlers/eventHandler.js | 44 ++++++++++++ dist/src/index.js | 29 ++++++++ dist/src/models/User.js | 22 ++++++ dist/src/utils/areCommandsDifferent.js | 39 +++++++++++ dist/src/utils/clearSlashCommands.js | 20 ++++++ dist/src/utils/commandInterface.js | 2 + dist/src/utils/getAllFiles.js | 25 +++++++ dist/src/utils/getApplicationCommands.js | 7 ++ dist/src/utils/getLocalCommands.js | 44 ++++++++++++ 20 files changed, 513 insertions(+), 1 deletion(-) create mode 100644 dist/config.json create mode 100644 dist/src/clearSlashCommands.js create mode 100644 dist/src/commands/economy/balance.js create mode 100644 dist/src/commands/economy/daily.js create mode 100644 dist/src/commands/misc/ping.js create mode 100644 dist/src/commands/ticket/ticket.js create mode 100644 dist/src/events/interactionCreate/handleCommands.js create mode 100644 dist/src/events/ready/01registerCommands.js create mode 100644 dist/src/events/ready/changeStatus.js create mode 100644 dist/src/events/ready/consoleLog.js create mode 100644 dist/src/handlers/eventHandler.js create mode 100644 dist/src/index.js create mode 100644 dist/src/models/User.js create mode 100644 dist/src/utils/areCommandsDifferent.js create mode 100644 dist/src/utils/clearSlashCommands.js create mode 100644 dist/src/utils/commandInterface.js create mode 100644 dist/src/utils/getAllFiles.js create mode 100644 dist/src/utils/getApplicationCommands.js create mode 100644 dist/src/utils/getLocalCommands.js diff --git a/.gitignore b/.gitignore index c8fba23..307beae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ /.env -/dist /node_modules \ No newline at end of file diff --git a/dist/config.json b/dist/config.json new file mode 100644 index 0000000..578dceb --- /dev/null +++ b/dist/config.json @@ -0,0 +1,4 @@ +{ + "testServer": "995670033961336862", + "devs": ["965683547723694112"] +} diff --git a/dist/src/clearSlashCommands.js b/dist/src/clearSlashCommands.js new file mode 100644 index 0000000..3dcf0f3 --- /dev/null +++ b/dist/src/clearSlashCommands.js @@ -0,0 +1,20 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const dotenv_1 = __importDefault(require("dotenv")); +dotenv_1.default.config(); +const discord_js_1 = require("discord.js"); +const commands = {}; +const rest = new discord_js_1.REST({ version: "10" }).setToken(process.env.TOKEN || ""); +(async () => { + try { + console.log("Lösche Alle Commands!"); + await rest.put(discord_js_1.Routes.applicationCommands(process.env.CLIENT_ID || ""), { body: commands }); + console.log("Alle Commands wurden gelöscht!"); + } + catch (error) { + console.log("Es gab einen Fehler beim Zurücksetzen aller Commands"); + } +})(); diff --git a/dist/src/commands/economy/balance.js b/dist/src/commands/economy/balance.js new file mode 100644 index 0000000..85e6b4c --- /dev/null +++ b/dist/src/commands/economy/balance.js @@ -0,0 +1,38 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +const discord_js_1 = require("discord.js"); +const User_1 = __importDefault(require("../../models/User")); +const command = { + name: "balance", + description: "Zeigt deinen Aktuellen Kontostand", + onlyServerCommand: true, + options: [ + { + name: "user", + description: "Der User von dem du das Konto sehen möchtest!", + type: discord_js_1.ApplicationCommandOptionType.Mentionable, + } + ], + callback: async (client, interaction) => { + await interaction.deferReply(); + const userOption = await interaction.options.get("user")?.value?.toString(); + let targetUser = await interaction.guild?.members.fetch(interaction.user.id); + if (userOption) { + targetUser = await interaction.guild?.members.fetch(userOption); + } + const query = { + userId: targetUser?.id, + guildId: interaction.guild?.id + }; + let user = await User_1.default.findOne(query); + if (!user) { + await interaction.editReply(`${targetUser?.displayName} hat 0$ auf dem Konto!`); + } + else { + await interaction.editReply(`${targetUser?.displayName} hat ${user.balance}$ auf dem Konto!`); + } + } +}; +module.exports = command; diff --git a/dist/src/commands/economy/daily.js b/dist/src/commands/economy/daily.js new file mode 100644 index 0000000..895fc76 --- /dev/null +++ b/dist/src/commands/economy/daily.js @@ -0,0 +1,42 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +const User_1 = __importDefault(require("../../models/User")); +const dailyAmount = 1000; +const command = { + name: "daily", + description: "Sammle dein Täglichen Geld Bonus ein!", + onlyServerCommand: true, + callback: async (client, interaction) => { + try { + await interaction.deferReply(); + const query = { + userId: interaction.user.id, + guildId: interaction.guild?.id, + }; + let user = await User_1.default.findOne(query); + if (user) { + const lastDailyDate = user.lastDaily.toDateString(); + let currendate = new Date().toDateString(); + if (lastDailyDate === currendate) { + interaction.editReply("Du hast deinen Täglichen Bonus heute schon gesichtert!"); + return; + } + } + else { + user = new User_1.default({ + ...query, + lastDaily: new Date() + }); + } + user.balance += dailyAmount; + await user.save(); + interaction.editReply(`Dein Täglicher Bonus von ${dailyAmount}$ wurde Gesetzt! | Kontostand: ${user.balance}`); + } + catch (error) { + console.log("Fehler bei Daily: " + error); + } + } +}; +module.exports = command; diff --git a/dist/src/commands/misc/ping.js b/dist/src/commands/misc/ping.js new file mode 100644 index 0000000..070c9cb --- /dev/null +++ b/dist/src/commands/misc/ping.js @@ -0,0 +1,12 @@ +"use strict"; +const command = { + name: "ping", + description: "Lasse die den Ping vom Bot anzeigen!", + callback: async (client, interaction) => { + await interaction.deferReply(); + const reply = await interaction.fetchReply(); + const ping = reply.createdTimestamp - interaction.createdTimestamp; + interaction.editReply(`Pong! Client ${ping}ms | Websocket: ${client.ws.ping}`); + } +}; +module.exports = command; diff --git a/dist/src/commands/ticket/ticket.js b/dist/src/commands/ticket/ticket.js new file mode 100644 index 0000000..dc0040f --- /dev/null +++ b/dist/src/commands/ticket/ticket.js @@ -0,0 +1,27 @@ +"use strict"; +const discord_js_1 = require("discord.js"); +module.exports = { + name: "ban", + description: "Banne ein Mitglied!", + // devOnly: true, + // testOnly: true, + // deleted: false, + options: [ + { + name: "target-user", + description: "Den Nutzer den du bannen möchtest!", + required: true, + type: discord_js_1.ApplicationCommandOptionType.Mentionable, + }, + { + name: "reason", + description: "Der Grund für den Bann!", + type: discord_js_1.ApplicationCommandOptionType.String, + }, + ], + permissionsRequired: [discord_js_1.PermissionFlagsBits.Administrator], + botPermissions: [discord_js_1.PermissionFlagsBits.Administrator], + callback: (client, interaction) => { + interaction.reply(`Bna!`); + } +}; diff --git a/dist/src/events/interactionCreate/handleCommands.js b/dist/src/events/interactionCreate/handleCommands.js new file mode 100644 index 0000000..5292158 --- /dev/null +++ b/dist/src/events/interactionCreate/handleCommands.js @@ -0,0 +1,68 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const config_json_1 = require("../../../config.json"); +const getLocalCommands_1 = __importDefault(require("../../utils/getLocalCommands")); +exports.default = async (client, interaction) => { + if (!interaction.isChatInputCommand()) + return; + const localCommands = await (0, getLocalCommands_1.default)(); + try { + const commandObject = localCommands.find((cmd) => cmd.name == interaction.commandName); + if (!commandObject) + return; + if (commandObject.devOnly) { + if (!config_json_1.devs.includes(interaction.user.id)) { + interaction.reply({ + content: "Nur Developer können diesen Befehl ausführen!", + ephemeral: true, + }); + return; + } + } + if (commandObject.testOnly) { + if (!(interaction.guildId === config_json_1.testServer)) { + interaction.reply({ + content: "Diese Command kann bisher nur im Hauseigenden Test-Discord genutzt werden!", + ephemeral: true, + }); + return; + } + } + if (commandObject.permissionsRequired?.length) { + for (const permission of commandObject.permissionsRequired) { + if (!interaction.memberPermissions?.has(permission)) { + interaction.reply({ + content: "Dir fehlen die Nötigen Berechtigungen zum Ausführen dieses Commands!", + ephemeral: true, + }); + return; + } + } + } + if (commandObject.botPermissions?.length) { + for (const permission of commandObject.botPermissions) { + const bot = interaction.guild?.members.me; + if (!bot?.permissions.has(permission)) { + interaction.reply({ + content: "Ich habe nicht genug Rechte um dies Auszuführen! Benötigt: " + commandObject.botPermissions, + ephemeral: true, + }); + return; + } + } + } + if (commandObject.onlyServerCommand) { + if (!interaction.inGuild()) { + interaction.reply("Dieser Command kann nur in einem Server ausgefürt werden!"); + return; + } + } + await commandObject.callback(client, interaction); + } + catch (error) { + console.log(`Es gab einen Fehler den Comand auszuführen | ${error}`); + } +}; diff --git a/dist/src/events/ready/01registerCommands.js b/dist/src/events/ready/01registerCommands.js new file mode 100644 index 0000000..f8d5be6 --- /dev/null +++ b/dist/src/events/ready/01registerCommands.js @@ -0,0 +1,47 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const getApplicationCommands_1 = __importDefault(require("../../utils/getApplicationCommands")); +const getLocalCommands_1 = __importDefault(require("../../utils/getLocalCommands")); +const areCommandsDifferent_1 = __importDefault(require("../../utils/areCommandsDifferent")); +exports.default = async (client) => { + try { + const localCommands = await (0, getLocalCommands_1.default)(); + const applicationCommands = await (0, getApplicationCommands_1.default)(client); + for (const localCommand of localCommands) { + let { name, description, options } = localCommand; + const existingCommand = (await applicationCommands).cache.find((cmd) => cmd.name === name); + if (existingCommand) { + if (localCommand.deleted) { + (await applicationCommands).delete(existingCommand.id); + console.log(`Command ${name} gelöscht!`); + continue; + } + if ((0, areCommandsDifferent_1.default)(existingCommand, localCommand)) { + (await applicationCommands).edit(existingCommand.id, { + description, + options + }); + console.log(`Command ${name} Geändert!`); + } + } + else { + if (localCommand.deleted) { + console.log(`Überspringe registrierung von Command ${name}, da er auf geläscht gesetzt wurde!`); + continue; + } + (await applicationCommands).create({ + name, + description, + options, + }); + console.log(`Command ${name} hinzugefügt!`); + } + } + } + catch (error) { + console.log(`Es gab einen Fehler: ${error}`); + } +}; diff --git a/dist/src/events/ready/changeStatus.js b/dist/src/events/ready/changeStatus.js new file mode 100644 index 0000000..0ebde17 --- /dev/null +++ b/dist/src/events/ready/changeStatus.js @@ -0,0 +1,18 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const discord_js_1 = require("discord.js"); +const status = [make_status("Slash Commands"), make_status("/help"), make_status("by Agione")]; +exports.default = async (client) => { + client.user?.setActivity(status[0]); + setInterval(() => { + let random = Math.floor(Math.random() * status.length); + client.user?.setActivity(status[random]); + }, 10000); +}; +function make_status(name) { + return { + name: name, + type: discord_js_1.ActivityType.Streaming, + url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ" + }; +} diff --git a/dist/src/events/ready/consoleLog.js b/dist/src/events/ready/consoleLog.js new file mode 100644 index 0000000..4f75ef3 --- /dev/null +++ b/dist/src/events/ready/consoleLog.js @@ -0,0 +1,5 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = async (client) => { + console.log(`${client.user.username} ist nun Gestartet!`); +}; diff --git a/dist/src/handlers/eventHandler.js b/dist/src/handlers/eventHandler.js new file mode 100644 index 0000000..e21a8f6 --- /dev/null +++ b/dist/src/handlers/eventHandler.js @@ -0,0 +1,44 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const path_1 = __importDefault(require("path")); +const getAllFiles_1 = __importDefault(require("../utils/getAllFiles")); +exports.default = (client) => { + const eventFolders = (0, getAllFiles_1.default)(path_1.default.join(__dirname, "..", "events"), true); + for (const eventFolder of eventFolders) { + const eventFiles = (0, getAllFiles_1.default)(eventFolder); + eventFiles.sort((a, b) => a.localeCompare(b)); + const eventName = eventFolder.replace(/\\/g, "/").split("/").pop(); + client.on(eventName || "", async (arg) => { + for (const eventFile of eventFiles) { + let eventFunction = await Promise.resolve(`${eventFile}`).then(s => __importStar(require(s))); + await eventFunction.default(client, arg); + } + }); + } +}; diff --git a/dist/src/index.js b/dist/src/index.js new file mode 100644 index 0000000..5f6c6cd --- /dev/null +++ b/dist/src/index.js @@ -0,0 +1,29 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const discord_js_1 = require("discord.js"); +const eventHandler_1 = __importDefault(require("./handlers/eventHandler")); +const dotenv_1 = __importDefault(require("dotenv")); +const mongoose_1 = __importDefault(require("mongoose")); +dotenv_1.default.config(); +const client = new discord_js_1.Client({ + intents: [ + discord_js_1.IntentsBitField.Flags.Guilds, + discord_js_1.IntentsBitField.Flags.GuildMembers, + discord_js_1.IntentsBitField.Flags.GuildMessages, + discord_js_1.IntentsBitField.Flags.MessageContent, + ], +}); +(async () => { + try { + await mongoose_1.default.connect(process.env.MONGODB_URI || ""); + console.log("Datenbank verbindung Hergestellt!"); + (0, eventHandler_1.default)(client); + client.login(process.env.TOKEN); + } + catch (error) { + console.log("Es gab einen Fehler: " + error); + } +})(); diff --git a/dist/src/models/User.js b/dist/src/models/User.js new file mode 100644 index 0000000..abacc62 --- /dev/null +++ b/dist/src/models/User.js @@ -0,0 +1,22 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const mongoose_1 = require("mongoose"); +const userSchema = new mongoose_1.Schema({ + userId: { + type: String, + required: true, + }, + guildId: { + type: String, + required: true, + }, + balance: { + type: Number, + default: 0, + }, + lastDaily: { + type: Date, + required: true, + } +}); +exports.default = (0, mongoose_1.model)("User", userSchema); diff --git a/dist/src/utils/areCommandsDifferent.js b/dist/src/utils/areCommandsDifferent.js new file mode 100644 index 0000000..95007c7 --- /dev/null +++ b/dist/src/utils/areCommandsDifferent.js @@ -0,0 +1,39 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = (existingCommand, localCommand) => { + const areChoicesDifferent = (existingChoices, localChoices) => { + for (const localChoice of localChoices) { + const existingChoice = existingChoices?.find((choice) => choice.name === localChoice.name); + if (!existingChoice) { + return true; + } + if (localChoice.value !== existingChoice.value) { + return true; + } + } + return false; + }; + const areOptionsDifferent = (existingOptions, localOptions) => { + for (const localOption of localOptions) { + const existingOption = existingOptions?.find((option) => option.name === localOption.name); + if (!existingOption) { + return true; + } + if (localOption.description !== existingOption.description || + localOption.type !== existingOption.type || + (localOption.required || false) !== existingOption.required || + (localOption.choices?.length || 0) !== + (existingOption.choices?.length || 0) || + areChoicesDifferent(localOption.choices || [], existingOption.choices || [])) { + return true; + } + } + return false; + }; + if (existingCommand.description !== localCommand.description || + existingCommand.options?.length !== (localCommand.options?.length || 0) || + areOptionsDifferent(existingCommand.options, localCommand.options || [])) { + return true; + } + return false; +}; diff --git a/dist/src/utils/clearSlashCommands.js b/dist/src/utils/clearSlashCommands.js new file mode 100644 index 0000000..3dcf0f3 --- /dev/null +++ b/dist/src/utils/clearSlashCommands.js @@ -0,0 +1,20 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const dotenv_1 = __importDefault(require("dotenv")); +dotenv_1.default.config(); +const discord_js_1 = require("discord.js"); +const commands = {}; +const rest = new discord_js_1.REST({ version: "10" }).setToken(process.env.TOKEN || ""); +(async () => { + try { + console.log("Lösche Alle Commands!"); + await rest.put(discord_js_1.Routes.applicationCommands(process.env.CLIENT_ID || ""), { body: commands }); + console.log("Alle Commands wurden gelöscht!"); + } + catch (error) { + console.log("Es gab einen Fehler beim Zurücksetzen aller Commands"); + } +})(); diff --git a/dist/src/utils/commandInterface.js b/dist/src/utils/commandInterface.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/dist/src/utils/commandInterface.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/src/utils/getAllFiles.js b/dist/src/utils/getAllFiles.js new file mode 100644 index 0000000..4218c15 --- /dev/null +++ b/dist/src/utils/getAllFiles.js @@ -0,0 +1,25 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const fs_1 = __importDefault(require("fs")); +const path_1 = __importDefault(require("path")); +exports.default = (directory, foldersOnly = false) => { + let fileNames = []; + const files = fs_1.default.readdirSync(directory, { withFileTypes: true }); + for (const file of files) { + const filePath = path_1.default.join(directory, file.name); + if (foldersOnly) { + if (file.isDirectory()) { + fileNames.push(filePath); + } + } + else { + if (file.isFile()) { + fileNames.push(filePath); + } + } + } + return fileNames; +}; diff --git a/dist/src/utils/getApplicationCommands.js b/dist/src/utils/getApplicationCommands.js new file mode 100644 index 0000000..0b41e84 --- /dev/null +++ b/dist/src/utils/getApplicationCommands.js @@ -0,0 +1,7 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = async (client) => { + const applicationCommands = await client.application.commands; + await applicationCommands.fetch(); + return applicationCommands; +}; diff --git a/dist/src/utils/getLocalCommands.js b/dist/src/utils/getLocalCommands.js new file mode 100644 index 0000000..ac2ef2b --- /dev/null +++ b/dist/src/utils/getLocalCommands.js @@ -0,0 +1,44 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +const path_1 = __importDefault(require("path")); +const getAllFiles_1 = __importDefault(require("./getAllFiles")); +module.exports = async (exceptions = []) => { + let localCommands = []; + const commandCategories = (0, getAllFiles_1.default)(path_1.default.join(__dirname, "..", "commands"), true); + for (const commandCategorie of commandCategories) { + const commandFiles = (0, getAllFiles_1.default)(commandCategorie); + for (const commandFile of commandFiles) { + let commandObject = await Promise.resolve(`${commandFile}`).then(s => __importStar(require(s))); + if (exceptions.includes(commandObject.name)) { + continue; + } + localCommands.push(commandObject); + } + } + return localCommands; +};