Skip to content

Commit 09816f8

Browse files
committed
Fix inconsistent logging to discord
1 parent 8ab755f commit 09816f8

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

src/clients/discord.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export interface IClient extends Client {
99
commands: Map<string, Command>;
1010
addCommand: (command: Command) => void;
1111
saveCommands: () => Promise<void>;
12+
/*
13+
* Waits for client to be ready, if it is already ready it will immidiately resolve
14+
*/
15+
awaitReady: () => Promise<void>;
1216
}
1317

1418
const client = new Client({
@@ -50,4 +54,14 @@ client.on("interactionCreateHook", (interaction: Interaction) => {
5054
command.execute(interaction);
5155
});
5256

57+
client.awaitReady = async function () {
58+
const isReady = this.isReady();
59+
if (isReady) return;
60+
61+
return new Promise((resolve) => {
62+
this.on("ready", () => resolve());
63+
});
64+
65+
};
66+
5367
export default client;

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import router from "./routes";
66
import poweredBy from "./middleware/poweredBy";
77
import cors from "./middleware/cors";
88

9-
// import "./commands";
10-
119
const app = new Koa();
1210

1311
app
12+
.use(poweredBy("rocket-fuel (koa)"))
13+
.use(router.allowedMethods())
1414
.use(cors({
15-
allowMethods: ["GET", "POST", "DELETE"],
1615
origin: ["https://www.commitrocket.com", "https://commitrocket.com", "http://localhost:3000"]
1716
}))
18-
.use(poweredBy("rocket-fuel (koa)"))
1917
.use(router.routes())
20-
.use(router.allowedMethods())
2118
.listen(3001, async () => console.log("Server started on port 3001 (http://localhost:3001)"));

src/routes/email.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Router from "koa-zod-router";
22
import { z } from "zod";
3+
34
import db from "../storage/db";
45
import { logToChannel } from "../storage/discord";
56
import { sanitizeHtml } from "../utils/sanitize";
@@ -27,6 +28,7 @@ const successResponse: z.infer<typeof subscriptionResponseSchema> = {
2728
};
2829

2930
router.post("/subscribe", async (ctx) => {
31+
3032
const body = ctx.request.body;
3133
const mailingList = db.collection("mailing-list");
3234

@@ -38,9 +40,10 @@ router.post("/subscribe", async (ctx) => {
3840
ctx.body = successResponse;
3941
return;
4042
}
43+
4144
await Promise.all([
42-
mailingList.set(body.email, newSubscriber, {}),
43-
logToChannel(body.email, process.env.DISCORD_MAIL_CHANNEL!)
45+
logToChannel(body.email, process.env.DISCORD_MAIL_CHANNEL!),
46+
mailingList.set(body.email, newSubscriber, {})
4447
]);
4548

4649
ctx.body = successResponse;

src/routes/feedback.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Router from "koa-zod-router";
2-
import { v4 as generateUuidV4 } from "uuid";
32
import { z } from "zod";
4-
import db from "../storage/db";
3+
54
import { logToChannel } from "../storage/discord";
65
import { sanitizeHtml } from "../utils/sanitize";
76

src/storage/db.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import CyclicDb from "@cyclic.sh/dynamodb";
22
import CyclicItem from "@cyclic.sh/dynamodb/dist/cy_db_item";
3-
import CyclicCollection from "@cyclic.sh/dynamodb/dist/cy_db_collection";
43
import CyclicIndex from "@cyclic.sh/dynamodb/dist/cy_db_index";
54

65
interface QueryResults<ResultType extends any> {

src/storage/discord.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import { ChannelType } from "discord.js";
22
import client from "../clients/discord";
33

44
export const logToChannel = async (text: string, channelID: string) => {
5-
const channel = await client.channels.fetch(channelID, { cache: true });
5+
await client.awaitReady();
6+
const channel = await client.channels.fetch(channelID);
67

7-
if (!channel)
8-
throw new Error("Channel doesn't exist");
9-
10-
if (channel.type !== ChannelType.GuildText)
11-
throw new Error("Channel needs to be a Text-based channel");
8+
if (!channel) throw new Error("Channel doesn't exist");
9+
if (channel.type !== ChannelType.GuildText) throw new Error("Channel needs to be a Text-based channel");
1210

1311
await channel.send({
1412
content: text,
1513
});
14+
return true;
1615
};

0 commit comments

Comments
 (0)