|
| 1 | +import * as Eris from "eris"; |
| 2 | +import dbl from "dblapi.js"; |
| 3 | +import { ShoukakuHandler } from "./modules/ShoukakuHandler"; |
| 4 | +import { QueueManager } from "./modules/Queue"; |
| 5 | +import { EventHandler } from "./modules/EventHandler"; |
| 6 | +import { Spotify } from "@tuneorg/spotify"; |
| 7 | +import { CommandLoader } from "./modules/CommandHandler"; |
| 8 | +import { DatabaseManager } from "./modules/DatabaseManager"; |
| 9 | +import { BaseError } from "./abstract/BaseListener"; |
| 10 | +import { Server } from "./modules/server"; |
| 11 | +import { CommandManager } from "./modules/CommandService"; |
| 12 | +import { voiceService } from "./modules/voiceService"; |
| 13 | +import { Client } from "discord-hybrid-sharding"; |
| 14 | +import { SlashManager } from "./modules/buttonHandler"; |
| 15 | +import { CollectorManager } from "./modules/Collector"; |
| 16 | +import { sweeperManager } from "./modules/sweeperManager"; |
| 17 | +import { Context } from "./modules/Context"; |
| 18 | +import { SlashContext } from "./modules/SlashContext"; |
| 19 | +const Cluster = require("discord-hybrid-sharding"); |
| 20 | + |
| 21 | + |
| 22 | +export class BaseDiscordClient extends Eris.Client { |
| 23 | + location: string; |
| 24 | + commands: CommandLoader; |
| 25 | + commandManager: CommandManager; |
| 26 | + database: DatabaseManager; |
| 27 | + config: any; |
| 28 | + collectors: CollectorManager; |
| 29 | + cluster: Client; |
| 30 | + voiceHandler: voiceService; |
| 31 | + shoukaku: ShoukakuHandler; |
| 32 | + dbl: dbl; |
| 33 | + events: EventHandler; |
| 34 | + server?: Server; |
| 35 | + sweepers: sweeperManager; |
| 36 | + spotify: Spotify; |
| 37 | + slashManager: SlashManager; |
| 38 | + queue: QueueManager; |
| 39 | + constructor(options: any) { |
| 40 | + |
| 41 | + // Passing options for the eris client |
| 42 | + super(options.token, |
| 43 | + { |
| 44 | + firstShardID: Cluster.data.FIRST_SHARD_ID, |
| 45 | + lastShardID: Cluster.data.LAST_SHARD_ID, |
| 46 | + maxShards: Cluster.data.TOTAL_SHARDS, |
| 47 | + restMode: true, |
| 48 | + largeThreshold: 1000000, |
| 49 | + guildCreateTimeout: 1000, |
| 50 | + allowedMentions: { everyone: false, roles: false, users: false }, |
| 51 | + autoreconnect: true, |
| 52 | + intents: ["guilds", "guildVoiceStates", "guildMessages"], |
| 53 | + connectionTimeout: 40000, |
| 54 | + maxResumeAttempts: 40, |
| 55 | + messageLimit: 0, |
| 56 | + disableEvents: { |
| 57 | + "TYPING_START": true, |
| 58 | + "USER_NOTE_UPDATE": true, |
| 59 | + "CHANNEL_PINS_UPDATE": true, |
| 60 | + "MESSAGE_UPDATE": true, |
| 61 | + "RELATIONSHIP_ADD": true, |
| 62 | + "RELATIONSHIP_REMOVE": true, |
| 63 | + "GUILD_UPDATE": true, |
| 64 | + "CHANNEL_CREATE": true, |
| 65 | + "CHANNEL_UPDATE": true, |
| 66 | + "CHANNEL_DELETE": true, |
| 67 | + "CHANNEL_OVERWRITE_CREATE": true, |
| 68 | + "CHANNEL_OVERWRITE_UPDATE": true, |
| 69 | + "CHANNEL_OVERWRITE_DELETE": true, |
| 70 | + "MEMBER_KICK": true, |
| 71 | + "MEMBER_PRUNE": true, |
| 72 | + "MEMBER_BAN_ADD": true, |
| 73 | + "MEMBER_BAN_REMOVE": true, |
| 74 | + "MEMBER_UPDATE": true, |
| 75 | + "MEMBER_ROLE_UPDATE": true, |
| 76 | + "BOT_ADD ": true, |
| 77 | + "ROLE_CREATE": true, |
| 78 | + "ROLE_UPDATE": true, |
| 79 | + "ROLE_DELETE": true, |
| 80 | + "INVITE_CREATE": true, |
| 81 | + "INVITE_UPDATE ": true, |
| 82 | + "INVITE_DELETE ": true, |
| 83 | + "WEBHOOK_CREATE": true, |
| 84 | + "WEBHOOK_UPDATE": true, |
| 85 | + "WEBHOOK_DELETE": true, |
| 86 | + "EMOJI_CREATE": true, |
| 87 | + "EMOJI_UPDATE": true, |
| 88 | + "EMOJI_DELETE": true, |
| 89 | + "MESSAGE_DELETE": true, |
| 90 | + "MESSAGE_BULK_DELETE": true, |
| 91 | + "MESSAGE_PIN": true, |
| 92 | + "MESSAGE_UNPIN": true, |
| 93 | + "INTEGRATION_CREATE": true, |
| 94 | + "INTEGRATION_UPDATE": true, |
| 95 | + "INTEGRATION_DELETE": true, |
| 96 | + "STAGE_INSTANCE_CREATE": true, |
| 97 | + "STAGE_INSTANCE_UPDATE": true, |
| 98 | + "STAGE_INSTANCE_DELETE": true, |
| 99 | + "STICKER_CREATE": true, |
| 100 | + "STICKER_UPDATE": true, |
| 101 | + "STICKER_DELETE": true, |
| 102 | + "GUILD_SCHEDULED_EVENT_CREATE": true, |
| 103 | + "GUILD_SCHEDULED_EVENT_UPDATE": true, |
| 104 | + "GUILD_SCHEDULED_EVENT_DELETE": true, |
| 105 | + "THREAD_CREATE": true, |
| 106 | + "THREAD_UPDATE": true, |
| 107 | + "THREAD_DELETE": true, |
| 108 | + "APPLICATION_COMMAND_PERMISSION_UPDATE": true, |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + // Core Things // |
| 113 | + this.config = options; |
| 114 | + this.shoukaku = new ShoukakuHandler(this); |
| 115 | + this.location = process.cwd(); |
| 116 | + this.spotify = new Spotify(options.spotify); |
| 117 | + this.dbl = new dbl(options.dbl, this); |
| 118 | + this.cluster = new Cluster.Client(this); |
| 119 | + this.collectors = new CollectorManager(); |
| 120 | + |
| 121 | + |
| 122 | + // Command/Slash Manager // |
| 123 | + this.commands = new CommandLoader(this); |
| 124 | + this.commandManager = new CommandManager(this); |
| 125 | + this.slashManager = new SlashManager(this); |
| 126 | + |
| 127 | + // Listenners for events // |
| 128 | + this.voiceHandler = new voiceService(this); |
| 129 | + this.on("voiceChannelJoin", (member, newChannel) => this.voiceHandler.handle("join", member, newChannel)); |
| 130 | + this.on("voiceChannelLeave", (member, oldChannel) => this.voiceHandler.handle("leave", member, oldChannel)); |
| 131 | + this.on("voiceChannelSwitch", (member, newChannel, oldChannel) => this.voiceHandler.handle("switch", member, newChannel, oldChannel)); |
| 132 | + |
| 133 | + this.on("messageCreate", m => this.commandManager.handle(m)); |
| 134 | + this.on("interactionCreate", i => this.slashManager.handle(i)) |
| 135 | + this.on("error", (err) => { |
| 136 | + if (err.toString().includes("Connection reset by peer")) return |
| 137 | + console.log(err) |
| 138 | + }) |
| 139 | + |
| 140 | + // Database // |
| 141 | + this.database = new DatabaseManager(this); |
| 142 | + |
| 143 | + // Shoukaku ( For music ) |
| 144 | + this.queue = new QueueManager(this); |
| 145 | + // Events |
| 146 | + this.events = new EventHandler(this); |
| 147 | + |
| 148 | + if (this.cluster.maintenance) console.log("[Maitainance Mode] Cluster is in maitainance") |
| 149 | + |
| 150 | + this.sweepers = new sweeperManager(this, { |
| 151 | + sweep: ["client", "emojis", "guildCategories", "stickers", "useless", "guildMembers"], |
| 152 | + timeout: 1000 * 60 * 60 * 10, |
| 153 | + changeStatus: "*help | green-bot.app" |
| 154 | + }) |
| 155 | + } |
| 156 | + listenners(debug: boolean) { |
| 157 | + const _list = debug ? ["multipleResolves", "uncaughtException", "uncaughtExceptionMonitor", "unhandledRejection", "warning"] : ["uncaughtException", "uncaughtExceptionMonitor", "unhandledRejection"]; |
| 158 | + _list.forEach((event) => { |
| 159 | + process.on(event, new BaseError(event).handler) |
| 160 | + }) |
| 161 | + return Promise; |
| 162 | + } |
| 163 | + |
| 164 | + hasBotPerm(context: Context | SlashContext, perm: any, channelVoice?: Eris.TextVoiceChannel) { |
| 165 | + if (context.me.permissions.has("administrator")) return true; |
| 166 | + const channel = channelVoice || context.channel; |
| 167 | + let hasPerm = false; |
| 168 | + if (perm === "voiceConnect" && !this.hasBotPerm(context, "viewChannel", channelVoice)) return false |
| 169 | + const owerwrites = channel.permissionOverwrites.filter(ow => ow.id === this.user.id || context.me.roles.includes(ow.id)) |
| 170 | + if (!owerwrites.length && perm !== "viewChannel" && context.me.permissions.has(perm)) return true |
| 171 | + if (!owerwrites.length && channel.permissionsOf(context.me).has(perm)) return true |
| 172 | + owerwrites.forEach(owerwrite => { |
| 173 | + if (hasPerm === true || hasPerm === false) return; |
| 174 | + const data = owerwrite.json |
| 175 | + if (data[perm] === undefined) return |
| 176 | + if (data[perm] === true) return hasPerm = true; |
| 177 | + if (data[perm] === false) return hasPerm = false; |
| 178 | + }) |
| 179 | + |
| 180 | + if (!hasPerm && context.me.permissions.has(perm)) return true |
| 181 | + if (!hasPerm && channel.permissionsOf(context.me).has(perm)) return true |
| 182 | + |
| 183 | + return hasPerm; |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + async init() { |
| 190 | + await this.connect(); |
| 191 | + await this.database.connect(); |
| 192 | + } |
| 193 | +} |
| 194 | + |
0 commit comments