aboutsummaryrefslogtreecommitdiff
path: root/index.js
blob: 787ef52eef892d36b19d76f9c69c9a8a4df43f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//process mods redirection
const fs = require("fs");
var access = fs.createWriteStream('./log');
process.stdout.write = process.stderr.write  = (function(write) {
  return function(string, encoding, fd) {
      write.apply(process.stdout, arguments)
      access.write(string)
  }
})(process.stdout.write)
process.title = "sns\-chan"
console.log("["+process.pid+"] running under the name "+process.title)
/**
 * Module Imports
 */
const { Client, Collection, EmbedBuilder, ActivityType, GatewayIntentBits, Partials, Events, SlashCommandBuilder, PermissionsBitField} = require("discord.js");
const Discord = require('discord.js');
const dotenv = require("dotenv").config();
const TOKEN = process.env.TOKEN;
const path = require("path");
let db = require("./src/db");

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildBans,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildVoiceStates,
    GatewayIntentBits.GuildMessageReactions,
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.DirectMessageReactions,
    GatewayIntentBits.DirectMessageTyping,
    //GatewayIntentBits.GuildPresences
  ],
  partials: [Partials.Message, Partials.Channel, Partials.Reaction, Partials.User]
});

client.login(TOKEN);

let commands = []
let s_commands = []
fs.readdirSync("./commands/").forEach(folder => {
  fs.readdirSync("./commands/"+folder).forEach(file => {
    if(path.extname(file)==".js"){
      try{
        let com = require("./commands/"+folder+"/"+file)
        com.last_command = {};
        commands.push(com)
        if(com.s_main!=null){
          let scom = new SlashCommandBuilder()
            .setName(com.name.replace(/ /g,'-'))
            .setDescription(com.config.desc)
          if(com.mod_only)
            scom.setDefaultMemberPermissions(PermissionsBitField.Flags.KickMembers)
          if(com.s_options!=null){
            for(let opt of com.s_options){
              switch(opt.type){
                case 'string':
                  scom.addStringOption(option =>
                    option.setName(opt.name)
                      .setDescription(opt.desc)
                      .setRequired(opt.required)
                      .setAutocomplete(opt.autocomplete!=null&&opt.autocomplete!=false))
                  break;
                case 'bool': case 'boolean':
                  scom.addBooleanOption(option =>
                    option.setName(opt.name)
                      .setDescription(opt.desc)
                      .setRequired(opt.required))
                  break;
                case 'channel':
                  scom.addChannelOption(option =>
                  option.setName(opt.name)
                    .setDescription(opt.desc)
                    .setRequired(opt.required))
                  break;
                case 'user':
                  scom.addUserOption(option =>
                  option.setName(opt.name)
                    .setDescription(opt.desc)
                    .setRequired(opt.required))
                  break;
                case 'role':
                  scom.addRoleOption(option =>
                  option.setName(opt.name)
                    .setDescription(opt.desc)
                    .setRequired(opt.required))
                  break;
                case 'attachment':
                  scom.addAttachmentOption(option =>
                  option.setName(opt.name)
                    .setDescription(opt.desc)
                    .setRequired(opt.required))
                  break;
              }
              
            }
            scom.opt = com.s_options
          }
          scom = scom.toJSON()
          scom.command = com
          s_commands.push(scom)
        }
      } catch (e) {
        if(e.code=="ENOENT"){
          console.log("[ENOENT] missing some config files:( run 'sh buildconfig.sh' to get them\nexiting~")
          process.exit(e.errno)
        }
        console.log("["+e.code+"]"+" unexpected error:( something is wrong with the ./commands/*/* files\n****\n")
        console.log(e)
        
        process.exit(e.errno)
      }
    }
  })
})

client.env = process.env
global.commands = commands;
global.s_commands = s_commands;

fs.readdirSync("./events/").forEach(file => {
  if(path.extname(file)==".js")
    require("./events/"+file).main(client,Discord)
})

try{
  require("./src/webui")
} catch(e) {
  console.log("failed loading webui:c")
}