-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (136 loc) · 5.31 KB
/
index.js
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
const VERSION = "4.0.0";
const {Client, GatewayIntentBits, REST, Routes } = require('discord.js');
//REQUIRED ''IMPORTS''
global.util = require('./utilities/util.js'); //My useful utility library
global.Discord = require('discord.js'); //Required discord api
global.fs = require('fs'); //File System handler
//Main Instance of the bot, call this for everything
global.bot = new Client({
intents:[
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessageReactions
]
});
//Code for dynamic command handling
bot.commands = new Discord.Collection();
reg_cmds = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// set a new item in the Collection
// with the key as the command name and the value as the exported module
bot.commands.set(command.help.name, command);
if(command.help.register){
reg_cmds.push(command.help);
}
}
//Filesystem Handling
const trackerPath = './config/';
const nicknamesJSON = 'nicknames.json';
const configJSON = 'config.json';
var files = [nicknamesJSON, configJSON];
//Check directory and required files existance
util.fileCheck(trackerPath,files);
//Load global config
global.config = require(trackerPath+configJSON);
global.nickTracker = require(trackerPath+nicknamesJSON);
//discord bot authentication
bot.login(config.token)
.then(console.log("Bot Login"))
.catch(error => console.log("The provided token is invalid. Please check your config file in config/config.json for a valid bot token.\n" + error));
bot.on('ready', (c) => {
console.log(`${c.user.username} is online v${VERSION}`);
console.log('Registering commands...');
//Get all guilds_id on which the bot is running
const Guilds = c.guilds.cache.map(guild => guild.id);
//Register commands for each guild
Guilds.forEach(guild => {
const rest = new REST({ version:'10' }).setToken(config.token);
(async ()=>{
try{
await rest.put(
Routes.applicationGuildCommands(c.user.id+"",guild+""),
{ body: reg_cmds }
);
console.log(`Slash commands where registered for ${guild}!`);
}catch(err){
console.log(reg_cmds);
console.log(`There was an error ${err}`);
}
})();
});
});
bot.on('messageCreate', msg => {
util.checkGuild(config,msg);
let guildID = msg.guild.id
let prefix = config["guild"][guildID]['prefix'];
//Exit when incoming message does not start with specifed prefix or is sent by the bot
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
//Remove Prefix and create Array with each of the arguments
let cmdString = msg.content.substring(prefix.length);
let args = cmdString.split(/ +/);
let command;
//Check if is a dice cmd
if(cmdString.match(/^\d+/) || cmdString.match(/^[dD]/)){
command = 'legacy_roll';
}else if(cmdString.match(/^vtm/)){
command = 'legacy_vtm';
}else{
return;
}
//Exit if the command doesn't exit
if (!bot.commands.has(command)) return;
//Executing command
try {
bot.commands.get(command).execute(msg, args);
} catch (error) {
console.error(error);
util.print(msg,'ERROR','Invalid Syntax: '+error,'red');
}
});
//Slash command operations
bot.on('interactionCreate',(interaction) => {
//is slash command
if(!interaction.isChatInputCommand() && !interaction.isButton()) return;
if(interaction.isChatInputCommand()){
//check if guils exists otherwise creates in config
util.checkGuild(config,interaction);
args = {};
//arguments retrieval
bot.commands.get(interaction.commandName).help.options?.forEach( (op)=>{
if(interaction.options.get(op.name) != null){
args[op.name] = interaction.options.get(op.name).value;
}
});
bot.commands.get(interaction.commandName).execute(interaction, args);
}else if(interaction.isButton()){
bot.commands.get('soundboard').sounds(interaction);
}
});
//Welcome a new user
bot.on("guildMemberAdd", (member) => {
bot.commands.get('welcome').welcome(member);
});
//change user nickname when joining a channel
bot.on('voiceStateUpdate', (oldState, newState) => {
if(oldState.channelId != newState.channelId){
bot.commands.get('nickname').renameNickname(oldState, newState);
}
})
//Saves the new nickname when changed via discord GUI
bot.on("guildMemberUpdate", (oldMember, newMember) => {
bot.commands.get('nickname').updateNickname(oldMember, newMember);
});
//clean registered users nicknames from the deleted voice channel
bot.on("channelDelete", (channel) => {
bot.commands.get('nickname').removeChannel(channel);
});
//Clean user nicknames when leaves the guild (server)
bot.on("guildMemberRemove", (member) => {
bot.commands.get('nickname').removeMember(member);
});