diff --git a/src/client.ts b/src/client.ts index 0b1f277..dd02ec5 100644 --- a/src/client.ts +++ b/src/client.ts @@ -23,7 +23,7 @@ import CacheServer from './funcs/CacheServer.js'; import fxp from 'fast-xml-parser'; import dayjs from 'dayjs'; import TSClient from './helpers/TSClient.js'; -const importconfig = ConfigHelper.loadConfig(process.argv[2] ?? 'src/config.json'); +const importconfig = ConfigHelper.loadConfig(); export default class TClient extends Discord.Client { invites: Map; diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 7fc072a..21a50fd 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -11,21 +11,20 @@ export default { if (!message.inGuild()) return (client.channels.resolve(client.config.mainServer.channels.logs) as Discord.TextChannel).send({content: `<:fish_unamused:1083675172407623711> ${MessageTool.formatMention(client.config.whitelist[0], 'user')}\n**${message.author.username}** tried to send me a DM, their message is:\`\`\`${message.content}\`\`\``, allowedMentions: {parse: ['users']}}); let automodded: boolean; - if (client.config.botSwitches.automod && !message.member.roles.cache.has(client.config.mainServer.roles.admin) && message.guildId == client.config.mainServer.id){ + if (client.config.botSwitches.automod && !message.member.roles.cache.has(client.config.mainServer.roles.admin) && message.guildId === client.config.mainServer.id){ const automodFailReason = 'msg got possibly deleted by another bot.'; if (await client.bannedWords._content.findById(Automoderator.scanMsg(message))/* && !Whitelist.includes(message.channelId) */){ automodded = true; message.delete().catch(()=>Logger.forwardToConsole('log', 'AUTOMOD-BANNEDWORDS', automodFailReason)); message.channel.send('That word isn\'t allowed here.').then(x=>setTimeout(()=>x.delete(), 10000)); - await Automoderator.repeatedMessages(client, message, 30000, 3, 'bw', '30m', 'Constant swearing'); + await Automoderator.repeatedMessages(client, message, 30000, 3, 'bw', '30m', 'Constant swears'); } else if (message.content.toLowerCase().includes('discord.gg/') && !MessageTool.isStaff(message.member as Discord.GuildMember)){ - const url = message.content.split(' ').find(x=>x.includes('discord.gg/')); - const validInvite = await client.fetchInvite(url).catch(()=>undefined); + const validInvite = await client.fetchInvite(message.content.split(' ').find(x=>x.includes('discord.gg/'))).catch(()=>null); if (validInvite && validInvite.guild?.id !== client.config.mainServer.id){ automodded = true; message.delete().catch(()=>Logger.forwardToConsole('log', 'AUTOMOD-ADVERT', automodFailReason)); message.channel.send('Please don\'t advertise other Discord servers.').then(x=>setTimeout(()=>x.delete(), 15000)); - await Automoderator.repeatedMessages(client, message, 60000, 2, 'adv', '1h', 'Discord advertisement'); + await Automoderator.repeatedMessages(client, message, 60000, 2, 'adv', '1h', 'Discord Advertisement'); } } } @@ -55,9 +54,8 @@ export default { Response.create(client, message, GeneralChatID, 'evening'); Response.create(client, message, GeneralChatID, 'night'); - CmdTrigger.registerCmds(client, message, '!!_wepanikfrfr'); - CmdTrigger.MFPwTrigger(message, '!!_farmpw'); - CmdTrigger.TriggerTest(message, '!!_test-trigger'); + CmdTrigger.registerCmds(client, message, 'wepanikfrfr'); + CmdTrigger.MFPwTrigger(message, 'farmpw'); if (message.type === 8 && message.channelId === GeneralChatID) message.channel.send({content: outgoingArrays.guildBoost[Math.floor(Math.random() * outgoingArrays.guildBoost.length)], allowedMentions: {parse: ['users']}}) if (message.mentions.members.has('309373272594579456') && !MessageTool.isStaff(message.member)) message.reply('Please don\'t tag Daggerwin, read rule 14 in <#468846117405196289>'); diff --git a/src/funcs/CmdModule.ts b/src/funcs/CmdModule.ts index d7a9361..b7765be 100644 --- a/src/funcs/CmdModule.ts +++ b/src/funcs/CmdModule.ts @@ -2,14 +2,15 @@ import Discord from 'discord.js'; import TClient from '../client.js'; export default class CmdTrigger { - protected static SenseTrigger(message:Discord.Message, trigger:string) { - return message.content.toLowerCase().startsWith(trigger) + private static readonly prefix = '!!_'; + private static SenseTrigger(message:Discord.Message, trigger:string): boolean { + return message.content.toLowerCase().startsWith(this.prefix+trigger) } static registerCmds(client:TClient, message:Discord.Message, trigger:string) { if (this.SenseTrigger(message, trigger) && client.config.whitelist.includes(message.author.id)) { (client.guilds.cache.get(message.guildId) as Discord.Guild).commands.set(client.registry) - .then(()=>message.reply('How did you manage to lose the commands??? Anyways, it\'s re-registered now.')) - .catch((e:Error)=>message.reply(`Failed to deploy slash commands:\n\`\`\`${e.message}\`\`\``)); + .then(()=>message.reply('How did you manage to lose the commands??? Anyways, it\'s re-registered now.')) + .catch((e:Error)=>message.reply(`Failed to deploy slash commands:\n\`\`\`${e.message}\`\`\``)); } } static MFPwTrigger(message:Discord.Message, trigger:string) { @@ -19,7 +20,4 @@ export default class CmdTrigger { else if (message.channelId === '1149138202662293555') return message.reply(farmPwText += '`eastfarm`') } } - static TriggerTest(message:Discord.Message, trigger:string) { - if (this.SenseTrigger(message, trigger)) return message.reply('Triggered!') - } } diff --git a/src/funcs/HookManager.ts b/src/funcs/HookManager.ts index 2dc7659..9a6e250 100644 --- a/src/funcs/HookManager.ts +++ b/src/funcs/HookManager.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import TClient from '../client.js'; -import {Config} from '../typings/interfaces'; -import {readFileSync} from 'node:fs'; -const config:Config = JSON.parse(readFileSync('src/config.json', 'utf-8')); +import ConfigHelper from '../helpers/ConfigHelper.js'; +const config = ConfigHelper.readConfig(); type ChannelList = keyof typeof config.mainServer.channels; export default class HookMgr { protected static async channelFetch(client:TClient, channel:ChannelList) { diff --git a/src/helpers/ConfigHelper.ts b/src/helpers/ConfigHelper.ts index 0ab8fb7..0dc5233 100644 --- a/src/helpers/ConfigHelper.ts +++ b/src/helpers/ConfigHelper.ts @@ -1,15 +1,18 @@ import {readFileSync} from 'node:fs'; import {Config} from '../typings/interfaces'; export default class ConfigHelper { - static loadConfig(configFile:string) { + static loadConfig() { let importconfig:Config; try { - importconfig = JSON.parse(readFileSync(configFile, 'utf8')); + importconfig = JSON.parse(readFileSync(process.argv[2] ?? 'src/config.json', 'utf8')); console.log(`Loaded the config :: ${importconfig.configName}`); } catch (e) { - console.error(`Error loading config file "${configFile}": ${e}`); + console.error(`Error loading config file "${process.argv[2] ?? 'src/config.json'}": ${e}`); process.exit(1); } return importconfig; } + static readConfig() { + return JSON.parse(readFileSync(process.argv[2] ?? 'src/config.json', 'utf8')) as Config; + } } diff --git a/src/helpers/FAQStore.ts b/src/helpers/FAQStore.ts index 6b86ec4..9a74d8f 100644 --- a/src/helpers/FAQStore.ts +++ b/src/helpers/FAQStore.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import MessageTool from './MessageTool.js'; -import {Config} from 'src/typings/interfaces'; -import {readFileSync} from 'node:fs'; -const config:Config = JSON.parse(readFileSync('src/config.json', 'utf8')); +import ConfigHelper from './ConfigHelper.js'; +const config = ConfigHelper.readConfig(); export default class FAQStore { protected static readonly errorMsg:string = 'Failed to send the message, please report to **Toast** if it continues.'; static async reply(interaction:Discord.ChatInputCommandInteraction, title:string|null, message:string, image:string|null, useEmbed:boolean=false) { diff --git a/src/helpers/FormatBytes.ts b/src/helpers/FormatBytes.ts index 16e1624..aa69f83 100644 --- a/src/helpers/FormatBytes.ts +++ b/src/helpers/FormatBytes.ts @@ -2,4 +2,4 @@ export default (bytes:number, decimals:number = 2)=>{ if (bytes === 0) return '0 Bytes'; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return parseFloat((bytes / Math.pow(1024, i)).toFixed(decimals < 0 ? 0 : decimals))+ ' ' +['Bytes', 'KB', 'MB', 'GB', 'TB'][i] -} \ No newline at end of file +} diff --git a/src/helpers/MessageTool.ts b/src/helpers/MessageTool.ts index c0b4f74..db1b45a 100644 --- a/src/helpers/MessageTool.ts +++ b/src/helpers/MessageTool.ts @@ -1,7 +1,6 @@ import Discord from 'discord.js'; -import {readFileSync} from 'node:fs'; -import {Config} from 'src/typings/interfaces'; -const config:Config = JSON.parse(readFileSync('src/config.json', 'utf8')); +import ConfigHelper from './ConfigHelper.js'; +const config = ConfigHelper.readConfig(); type RoleKeys = keyof typeof config.mainServer.roles; export default class MessageTool { diff --git a/src/helpers/UsernameHelper.ts b/src/helpers/UsernameHelper.ts index 9628384..8968da2 100644 --- a/src/helpers/UsernameHelper.ts +++ b/src/helpers/UsernameHelper.ts @@ -1,18 +1,6 @@ export default class UsernameHelper { static stripName(text: string){ - let matchesLeft = true; const dirSlash = process.platform === 'linux' ? '\/' : '\\'; - const array = text.split(dirSlash); - while (matchesLeft) { - let usersIndex = array.indexOf(process.platform === 'linux' ? 'media' : 'Users'); - if (usersIndex < 1) matchesLeft = false; - else { - let usernameIndex = usersIndex + 1; - if (array[usernameIndex].length === 0) usernameIndex += 1; - array[usernameIndex] = '・'.repeat(array[usernameIndex].length); - array[usersIndex] = process.platform === 'linux' ? 'med\u200bia' : 'Us\u200bers'; - } - return array.join(dirSlash); - } + return text.replace(/(?<=\/Users\/|\/media\/)[^/]+/g, match=>'・'.repeat(match.length)).split(dirSlash).join(dirSlash); } } diff --git a/src/index.ts b/src/index.ts index 51131ae..e34f241 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,14 +5,15 @@ client.init(); import Logger from './helpers/Logger.js'; import YTModule from './funcs/YTModule.js'; import MPModule from './funcs/MPModule.js'; +import UsernameHelper from './helpers/UsernameHelper.js'; import {Punishment} from './typings/interfaces'; import {writeFileSync, readFileSync} from 'node:fs'; // Error handler function DZ(error:Error, type:string){// Yes, I may have shiternet but I don't need to wake up to like a hundred messages or so. - if (JSON.parse(readFileSync('src/errorBlocklist.json', 'utf8')).includes(error.message)) return;// I wonder if my idea works, if not then please run me over with a bulldozer. + if (JSON.parse(readFileSync('src/errorBlocklist.json', 'utf8')).includes(error.message)) return; console.error(error); - (client.channels.resolve(client.config.mainServer.channels.errors) as Discord.TextChannel | null)?.send({embeds: [new client.embed().setColor('#560000').setTitle('Error caught!').setFooter({text: 'Error type: ' + type}).setDescription(`**Error:**\n\`\`\`${error.message}\`\`\`**Stack:**\n\`\`\`${`${error.stack}`.slice(0, 2500)}\`\`\``)]}) + (client.channels.resolve(client.config.mainServer.channels.errors) as Discord.TextChannel | null)?.send({embeds: [new client.embed().setColor('#560000').setTitle('Error caught!').setFooter({text: 'Error type: ' + type}).setDescription(`**Error:**\n\`\`\`${error.message}\`\`\`**Stack:**\n\`\`\`${`${UsernameHelper.stripName(error.stack)}`.slice(0, 2500)}\`\`\``)]}) } process.on('unhandledRejection', (error: Error)=>DZ(error, 'unhandledRejection')); process.on('uncaughtException', (error: Error)=>DZ(error, 'uncaughtException')); diff --git a/tsconfig.json b/tsconfig.json index bcf240e..2ee585e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,5 +18,5 @@ "typeRoots": [ "./src/typings" ] }, "include": [ "src/" ], - "exclude": [ ".yarn/cache", ".yarn/unplugged", ".git", "src/errorBlocklist.json", "src/config.json", "src/DB-Beta.config.json", "src/Toast-Testbot.config.json" ] + "exclude": [ ".yarn/cache", ".yarn/unplugged", ".git", "src/errorBlocklist.json", "src/config.json", "src/DB-Beta.config.json" ] }