aboutsummaryrefslogtreecommitdiff
path: root/index.js
blob: b0383f50619e398c2c463e47b0065ef95eee4dd2 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//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"
const llog = require("./src/logg")
llog.log("["+process.pid+"] running under the name "+process.title)
/**
 * Module Imports
 */

async function main(){
  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");
  let util = require("./src/util")
  const { ModalBuilder, REST, Routes, ContextMenuCommandBuilder, ApplicationCommandType } = require('discord.js');

  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);

  function update_options(scom,options){
    for(let opt of options){
      let autocomplete = opt.autocomplete!=null&&opt.autocomplete!=false;
      switch(opt.type){
        case 'string':
          scom.addStringOption(option => {
            option.setName(opt.name)
              .setDescription(opt.desc)
              .setRequired(opt.required)
              .setAutocomplete(autocomplete)
              if(!autocomplete&&opt.choices!=null&&opt.choices!=false){
                if(typeof opt.choices[0] === 'string'){
                  for(let i in opt.choices) opt.choices[i] = {name:opt.choices[i],value:opt.choices[i]}
                }
                option.setChoices(...opt.choices)
              } 
              return option;
            })
          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;
        case 'sub':
          scom.addSubcommand(subcommand => {
              subcommand
                .setName(opt.name)
                .setDescription("test")
              return update_options(subcommand, opt.options)
          })
          break;
      }
      
    }
    scom.opt = options
    return scom;
  }

  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.ModerateMembers)
            if(com.s_options!=null){
              update_options(scom,com.s_options);
            }
            scom = scom.toJSON()
            scom.command = com
            s_commands.push(scom)
          }
          if(com.c_main!=null){
            let ccom = new ContextMenuCommandBuilder()
              .setName(com.name.replace(/ /g,'-'))
              .setType(ApplicationCommandType.User)
            if(com.mod_only)
              ccom.setDefaultMemberPermissions(PermissionsBitField.Flags.ModerateMembers)
            ccom = ccom.toJSON()
            ccom.command = com
            s_commands.push(ccom)
          }
        } catch (e) {
          if(e.code=="ENOENT"){
            llog.error("[ENOENT] missing some config files:( run 'sh buildconfig.sh' to get them\nexiting~")
            process.exit(e.errno)
          }
          llog.error("["+e.code+"]"+" unexpected error:( something is wrong with the ./commands/*/* files\n****\n")
          llog.error(e)
          
          process.exit(e.errno)
        }
      }
    })
  })

  global.rest = new REST().setToken(TOKEN)
  global.c_commands = [new ContextMenuCommandBuilder()
    .setName('test')
    .setType(ApplicationCommandType.User)
    .setDefaultMemberPermissions(PermissionsBitField.Flags.ManageRoles)
]

  client.env = process.env
  global.commands = commands;
  global.s_commands = s_commands;
  global.recent_messages = [];

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

  try{
    require("./src/webui")
  } catch(e) {
    llog.error("failed loading webui:c")
  }
}
main()