Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automod #28

Merged
merged 17 commits into from
Aug 6, 2024
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/config.json
node_modules/
logs/
build/
build/
data/
5 changes: 4 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"teamRole": "TEAM_ROLE_ID",
"devRole": "DEV_ROLE_ID",
"contributorsRole": "CONTRIBUTORS_ROLE_ID",
"supportCategory": "SUPPORT_CATEGORY_ID"
"supportCategory": "SUPPORT_CATEGORY_ID",
"autoModBypassRole": "AUTOMOD_BYPASS_ROLE_ID",
"serverId": "SERVER_ID",
"infractionLogchannel": "INFRACTION_LOG_CHANNEL"
}
7 changes: 4 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default [
sourceType: 'module',
globals: {
...globals.es2022,
...globals.node
...globals.node,
guild: 'writable'
}
},
rules: {
Expand Down Expand Up @@ -44,6 +45,7 @@ export default [
'default-case-last': 'warn',
'no-self-compare': 'error',
'no-new-wrappers': 'error',
'no-fallthrough': 'error',
'no-lone-blocks': 'error',
'no-undef-init': 'error',
'no-else-return': 'warn',
Expand All @@ -56,11 +58,10 @@ export default [
'no-multi-str': 'warn',
'no-lonely-if': 'warn',
'no-new-func': 'error',
'no-console': 'error',
camelcase: 'warn',
'no-var': 'warn',
eqeqeq: 'warn',
semi: 'error'
}
}
];
];
23 changes: 3 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
import { Client, Events, GatewayIntentBits } from 'discord.js';
import deployCommands from './src/functions/deployCommands';
import { execute } from './src/events/ready';
import { token } from './config.json';
import DiscordManager from './src/DiscordManager';
const discord = new DiscordManager();

const client: Client = new Client({
intents: [
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.Guilds
]
});

deployCommands(client);

client.on(Events.ClientReady, () => {
execute(client);
});

client.login(token);
discord.connect();
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
"discord.js": "^14.15.3",
"discord.js-docs": "^0.3.0",
"mongoose": "^8.5.2",
"ms": "^2.1.3",
"node-cron": "^3.0.3",
"winston": "^3.13.1"
},
"devDependencies": {
"@eslint/js": "^9.8.0",
"@types/eslint": "^9.6.0",
"@types/ms": "^0.7.34",
"@types/node-cron": "^3.0.11",
"@types/node": "^22.1.0",
"eslint": "^9.8.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions src/DiscordManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Client, GatewayIntentBits, Collection, REST, Routes } from 'discord.js';
import InteractionHandler from './handlers/InteractionHandler';
import MessageHandler from './handlers/MessageHandler';
import { token, serverId } from '../config.json';
import CheckPermits from './utils/CheckPermits';
import { SlashCommand } from './types/main';
import { connectDB } from './utils/mongo';
import { readdirSync } from 'fs';
import cron from 'node-cron';

class DiscordManager {
interactionHandler: InteractionHandler;
messageHandler: MessageHandler;
client?: Client;
constructor() {
this.interactionHandler = new InteractionHandler(this);
this.messageHandler = new MessageHandler(this);
}

connect(): void {
this.client = new Client({
intents: [
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.Guilds
]
});

this.deployCommands();
this.client.on('ready', () => this.ready());
this.client.on('messageCreate', (message) => this.messageHandler.onMessage(message));
this.client.on('interactionCreate', (interaction) => this.interactionHandler.onInteraction(interaction));

this.client.login(token).catch((e) => console.log(e));
}

async ready() {
if (!this.client) return;
console.log(`Logged in as ${this.client.user?.username} (${this.client.user?.id})!`);
global.guild = await this.client.guilds.fetch(serverId);
cron.schedule(`* * * * *`, () => CheckPermits());
connectDB();
}

async deployCommands(): Promise<void> {
if (!this.client) return;
this.client.commands = new Collection<string, SlashCommand>();
const commandFiles = readdirSync('./src/commands');
const commands = [];
for (const file of commandFiles) {
const command = await import(`./commands/${file}`);
commands.push(command.data.toJSON());
if (command.data.name) {
this.client.commands.set(command.data.name, command);
}
}
const rest = new REST({ version: '10' }).setToken(token);
const clientID = Buffer.from(token.split('.')[0], 'base64').toString('ascii');
await rest.put(Routes.applicationCommands(clientID), { body: commands });
console.log(`Successfully reloaded ${commands.length} application command(s).`);
}
}

export default DiscordManager;
Loading
Loading