Advertisement
daysling

Untitled

May 15th, 2022
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Client, Intents, MessageEmbed } from "discord.js";
  2. import { BotClient, CommandInfo } from "./interfaces/BotClient";
  3. import { Command } from "./interfaces/Command";
  4. import "reflect-metadata";
  5. import { createConnection } from "typeorm";
  6. import * as fs from "fs";
  7. import * as path from "path";
  8.  
  9. const client: BotClient = new Client({
  10.   intents: [
  11.     Intents.FLAGS.GUILDS,
  12.     Intents.FLAGS.GUILD_MESSAGES,
  13.     Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
  14.     Intents.FLAGS.GUILD_BANS,
  15.     Intents.FLAGS.GUILD_MEMBERS,
  16.     Intents.FLAGS.GUILD_VOICE_STATES,
  17.   ],
  18. });
  19.  
  20. client.localCache = new Map();
  21. client.commands = new Map();
  22. client.config = Config;
  23.  
  24. client.on("ready", async (client: BotClient) => {
  25.   console.log(`Logged in as ${client.user?.tag}!`);
  26.   client.orm = await createConnection();
  27.   client.user?.setActivity("Minecraft");
  28.   client.user?.setStatus("dnd");
  29.  
  30. });
  31.  
  32. // Read all commands from the commands directory
  33. let commands = fs.readdirSync(path.resolve(__dirname, "commands"));
  34.  
  35. commands.map((file) => {
  36.   const command: Command = require(`./commands/${file}`);
  37.   command.aliases.map((alias) => {
  38.     client.commands?.set(
  39.       JSON.stringify({
  40.         name: alias,
  41.         isAlias: true,
  42.       }),
  43.       command
  44.     );
  45.   });
  46.   client.commands?.set(
  47.     JSON.stringify({
  48.       name: command.name,
  49.       isAlias: false,
  50.     }),
  51.     command
  52.   );
  53. });
  54. // Load all listeners from the listeners directory
  55. const listeners = fs.readdirSync(path.resolve(__dirname, "listeners"));
  56. listeners.forEach((file) => {
  57.   const listener = require(`./listeners/${file}`);
  58.   if (listener.name && listener.execute)
  59.     client.on(listener.name, listener.execute);
  60. });
  61.  
  62. // Listening for messages and checking for commands
  63. client.on("messageCreate", async (message) => {
  64.   if (message.author.bot || !message.inGuild) return;
  65.   if (message.content.indexOf(client.config.prefix) !== 0) return;
  66.   const args = message.content
  67.     .slice(client.config.prefix.length)
  68.     .trim()
  69.     .split(/ +/g);
  70.   const command = args.shift()!.toLowerCase();
  71.   // Check if command is help command
  72.   if (command === "help") {
  73.     // Loop through all commands
  74.     const helpEmbed = new MessageEmbed()
  75.       .setColor("#0099ff")
  76.       .setTitle("Help | Commands")
  77.       .setDescription("Here is a list of all commands and their usages.\n\n")
  78.       .setFooter({ text: "Made by SeniorStudios" });
  79.     client.commands?.forEach((value, key) => {
  80.       const commandInfo: CommandInfo = JSON.parse(key);
  81.       if (commandInfo.isAlias) return;
  82.       if (command) {
  83.         helpEmbed.addField(
  84.           `${client.config.prefix}${value.name}`,
  85.           `\`\`\`${value.description}\nUsage: ${client.config.prefix}${
  86.             value.usage
  87.           }\nAlias: ${value.aliases.join(", ")}\`\`\``,
  88.           false
  89.         );
  90.       }
  91.     });
  92.     await message.channel.send({ embeds: [helpEmbed] });
  93.     return;
  94.   }
  95.   // Change the command into command object and check if it or alias exists, if it does execute it
  96.   const cmd = client.commands?.get(
  97.     JSON.stringify({
  98.       name: command,
  99.       isAlias: false,
  100.     })
  101.   );
  102.  
  103.   if (cmd) {
  104.     await cmd.execute(message, client, args);
  105.   }
  106.   // Check if alias exists, if it does execute it
  107.   const alias = client.commands?.get(
  108.     JSON.stringify({
  109.       name: command,
  110.       isAlias: true,
  111.     })
  112.   );
  113.   if (alias) {
  114.     await alias.execute(message, client, args);
  115.   }
  116. });
  117.  
  118. // Login through dotenv config
  119. client.login(process.env.TOKEN);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement