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

Smtp mailing service #107

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ POSTGRES_PASSWORD=
DATABASE_PORT=5432
DATABASE_HOST=localhost
NODE_ENV=development
SENDGRID_API_KEY=
SMTP_USER=
SMTP_PASS=
12 changes: 8 additions & 4 deletions config.development.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@
"emote": "📢"
},
"communicationServiceOptions": {
"mailData": {
"from": "[email protected]",
"templateId": "d-c8892944b4f2417bafece2d7a5b73d1f"
}
"from": "[email protected]",
"port": 587,
"host": "smtp.office365.com",
"tls": {
"rejectUnauthorized": false
},
"debug": true,
"logger": true
}
}
8 changes: 5 additions & 3 deletions config.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@
"emote": "📢"
},
"communicationServiceOptions": {
"mailData": {
"from": "[email protected]",
"templateId": "d-ae8dfdd7cc9c49f1af6e016ecbb4d856"
"from": "[email protected]",
"port": 587,
"host": "smtp.office365.com",
"tls": {
"rejectUnauthorized": false
}
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datadrop",
"version": "2.0.0",
"version": "2.0.1",
"type": "module",
"main": "./build/index.js",
"scripts": {
Expand All @@ -26,15 +26,16 @@
"@hunteroi/discord-selfrole": "^4.0.4",
"@hunteroi/discord-temp-channels": "^3.3.0",
"@hunteroi/discord-verification": "^1.5.0",
"@sendgrid/mail": "8.1.3",
"discord-sync-commands": "^0.3.0",
"discord.js": "^14.16.2",
"dotenv": "^16.4.5",
"nodemailer": "^6.9.1",
"ts-postgres": "1.3.0"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@types/node": "^20.12.7",
"@types/nodemailer": "^6.4.7",
"typescript": "^5.4.4"
}
}
1 change: 1 addition & 0 deletions scripts/deploy-commands.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Client, Collection, REST, Routes } = require("discord.js");
const path = require("node:path");
const fsp = require("node:fs/promises");
const synchronizeSlashCommands = require("discord-sync-commands");

const { botId: botProdId } = require("../config.production.json");
const { guildId, botId: botDevId } = require("../config.development.json");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/admins/announce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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/others/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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/others/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
} 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 ContextMenuCommandBuilder()
.setName("(Dés)Épingler le message")
.setType(ApplicationCommandType.Message)

Check failure on line 14 in src/commands/others/pin.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Argument of type 'ApplicationCommandType.Message' is not assignable to parameter of type 'ContextMenuCommandType'.
.setContexts(InteractionContextType.Guild),

async execute(
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 @@ -7,7 +7,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 @@ -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";

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 @@ -5,7 +5,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
12 changes: 8 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
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,8 +53,10 @@ export async function readConfig(): Promise<Configuration> {
...json,
version: `${environment}-v${packageInfo.version}`,
};
config.communicationServiceOptions.apiKey =
process.env.SENDGRID_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 @@ -34,16 +34,17 @@ import {
VerificationManager,
VerificationManagerEvents,
} from "@hunteroi/discord-verification";
import { SendGridService } from "@hunteroi/discord-verification/lib/services/SendGridService.js";

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 @@ -75,7 +76,7 @@ export class DatadropClient extends Client {
this.tempChannelsManager = new TempChannelsManager(this);

this.database = new PostgresDatabaseService(this.logger);
const communicationService = new SendGridService(
const communicationService = new SMTPService(
config.communicationServiceOptions,
);
this.verificationManager = new VerificationManager(
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 @@ -8,8 +8,7 @@ import {
} from "discord.js";

import type { DatadropClient } from "../datadrop.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
@@ -1,7 +1,7 @@
import { Events, type GuildMember } from "discord.js";

import type { DatadropClient } from "../datadrop.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 @@ -16,7 +16,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
3 changes: 1 addition & 2 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { ButtonStyle, Events, Role, bold, roleMention } from "discord.js";
import type { RoleToEmojiData } from "@hunteroi/discord-selfrole";

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
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
4 changes: 2 additions & 2 deletions src/models/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Snowflake } from "discord.js";

import type { SendGridOptions } from "@hunteroi/discord-verification/lib/services/SendGridService.js";
import type { SMTPServiceOptions } from "../services/SMTPService.js";

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

announce: AnnounceConfiguration;

communicationServiceOptions: SendGridOptions;
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
23 changes: 23 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Command } from "./Command.js";
import {
AnnounceConfiguration,
Configuration,
GroupConfiguration,
SpecialRoleConfiguration,
YearConfiguration,
} from "./Configuration.js";
import { Event } from "./Event.js";
import { IDatabaseService } from "./IDatabaseService.js";
import { User } from "./User.js";

export {
Command,
SpecialRoleConfiguration,
GroupConfiguration,
YearConfiguration,
AnnounceConfiguration,
Configuration,
Event,
IDatabaseService,
User,
};
11 changes: 6 additions & 5 deletions src/services/PostgresDatabaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {

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

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 {
readonly #logger: ConsoleLogger;
Expand Down Expand Up @@ -206,7 +205,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 @@ -216,8 +215,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 { createTransport } from "nodemailer";
import type SMTPTransport from "nodemailer/lib/smtp-transport";

import type { ISenderAPI, SenderAPIData } from "@hunteroi/discord-verification";

export type SMTPServiceOptions = SMTPTransport.Options;

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

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

async send({ name, code, ...data }: SenderAPIData): Promise<void> {
const transporter = createTransport(this.#options);
await transporter.verify();
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>`,
});
}
}
4 changes: 4 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import PostgresDatabaseService from "./PostgresDatabaseService.js";
import SMTPService from "./SMTPService.js";

export { PostgresDatabaseService, SMTPService };
Loading
Loading