Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Client, Intents, MessageEmbed } from "discord.js";
- import { BotClient, CommandInfo } from "./interfaces/BotClient";
- import { Command } from "./interfaces/Command";
- import "reflect-metadata";
- import { createConnection } from "typeorm";
- import * as fs from "fs";
- import * as path from "path";
- const client: BotClient = new Client({
- intents: [
- Intents.FLAGS.GUILDS,
- Intents.FLAGS.GUILD_MESSAGES,
- Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
- Intents.FLAGS.GUILD_BANS,
- Intents.FLAGS.GUILD_MEMBERS,
- Intents.FLAGS.GUILD_VOICE_STATES,
- ],
- });
- client.localCache = new Map();
- client.commands = new Map();
- client.config = Config;
- client.on("ready", async (client: BotClient) => {
- console.log(`Logged in as ${client.user?.tag}!`);
- client.orm = await createConnection();
- client.user?.setActivity("Minecraft");
- client.user?.setStatus("dnd");
- });
- // Read all commands from the commands directory
- let commands = fs.readdirSync(path.resolve(__dirname, "commands"));
- commands.map((file) => {
- const command: Command = require(`./commands/${file}`);
- command.aliases.map((alias) => {
- client.commands?.set(
- JSON.stringify({
- name: alias,
- isAlias: true,
- }),
- command
- );
- });
- client.commands?.set(
- JSON.stringify({
- name: command.name,
- isAlias: false,
- }),
- command
- );
- });
- // Load all listeners from the listeners directory
- const listeners = fs.readdirSync(path.resolve(__dirname, "listeners"));
- listeners.forEach((file) => {
- const listener = require(`./listeners/${file}`);
- if (listener.name && listener.execute)
- client.on(listener.name, listener.execute);
- });
- // Listening for messages and checking for commands
- client.on("messageCreate", async (message) => {
- if (message.author.bot || !message.inGuild) return;
- if (message.content.indexOf(client.config.prefix) !== 0) return;
- const args = message.content
- .slice(client.config.prefix.length)
- .trim()
- .split(/ +/g);
- const command = args.shift()!.toLowerCase();
- // Check if command is help command
- if (command === "help") {
- // Loop through all commands
- const helpEmbed = new MessageEmbed()
- .setColor("#0099ff")
- .setTitle("Help | Commands")
- .setDescription("Here is a list of all commands and their usages.\n\n")
- .setFooter({ text: "Made by SeniorStudios" });
- client.commands?.forEach((value, key) => {
- const commandInfo: CommandInfo = JSON.parse(key);
- if (commandInfo.isAlias) return;
- if (command) {
- helpEmbed.addField(
- `${client.config.prefix}${value.name}`,
- `\`\`\`${value.description}\nUsage: ${client.config.prefix}${
- value.usage
- }\nAlias: ${value.aliases.join(", ")}\`\`\``,
- false
- );
- }
- });
- await message.channel.send({ embeds: [helpEmbed] });
- return;
- }
- // Change the command into command object and check if it or alias exists, if it does execute it
- const cmd = client.commands?.get(
- JSON.stringify({
- name: command,
- isAlias: false,
- })
- );
- if (cmd) {
- await cmd.execute(message, client, args);
- }
- // Check if alias exists, if it does execute it
- const alias = client.commands?.get(
- JSON.stringify({
- name: command,
- isAlias: true,
- })
- );
- if (alias) {
- await alias.execute(message, client, args);
- }
- });
- // Login through dotenv config
- client.login(process.env.TOKEN);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement