Advertisement
Guest User

Discord bot index.ts

a guest
Dec 3rd, 2020
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as Discord from "discord.js";
  2. import { IBotCommand } from "./api";
  3. import * as ConfigFile from "./config";
  4. import * as db from "quick.db";
  5.  
  6. const client: Discord.Client = new Discord.Client();
  7. //setting the econmy db up
  8. let economy = new db.table("economy")
  9.  
  10. let commands:IBotCommand[] = [];
  11. loadCommands(`${__dirname}/commands`)
  12.  
  13. client.on("ready", () => {
  14.     console.log("Logged in!")
  15.  
  16.     let allUsers = client.users.array();
  17.     //making sure everyone is in the db
  18.     for(let i = 0; i< allUsers.length; i++){
  19.         if(economy.get(allUsers[i].id) === null){
  20.             economy.set(allUsers[i].id, {money:100})
  21.         }
  22.     }
  23. })
  24.  
  25. client.on("guildMemberAdd", (member) => {
  26.     //adding new people when they join
  27.     if(economy.get(member.id) === null){
  28.         economy.set(member.id, {money:100})
  29.     }
  30. })
  31.  
  32. client.on("message", msg => {
  33.     if(msg.author.bot){return;}
  34.     if(!msg.content.startsWith(ConfigFile.config.prefix)){return;}
  35.     handleCommand(msg);
  36.     // this is where im testng the db
  37.     if(msg.content.startsWith("$money")){
  38.        console.log(economy.get(`${msg.author.id}.money`))
  39.     }
  40. })
  41.  
  42. async function handleCommand(msg:Discord.Message) {
  43.     let command = msg.content.split(' ')[0].replace(ConfigFile.config.prefix, "")
  44.     let args = msg.content.split(" ").slice(1)
  45.  
  46.     for(const commandClass of commands){
  47.         try{
  48.             if(!commandClass.isThisCommand(command)){
  49.                 continue;
  50.             }
  51.             await commandClass.runCommand(args, msg, client)
  52.         } catch(exepction){
  53.             console.log(exepction)
  54.         }
  55.     }
  56. }
  57.  
  58. function loadCommands(commandPath:string) {
  59.     if(!ConfigFile.config || (ConfigFile.config.commands as string[]).length === 0){
  60.         return;
  61.     }
  62.  
  63.     for (const commandName of ConfigFile.config.commands as string[]){
  64.         const commandClass = require(`${commandPath}/${commandName}`).default
  65.         const command = new commandClass() as IBotCommand;
  66.         commands.push(command);
  67.     }
  68. }
  69.  
  70. client.login(ConfigFile.config.token)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement