2021-09-18 05:19:53 -07:00
|
|
|
import {Client, Guild, GuildMember, Intents, Interaction, Message, TextChannel} from "discord.js";
|
2021-09-14 09:56:11 -07:00
|
|
|
|
|
|
|
|
import Logger from "../libs/Logger";
|
|
|
|
|
import Environment from "./Environment";
|
|
|
|
|
|
2021-09-15 04:10:50 -07:00
|
|
|
import Discord_Core from "../discord/Core";
|
2021-09-18 11:15:22 -07:00
|
|
|
import Discord_Settings from "../discord/Settings";
|
2021-09-15 04:10:50 -07:00
|
|
|
import Discord_Ping from "../discord/Ping";
|
|
|
|
|
import Discord_Help from "../discord/Help";
|
|
|
|
|
import Discord_Invite from "../discord/Invite";
|
|
|
|
|
import Discord_Say from "../discord/Say";
|
2021-09-18 06:28:41 -07:00
|
|
|
import Discord_InteractionManager from "../discord/InteractionManager";
|
2021-09-15 04:10:50 -07:00
|
|
|
import Discord_MembershipScreening from "../discord/MembershipScreening";
|
2021-09-14 09:56:11 -07:00
|
|
|
import Cache from "./Cache";
|
|
|
|
|
|
|
|
|
|
class Discord {
|
|
|
|
|
|
|
|
|
|
public client: Client;
|
|
|
|
|
private loaded_module: any;
|
|
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
|
this.client = new Client({
|
|
|
|
|
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
|
2021-09-15 04:10:50 -07:00
|
|
|
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS]
|
2021-09-14 09:56:11 -07:00
|
|
|
});
|
|
|
|
|
this.loaded_module = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public init(): void {
|
|
|
|
|
Logger.info('Logging in to discord');
|
|
|
|
|
this.client.login(Environment.get().DISCORD_TOKEN);
|
|
|
|
|
|
2021-09-15 04:10:50 -07:00
|
|
|
|
|
|
|
|
// TODO: Improve this
|
2021-09-14 09:56:11 -07:00
|
|
|
this.loaded_module["Core"] = new Discord_Core();
|
2021-09-18 11:15:22 -07:00
|
|
|
this.loaded_module["Discord_Settings"] = new Discord_Settings();
|
|
|
|
|
this.loaded_module["Discord_Ping"] = new Discord_Ping();
|
2021-09-15 04:10:50 -07:00
|
|
|
this.loaded_module["Discord_Help"] = new Discord_Help();
|
|
|
|
|
this.loaded_module["Discord_Invite"] = new Discord_Invite();
|
2021-09-18 06:28:41 -07:00
|
|
|
this.loaded_module["Discord_InteractionManager"] = new Discord_InteractionManager();
|
2021-09-15 04:10:50 -07:00
|
|
|
this.loaded_module["Discord_Say"] = new Discord_Say();
|
|
|
|
|
this.loaded_module["MembershipScreening"] = new Discord_MembershipScreening();
|
2021-09-14 09:56:11 -07:00
|
|
|
|
|
|
|
|
for(const module in this.loaded_module) {
|
|
|
|
|
let thisModule = this.loaded_module[module];
|
|
|
|
|
|
|
|
|
|
if (typeof thisModule.init === "function")
|
|
|
|
|
thisModule.init();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 05:19:53 -07:00
|
|
|
// On bot logged in
|
2021-09-14 09:56:11 -07:00
|
|
|
this.client.on("ready", () => {
|
|
|
|
|
for(const module in this.loaded_module) {
|
|
|
|
|
let thisModule = this.loaded_module[module];
|
|
|
|
|
|
|
|
|
|
if (typeof thisModule.ready === "function")
|
|
|
|
|
thisModule.ready();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-18 05:19:53 -07:00
|
|
|
// Member join guild event to modules
|
2021-09-15 04:10:50 -07:00
|
|
|
this.client.on("guildMemberAdd", (member: GuildMember) => {
|
|
|
|
|
for(const module in this.loaded_module) {
|
|
|
|
|
let thisModule = this.loaded_module[module];
|
|
|
|
|
|
|
|
|
|
if (typeof thisModule.guildMemberAdd === "function")
|
|
|
|
|
thisModule.guildMemberAdd(member);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-18 05:19:53 -07:00
|
|
|
// Interaction create event to modules
|
2021-09-15 04:10:50 -07:00
|
|
|
this.client.on("interactionCreate", (interaction: Interaction) => {
|
|
|
|
|
for(const module in this.loaded_module) {
|
|
|
|
|
let thisModule = this.loaded_module[module];
|
|
|
|
|
|
|
|
|
|
if (typeof thisModule.interactionCreate === "function")
|
|
|
|
|
thisModule.interactionCreate(interaction);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-18 05:19:53 -07:00
|
|
|
// Message create event to modules
|
2021-09-15 04:10:50 -07:00
|
|
|
this.client.on("messageCreate", (message: Message) => {
|
2021-09-14 09:56:11 -07:00
|
|
|
for(const module in this.loaded_module) {
|
|
|
|
|
let thisModule = this.loaded_module[module];
|
|
|
|
|
|
|
|
|
|
if (typeof thisModule.messageCreate === "function")
|
|
|
|
|
thisModule.messageCreate(message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-18 05:19:53 -07:00
|
|
|
// Joined guild event to modules
|
|
|
|
|
this.client.on("guildCreate", (guild : Guild) => {
|
|
|
|
|
for(const module in this.loaded_module) {
|
|
|
|
|
let thisModule = this.loaded_module[module];
|
|
|
|
|
|
|
|
|
|
if (typeof thisModule.guildCreate === "function")
|
|
|
|
|
thisModule.guildCreate(guild);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-14 09:56:11 -07:00
|
|
|
// Handling commands
|
2021-09-15 04:10:50 -07:00
|
|
|
this.client.on("messageCreate", async (message: Message) => {
|
2021-09-14 09:56:11 -07:00
|
|
|
|
|
|
|
|
// TODO: Handle DMs commands soon
|
|
|
|
|
if(!(message.channel instanceof TextChannel)) return;
|
2021-09-15 06:05:30 -07:00
|
|
|
if(message.author.bot) return;
|
2021-09-14 09:56:11 -07:00
|
|
|
|
|
|
|
|
if(typeof message.guild?.id === 'undefined') return;
|
|
|
|
|
|
|
|
|
|
let GuildCache = await Cache.getGuild(message.guild.id);
|
|
|
|
|
if(typeof GuildCache === 'undefined' || typeof GuildCache.prefix === 'undefined') return;
|
|
|
|
|
|
|
|
|
|
if(!message.content.startsWith(GuildCache.prefix)) return;
|
|
|
|
|
|
2021-09-18 11:15:22 -07:00
|
|
|
let cachedPrefix = GuildCache.prefix;
|
|
|
|
|
|
2021-09-14 09:56:11 -07:00
|
|
|
let args = message.content.split(" ");
|
2021-09-18 11:15:22 -07:00
|
|
|
|
|
|
|
|
let skipIndex = 0;
|
|
|
|
|
if((cachedPrefix.split(" ")[0] !== cachedPrefix)) {
|
|
|
|
|
//skipIndex = (cachedPrefix.split(" ").length) - (args.length - (cachedPrefix.split(" ").length));
|
|
|
|
|
skipIndex = (cachedPrefix.split(" ").length) - 1;
|
|
|
|
|
|
|
|
|
|
//if(/^[A-Za-z0-9_.]+$/.test(cachedPrefix.split(" ")[cachedPrefix.split(" ").length - 1]))
|
|
|
|
|
skipIndex++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if(/^[A-Za-z0-9_.]+$/.test(cachedPrefix.charAt(cachedPrefix.length - 1))) {
|
|
|
|
|
if(![
|
|
|
|
|
'!','@','#','$','%','^','&','*','(',')','-','=','_','+','\\','/','<','>','[',']','{','}','`','"',"'",',','.','~','|',';',':','?'
|
|
|
|
|
].includes(cachedPrefix.charAt(cachedPrefix.length - 1))) {
|
|
|
|
|
cachedPrefix = cachedPrefix + " ";
|
|
|
|
|
// if(!/^[A-Za-z0-9_.]+$/)
|
|
|
|
|
skipIndex++;
|
|
|
|
|
} else if (
|
|
|
|
|
(cachedPrefix.startsWith('<@!') && cachedPrefix.endsWith('>')) ||
|
|
|
|
|
cachedPrefix.startsWith('<:') && cachedPrefix.endsWith('>') ||
|
|
|
|
|
cachedPrefix.startsWith('<a:') && cachedPrefix.endsWith('>') ||
|
|
|
|
|
cachedPrefix.startsWith('<#') && cachedPrefix.endsWith('>')
|
|
|
|
|
) {
|
|
|
|
|
cachedPrefix = cachedPrefix + " ";
|
|
|
|
|
skipIndex++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(typeof args[0 + skipIndex] === 'undefined') return;
|
|
|
|
|
|
|
|
|
|
let command = args[0 + skipIndex].replace(cachedPrefix, '');
|
|
|
|
|
|
|
|
|
|
if(command === '') return;
|
|
|
|
|
|
|
|
|
|
for(let i = 0; i < (skipIndex + 1); i++) {
|
|
|
|
|
args.shift();
|
|
|
|
|
}
|
2021-09-14 09:56:11 -07:00
|
|
|
args = args.filter(e => e !== '');
|
|
|
|
|
|
|
|
|
|
if(args.length === 0)
|
|
|
|
|
args = [];
|
|
|
|
|
|
|
|
|
|
for(const module in this.loaded_module) {
|
|
|
|
|
let thisModule = this.loaded_module[module];
|
|
|
|
|
|
|
|
|
|
if (typeof thisModule.onCommand === "function")
|
|
|
|
|
thisModule.onCommand(command, args, message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-18 03:51:14 -07:00
|
|
|
// Handling mentions
|
|
|
|
|
this.client.on("messageCreate", async (message: Message) => {
|
|
|
|
|
|
|
|
|
|
// TODO: Handle DMs commands soon
|
|
|
|
|
if(!(message.channel instanceof TextChannel)) return;
|
|
|
|
|
if(message.author.bot) return;
|
|
|
|
|
|
|
|
|
|
if(typeof message.guild?.id === 'undefined') return;
|
|
|
|
|
if(!message.mentions.users) return;
|
|
|
|
|
|
|
|
|
|
if(message.mentions.users.first()?.id !== this.client.user?.id) return;
|
|
|
|
|
if(!message.content.startsWith(`<@!${this.client.user?.id}>`)) return;
|
|
|
|
|
|
|
|
|
|
let args = message.content.split(" ");
|
|
|
|
|
let command = args[1];
|
|
|
|
|
|
|
|
|
|
args.shift();
|
|
|
|
|
args.shift();
|
|
|
|
|
args = args.filter(e => e !== '');
|
|
|
|
|
|
|
|
|
|
if(args.length === 0)
|
|
|
|
|
args = [];
|
|
|
|
|
|
|
|
|
|
for(const module in this.loaded_module) {
|
|
|
|
|
let thisModule = this.loaded_module[module];
|
|
|
|
|
|
|
|
|
|
if (typeof thisModule.onCommand === "function")
|
|
|
|
|
thisModule.onCommand(command, args, message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-14 09:56:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default new Discord();
|