1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-12-27 00:05:39 -05:00

Bot improvements

This commit is contained in:
toast-ts 2023-10-09 09:03:17 +11:00
parent c37bffe8a9
commit e3c025ad12
11 changed files with 30 additions and 45 deletions

View File

@ -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<any, any>;

View File

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

View File

@ -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!')
}
}

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -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]
}
}

View File

@ -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 {

View File

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

View File

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

View File

@ -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" ]
}