Skip to content

Commit

Permalink
feat: create jail module (#97)
Browse files Browse the repository at this point in the history
* refacto: move index.js higher

* feat(jail): basic implementation of user context menu

* refacto: change file name convention

* feat: handle most cases and sends to jail

* feat: add auto move back to channel

* feat(logs): add permissions to error message

Now, the error message will include the specific permission that is missing,
making it easier for users to resolve the issue.
out of scope but had to be done.
But too lazy to go to develop and come back to this branch

* chore: update adroi.d.ea dependency to version 1.3.0

* Update deploy-commands.ts
  • Loading branch information
adan-ea authored Jun 20, 2024
1 parent a09de14 commit a314e23
Show file tree
Hide file tree
Showing 83 changed files with 489 additions and 198 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ RUN addgroup -S adroidea && adduser -S adan -G adroidea
RUN apk add --no-cache tzdata
ENV TZ=Europe/Paris

RUN mkdir -p /usr/src/adroid
WORKDIR /usr/src/adroid
RUN mkdir -p /usr/adroid/src
WORKDIR /usr/adroid/src

COPY package.json /usr/src/adroid
COPY package.json /usr/adroid/src
RUN npm install --omit=dev --ignore-scripts

COPY dist /usr/src/adroid
COPY dist /usr/adroid/src

USER adan
CMD ["node", "index.js"]
12 changes: 6 additions & 6 deletions src/index.ts → index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import DiscordClient from './client';
import DiscordClient from './src/client';
import IORedis from 'ioredis';
import Logger from './utils/logger';
import Logger from './src/utils/logger';
import { Partials } from 'discord.js';
import dotenv from 'dotenv';
import mongoose from 'mongoose';
Expand All @@ -19,7 +19,7 @@ export const client: any = new DiscordClient({
partials: [Partials.Channel]
});

const filePath = path.join(__dirname, 'handlers/module.handler.js');
const filePath = path.join(__dirname, 'src/handlers/module.handler.js');
import(filePath).then(handler => handler.default(client));

mongoose.set('strictQuery', false);
Expand All @@ -39,14 +39,14 @@ mongoose

export const connection = new IORedis({
host: process.env.REDIS_HOST,
port: 6379
port: 6379,
maxRetriesPerRequest: null
})
.on('connect', () => {
Logger.info('🔴 Redis connected');
})
.on('error', (error: any) => {
if (error.code === 'ECONNREFUSED') return;
else Logger.error(`Redis error:`, error);
Logger.error(`Redis error:`, error);
});

client.login(process.env.TOKEN);
Expand Down
21 changes: 17 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
]
},
"dependencies": {
"adroi.d.ea": "^1.1.0",
"adroi.d.ea": "^1.3.0",
"ansis": "^1.5.5",
"bullmq": "^3.12.0",
"discord.js": "^14.15.3",
Expand Down
3 changes: 3 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IDiscordClient {
buttons: Collection<string, any>;
modals: Collection<string, any>;
selectMenus: Collection<string, any>;
contextMenus: Collection<string, any>;
cooldowns: Collection<string, any>;
tempVoice: Collection<string, ItempVoiceSettings>;
}
Expand All @@ -19,6 +20,7 @@ export default class DiscordClient extends Client implements IDiscordClient {
public buttons: Collection<string, any>;
public modals: Collection<string, any>;
public selectMenus: Collection<string, any>;
public contextMenus: Collection<string, any>;
public cooldowns: Collection<string, any>;
public tempVoice: Collection<string, ItempVoiceSettings>;

Expand All @@ -28,6 +30,7 @@ export default class DiscordClient extends Client implements IDiscordClient {
this.buttons = new Collection();
this.modals = new Collection();
this.selectMenus = new Collection();
this.contextMenus = new Collection();
this.cooldowns = new Collection();
this.tempVoice = new Collection();
}
Expand Down
1 change: 1 addition & 0 deletions src/deploy-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const regCMD = async (clientId: string) => {

const categoryFolders = [
path.join(__dirname, 'modules/core/commands'),
path.join(__dirname, 'modules/jail/components/contextMenus'),
//path.join(__dirname, 'modules/logs/commands'),
path.join(__dirname, 'modules/qotd/commands'),
//path.join(__dirname, 'modules/scheduledEvents/commands'),
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/component.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ const handleSubComponent = async (

if (stat.isDirectory()) {
result += await handleSubComponent(client, subCompPath, file);
} else if (['Btn.js', 'Menu.js', 'Modal.js'].some(extension => file.endsWith(extension))) {
} else if (
['.button.js', 'Menu.js', '.modal.js'].some(extension => file.endsWith(extension))
) {
const { default: component } = await import(filePath);

const hasWarning = checkComponentOptions(component, filePath);
Expand Down
75 changes: 61 additions & 14 deletions src/handlers/task.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export type TaskFunction = () => void;
* @returns A Promise that resolves to the number of tasks handled.
*/
export const handleTask = async (taskPath: string): Promise<number> => {
let result = 0;
const files = fs.readdirSync(taskPath);
let result = 0;

for (const file of files) {
const filePath = path.join(taskPath, file);
Expand All @@ -20,21 +20,68 @@ export const handleTask = async (taskPath: string): Promise<number> => {
if (stat.isDirectory()) {
result += await handleTask(filePath);
} else if (file.endsWith('.js')) {
try {
const { default: taskFunction } = await import(filePath);

if (taskFunction) {
await taskFunction();
result++;
} else {
Logger.warn(`File: ${file}`);
Logger.warn(`Task function not found. Skipping...`);
}
} catch (error: any) {
Logger.error(`Error importing task from file: ${file}`, error);
}
result += await processFile(file, filePath);
}
}

return result;
};

/**
* Processes a single file based on its type.
* @param file - The name of the file.
* @param filePath - The full path to the file.
* @returns A Promise that resolves to 1 if the file was handled, 0 otherwise.
*/
const processFile = async (file: string, filePath: string): Promise<number> => {
try {
const { default: taskFunction } = await import(filePath);

switch (getFileType(file)) {
case 'cron':
return await handleCronTask(file, taskFunction);
case 'worker':
case 'queue':
return 1;
default:
Logger.warn(`File: ${file}`);
Logger.warn(`File type is not supported. Skipping...`);
return 0;
}
} catch (error: any) {
Logger.error(`Error importing task from file: ${file}`, error);
return 0;
}
};

/**
* Determines the type of a file based on its name.
* @param file - The name of the file.
* @returns The type of the file ('cron', 'worker', 'queue', or 'unknown').
*/
const getFileType = (file: string): string => {
if (file.endsWith('cron.js')) return 'cron';
if (file.endsWith('worker.js')) return 'worker';
if (file.endsWith('queue.js')) return 'queue';
return 'unknown';
};

/**
* Handles a cron task file.
* @param file - The name of the file.
* @param taskFunction - The task function to execute.
* @returns A Promise that resolves to 1 if the task was executed, 0 otherwise.
*/
const handleCronTask = async (
file: string,
taskFunction: TaskFunction | undefined
): Promise<number> => {
if (taskFunction) {
taskFunction();
return 1;
} else {
Logger.warn(`File: ${file}`);
Logger.warn(`Task function not found. Skipping...`);
return 0;
}
};
2 changes: 2 additions & 0 deletions src/models/guild.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IGuild } from 'adroi.d.ea';
import { auditLogsSchema } from '../modules/auditLogs/models';
import { jailSchema } from '../modules/jail/models';
import mongoose from 'mongoose';
import { qotdSchema } from '../modules/qotd/models';
import { tempVoiceSchema } from '../modules/tempVoice/models';
Expand All @@ -18,6 +19,7 @@ const guildSchema = new mongoose.Schema<IGuild>({
modules: {
eventManagement: eventManagementSchema,
auditLogs: auditLogsSchema,
jail: jailSchema,
qotd: qotdSchema,
tempVoice: tempVoiceSchema,
twitch: twitchSchema
Expand Down
2 changes: 1 addition & 1 deletion src/modules/auditLogs/events/guild/guildBanAdd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AuditLogEvent, Client, EmbedBuilder, Events, GuildBan, userMention } from 'discord.js';
import { Colors, Emojis } from '../../../../utils/consts';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { addAuthor } from '../../../../utils/embeds.util';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auditLogs/events/guild/guildBanRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from 'discord.js';
import { Colors, Emojis } from '../../../../utils/consts';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { canSendMessage } from '../../../../utils/botUtil';
import { addAuthor } from '../../../../utils/embeds.util';
import { canSendMessage } from '../../../../utils/bot.util';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auditLogs/events/guild/guildRoleCreate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AuditLogEvent, Client, EmbedBuilder, Events, Role } from 'discord.js';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { addPermissionsNames } from '../../../../utils/modulesUil';
import { addAuthor } from '../../../../utils/embeds.util';
import { addPermissionsNames } from '../../../../utils/modules.uil';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auditLogs/events/guild/guildRoleDelete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AuditLogEvent, Client, EmbedBuilder, Events, Role } from 'discord.js';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { addPermissionsNames } from '../../../../utils/modulesUil';
import { addAuthor } from '../../../../utils/embeds.util';
import { addPermissionsNames } from '../../../../utils/modules.uil';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auditLogs/events/guild/guildRoleUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from 'discord.js';
import { Emojis } from '../../../../utils/consts';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { addComparedPermissionsNames } from '../../../../utils/modulesUil';
import { addAuthor } from '../../../../utils/embeds.util';
import { addComparedPermissionsNames } from '../../../../utils/modules.uil';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auditLogs/events/guild_members/guildMemberAdd.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Client, EmbedBuilder, Events, GuildMember } from 'discord.js';
import { Colors, Emojis } from '../../../../utils/consts';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { addAuthor } from '../../../../utils/embeds.util';
import guildService from '../../../../services/guild.service';
import { timestampToDate } from '../../../../utils/botUtil';
import { timestampToDate } from '../../../../utils/bot.util';

export default {
name: Events.GuildMemberAdd,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Client, EmbedBuilder, Events, GuildMember, inlineCode } from 'discord.js';
import { Colors, Emojis } from '../../../../utils/consts';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { detailedShortDate } from '../../../../utils/botUtil';
import { addAuthor } from '../../../../utils/embeds.util';
import { detailedShortDate } from '../../../../utils/bot.util';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
userMention
} from 'discord.js';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { canSendMessage } from '../../../../utils/botUtil';
import { addAuthor } from '../../../../utils/embeds.util';
import { canSendMessage } from '../../../../utils/bot.util';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
userMention
} from 'discord.js';
import { IAuditLogsModule } from 'adroi.d.ea';
import { canSendMessage } from '../../../../utils/botUtil';
import { canSendMessage } from '../../../../utils/bot.util';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auditLogs/events/guild_messages/messageDelete.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Client, EmbedBuilder, Events, GuildBasedChannel, Message } from 'discord.js';
import { Colors, Emojis } from '../../../../utils/consts';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { canSendMessage } from '../../../../utils/botUtil';
import { addAuthor } from '../../../../utils/embeds.util';
import { canSendMessage } from '../../../../utils/bot.util';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auditLogs/events/guild_messages/messageUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client, EmbedBuilder, Events, GuildBasedChannel, Message } from 'discord.js';
import { IAuditLogsModule } from 'adroi.d.ea';
import { addAuthor } from '../../../../utils/embedsUtil';
import { canSendMessage } from '../../../../utils/botUtil';
import { addAuthor } from '../../../../utils/embeds.util';
import { canSendMessage } from '../../../../utils/bot.util';
import guildService from '../../../../services/guild.service';

export default {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/core/commands/misc/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
PermissionsBitField
} from 'discord.js';
import { Channels, Colors, Guilds } from '../../../../utils/consts';
import { addAuthor } from '../../../../utils/embedsUtil';
import { addAuthor } from '../../../../utils/embeds.util';

export default {
data: {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/core/commands/moderation/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'discord.js';
import { CustomErrors } from '../../../../utils/errors';
import { OWNER_ID } from '../../../../utils/consts';
import { sendChangeLogRow } from '../../components/buttons/sendChangelogBtn';
import { sendChangeLogRow } from '../../components/buttons/sendChangelog.button';

export default {
data: {
Expand Down
Loading

0 comments on commit a314e23

Please sign in to comment.