Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion #.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ POSTGRES_PASSWORD=
DATABASE_PORT=5432
DATABASE_HOST=localhost
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${POSTGRES_DB}?schema=public
MAILING_API_KEY=
SMTP_USER=
SMTP_PASS=
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ POSTGRES_PASSWORD=
DATABASE_PORT=5432
DATABASE_HOST=localhost
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${POSTGRES_DB}?schema=public
MAILING_API_KEY=
SMTP_USER=
SMTP_PASS=
```
An empty copy of this file is available as [#.env.development](./#.env.development).

Expand Down
11 changes: 7 additions & 4 deletions config.development.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@
"emote": "📢"
},
"communicationServiceOptions": {
"mailData": {
"from": "[email protected]",
"templateId": "9131196"
}
"from": "[email protected]",
"port": 587,
"host": "smtp.ethereal.email",
"secure": false,
"service": "Ethereal",
"debug": true,
"logger": true
}
}
9 changes: 5 additions & 4 deletions config.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@
"emote": "📢"
},
"communicationServiceOptions": {
"mailData": {
"from": "[email protected]",
"templateId": "9131196"
}
"from": "[email protected]",
"host": "smtp.office365.com",
"port": 587,
"secure": true,
"service": "Outlook365"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
"@hunteroi/discord-selfrole": "^4.0.5",
"@hunteroi/discord-temp-channels": "^3.3.1",
"@hunteroi/discord-verification": "^1.5.2",
"smtp2go-nodejs": "^0.3.5",
"discord-sync-commands": "^0.5.2",
"discord.js": "^14.22.1",
"nodemailer": "^7.0.6",
"ts-postgres": "1.3.0"
},
"devDependencies": {
"@biomejs/biome": "^2.2.4",
"@types/node": "^24.5.2",
"@types/nodemailer": "^7.0.1",
"tsx": "^4.20.5",
"typescript": "^5.9.2"
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/others/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "discord.js";

import type { DatadropClient } from "../../datadrop.js";
import type { Command } from "../../models/Command.js";
import type { Command } from "../../models/index.js";

export default {
data: new SlashCommandBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/commands/owner/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import type { DatadropClient } from "../../datadrop.js";
import { clean } from "../../helpers.js";
import type { Command } from "../../models/Command.js";
import type { Command } from "../../models/index.js";

export default {
data: new SlashCommandBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/commands/owner/reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "discord.js";

import type { DatadropClient } from "../../datadrop.js";
import type { Command } from "../../models/Command.js";
import type { Command } from "../../models/index.js";

export default {
data: new SlashCommandBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/commands/owner/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
} from "discord.js";

import type { DatadropClient } from "../../datadrop.js";
import type { Command } from "../../models/Command.js";
import type { Command } from "../../models/index.js";

export default {
data: new SlashCommandBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/commands/utility/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "discord.js";

import type { DatadropClient } from "../../datadrop.js";
import type { Command } from "../../models/Command.js";
import type { Command } from "../../models/index.js";

const people = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/commands/utility/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "discord.js";

import type { DatadropClient } from "../../datadrop.js";
import type { Command } from "../../models/Command.js";
import type { Command } from "../../models/index.js";

export default {
data: new SlashCommandBuilder().setName("ping").setDescription("Pong!"),
Expand Down
13 changes: 9 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import packageInfo from "../package.json" with { type: "json" };
import type { Configuration } from "./models/Configuration.js";
import type { Configuration } from "./models/index.js";

// should be Partial<Configuration> but codebase not ready yet
const defaultConfig: Configuration = {
Expand Down Expand Up @@ -29,8 +29,10 @@ const defaultConfig: Configuration = {
tutor: { roleid: "", emote: "" },
announce: { roleid: "", emote: "", channelid: "" },
communicationServiceOptions: {
apiKey: "",
mailData: { from: "", templateId: "" },
auth: { user: "", pass: "" },
from: "",
port: 587,
host: "",
},
};

Expand All @@ -51,7 +53,10 @@ export async function readConfig(): Promise<Configuration> {
...json,
version: `${environment}-v${packageInfo.version}`,
};
config.communicationServiceOptions.apiKey = process.env.MAILING_API_KEY;
config.communicationServiceOptions.auth = {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
};

return config;
} catch (err: unknown) {
Expand Down
17 changes: 9 additions & 8 deletions src/datadrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
VerificationManager,
VerificationManagerEvents,
} from "@hunteroi/discord-verification";
import { SMTP2GoService } from "@hunteroi/discord-verification/lib/services/SMTP2GoService.js";
import {
type ButtonInteraction,
Client,
Expand All @@ -37,12 +36,14 @@ import {

import { readConfig } from "./config.js";
import { getErrorMessage, readFilesFrom } from "./helpers.js";
import type { Command } from "./models/Command.js";
import type { Configuration } from "./models/Configuration.js";
import type { Event } from "./models/Event.js";
import type { IDatabaseService } from "./models/IDatabaseService.js";
import type { User } from "./models/User.js";
import PostgresDatabaseService from "./services/PostgresDatabaseService.js";
import type {
Command,
Configuration,
Event,
IDatabaseService,
User,
} from "./models/index.js";
import { PostgresDatabaseService, SMTPService } from "./services/index.js";

export class DatadropClient extends Client {
#config: Configuration;
Expand Down Expand Up @@ -74,7 +75,7 @@ export class DatadropClient extends Client {
this.tempChannelsManager = new TempChannelsManager(this);

this.database = new PostgresDatabaseService(this.logger);
const communicationService = new SMTP2GoService(
const communicationService = new SMTPService(
config.communicationServiceOptions,
);
this.verificationManager = new VerificationManager(
Expand Down
3 changes: 1 addition & 2 deletions src/events/clientReady.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { RoleToEmojiData } from "@hunteroi/discord-selfrole";
import { ButtonStyle, bold, Events, Role, roleMention } from "discord.js";

import type { DatadropClient } from "../datadrop.js";
import type { Configuration } from "../models/Configuration.js";
import type { Event } from "../models/Event.js";
import type { Configuration, Event } from "../models/index.js";

export default {
name: Events.ClientReady,
Expand Down
7 changes: 7 additions & 0 deletions src/events/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { DatadropClient } from "../datadrop.js";

export default async function error(client: DatadropClient, error: Error) {
client.logger.error(
`${error.name}: ${error.message}\n${error.cause}\n${error.stack}`,
);
}
3 changes: 1 addition & 2 deletions src/events/guildMemberAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {

import type { DatadropClient } from "../datadrop.js";
import { getErrorMessage } from "../helpers.js";
import type { AnnounceConfiguration } from "../models/Configuration.js";
import type { Event } from "../models/Event.js";
import type { AnnounceConfiguration, Event } from "../models/index.js";

export default {
name: Events.GuildMemberAdd,
Expand Down
2 changes: 1 addition & 1 deletion src/events/guildMemberRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Events, type GuildMember } from "discord.js";

import type { DatadropClient } from "../datadrop.js";
import { getErrorMessage } from "../helpers.js";
import type { Event } from "../models/Event.js";
import type { Event } from "../models/index.js";

export default {
name: Events.GuildMemberRemove,
Expand Down
2 changes: 1 addition & 1 deletion src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from "discord.js";

import type { DatadropClient } from "../datadrop.js";
import type { Event } from "../models/Event.js";
import type { Event } from "../models/index.js";
import { CommandHandler } from "../services/CommandHandler.js";

export default {
Expand Down
1 change: 1 addition & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fsp from "node:fs/promises";
import * as path from "node:path";

import { ConsoleLogger, type DefaultLogger } from "@hunteroi/advanced-logger";

const console = new ConsoleLogger();
Expand Down
5 changes: 3 additions & 2 deletions src/models/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SMTP2GoOptions } from "@hunteroi/discord-verification/lib/services/SMTP2GoService.js";
import type { Snowflake } from "discord.js";

import type { SMTPServiceOptions } from "../services/SMTPService.js";

export interface SpecialRoleConfiguration {
roleid: Snowflake;
emote: string;
Expand Down Expand Up @@ -58,5 +59,5 @@ export interface Configuration {

announce: AnnounceConfiguration;

communicationServiceOptions: SMTP2GoOptions;
communicationServiceOptions: SMTPServiceOptions;
}
1 change: 1 addition & 0 deletions src/models/Event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ClientEvents } from "discord.js";

import type { DatadropClient } from "../datadrop.js";

export interface Event {
Expand Down
11 changes: 11 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { Command } from "./Command.js";
export {
AnnounceConfiguration,
Configuration,
GroupConfiguration,
SpecialRoleConfiguration,
YearConfiguration,
} from "./Configuration.js";
export { Event } from "./Event.js";
export { IDatabaseService } from "./IDatabaseService.js";
export { User } from "./User.js";
15 changes: 8 additions & 7 deletions src/services/PostgresDatabaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
} from "ts-postgres";

import { getErrorMessage } from "../helpers.js";
import type { IDatabaseService } from "../models/IDatabaseService.js";
import type { User } from "../models/User.js";
import type { IDatabaseService, User } from "../models/index.js";

export default class PostgresDatabaseService implements IDatabaseService {
export class PostgresDatabaseService implements IDatabaseService {
readonly #logger: ConsoleLogger;
readonly #database: Client;

Expand Down Expand Up @@ -105,7 +104,7 @@ export default class PostgresDatabaseService implements IDatabaseService {
*/
public async readBy(
argument: // biome-ignore lint/suspicious/noExplicitAny: DB values can be of any type
Map<string, any> | ((user: User, index: string | number) => boolean),
Map<string, any> | ((user: User, index: string | number) => boolean),
): Promise<User | undefined | null> {
if (!(argument instanceof Map))
throw new Error("Method not implemented.");
Expand Down Expand Up @@ -208,7 +207,7 @@ export default class PostgresDatabaseService implements IDatabaseService {
async #runMigrations(): Promise<void> {
await this.#runMigration("User soft delete", async () => {
await this.#database.query(
"ALTER TABLE Users ADD IF NOT EXISTS isDeleted timestamp;",
"ALTER TABLE Users ADD COLUMN IF NOT EXISTS isDeleted timestamp;",
);
});
}
Expand All @@ -218,8 +217,10 @@ export default class PostgresDatabaseService implements IDatabaseService {
"SELECT * FROM Migrations WHERE name = $1",
[name],
);
const migration = [...result].pop();
if (!migration) {
this.#logger.verbose(
`Running migration "${name}" with pre-query returning ${JSON.stringify(result)}`,
);
if (result && result.rows.length === 0) {
await this.#database.query(
"INSERT INTO Migrations (name) VALUES($1);",
[name],
Expand Down
26 changes: 26 additions & 0 deletions src/services/SMTPService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ISenderAPI, SenderAPIData } from "@hunteroi/discord-verification";
import { createTransport, getTestMessageUrl } from "nodemailer";
import type SMTPTransport from "nodemailer/lib/smtp-transport/index.js";

export type SMTPServiceOptions = SMTPTransport.Options;

export class SMTPService implements ISenderAPI {
readonly #options: SMTPServiceOptions;

constructor(options: SMTPServiceOptions) {
this.#options = options;
}

async send({ name, code, ...data }: SenderAPIData): Promise<void> {
const transporter = createTransport(this.#options);
await transporter.verify();
const result = await transporter.sendMail({
from: this.#options.from,
to: data.to,
subject: "Code d'Authentification Discord",
text: `Hello ${name}! Ton code est ${code}. A plus tard o/`,
html: `<p>Hello ${name}!</p><p>Ton code est ${code}.</p><p>A plus tard o/</p>`,
});
console.debug("Preview URL: %s", getTestMessageUrl(result));
}
}
2 changes: 2 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { PostgresDatabaseService } from "./PostgresDatabaseService.js";
export { SMTPService } from "./SMTPService.js";
Loading