Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.
Merged
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
12 changes: 6 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ model blacklistedUsers {
}

model connectedList {
id String @id @default(auto()) @map("_id") @db.ObjectId
channelId String @unique
id String @id @default(auto()) @map("_id") @db.ObjectId
channelId String @unique
serverId String
connected Boolean
compact Boolean
invite String?
profFilter Boolean
webhook NetworkWebhook
date DateTime @default(now())
hub hubs? @relation(fields: [hubId], references: [id])
hubId String? @db.ObjectId
webhookURL String
date DateTime @default(now())
hub hubs? @relation(fields: [hubId], references: [id])
hubId String @db.ObjectId
}

model hubs {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Network/editMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default {
const channelSettings = channelSettingsArr.find(c => c.channelId === obj.channelId);

if (channelSettings) {
const webhook = new WebhookClient({ id: channelSettings.webhook.id, token: channelSettings.webhook.token });
const webhook = new WebhookClient({ url: channelSettings.webhookURL });
const compact = channelSettings?.profFilter ? newMessage : censoredNewMessage;
const webhookEmbed = channelSettings?.profFilter ? censoredEmbed : newEmbed;

Expand Down
8 changes: 4 additions & 4 deletions src/Events/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface NetworkMessage extends Message {

export interface NetworkWebhookSendResult {
message: APIMessage | null
webhookId: string;
webhookURL: string;
}

export default {
Expand Down Expand Up @@ -120,13 +120,13 @@ export default {
};
}

const webhook = new WebhookClient({ id: `${connection?.webhook.id}`, token: `${connection?.webhook.token}` });
const webhook = new WebhookClient({ url: connection.webhookURL });
const webhookSendRes = await webhook.send(webhookMessage).catch(() => null);
return { webhookId: webhook.id, message: webhookSendRes } as NetworkWebhookSendResult;
return { webhookURL: webhook.url, message: webhookSendRes } as NetworkWebhookSendResult;
});

message.delete().catch(() => null);
cleanup.execute(message, await Promise.all(messageResults), channelInDb.hubId);
cleanup(message, await Promise.all(messageResults), channelInDb.hubId);
}
},
};
Expand Down
81 changes: 39 additions & 42 deletions src/Scripts/message/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,46 @@ import { Message } from 'discord.js';
import { NetworkWebhookSendResult } from '../../Events/messageCreate';
import { getDb } from '../../Utils/functions/utils';

export default {
/**
* Disconnects connections if an errored occured while sending the message to it.
* Otherwise, inserts messages into `messageData` collection for future use.
*/
async execute(message: Message, channelAndMessageIds: NetworkWebhookSendResult[], hubId: string | null) {
// All message data is stored in the database, so we can delete the message from the network later
const messageDataObj: { channelId: string, messageId: string }[] = [];
const invalidWebhookIds: string[] = [];
/**
* Disconnects connections if an errored occured while sending the message to it.
* Otherwise, inserts messages into `messageData` collection for future use.
*/
export default async function execute(message: Message, channelAndMessageIds: NetworkWebhookSendResult[], hubId: string | null) {
const messageDataObj: { channelId: string, messageId: string }[] = [];
const invalidWebhookURLs: string[] = [];

channelAndMessageIds.forEach((result) => {
if (!result.message) {
invalidWebhookIds.push(result.webhookId);
}
else {
messageDataObj.push({
channelId: result.message.channel_id,
messageId: result.message.id,
});
}
});

const db = getDb();
if (message.guild && hubId) {
// store message data in db
await db.messageData.create({
data: {
hub: { connect: { id: hubId } },
channelAndMessageIds: messageDataObj,
timestamp: message.createdAt,
authorId: message.author.id,
serverId: message.guild.id,
reference: message.reference,
},
});
channelAndMessageIds.forEach((result) => {
if (!result.message) {
invalidWebhookURLs.push(result.webhookURL);
}

// disconnect network if, webhook does not exist/bot cannot access webhook
if (invalidWebhookIds.length > 0) {
await db.connectedList.updateMany({
where: { webhook: { is: { id: { in: invalidWebhookIds } } } },
data: { connected: false },
else {
messageDataObj.push({
channelId: result.message.channel_id,
messageId: result.message.id,
});
}
},
};
});

const db = getDb();
if (message.guild && hubId) {
// store message data in db
await db.messageData.create({
data: {
hub: { connect: { id: hubId } },
channelAndMessageIds: messageDataObj,
timestamp: message.createdAt,
authorId: message.author.id,
serverId: message.guild.id,
reference: message.reference,
},
});
}

// disconnect network if, webhook does not exist/bot cannot access webhook
if (invalidWebhookURLs.length > 0) {
await db.connectedList.updateMany({
where: { webhookURL: { in: invalidWebhookURLs } },
data: { connected: false },
});
}
}
2 changes: 1 addition & 1 deletion src/Scripts/network/displaySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export = {
where: { channelId: updConnection.channelId },
data: {
channelId: channel?.id,
webhook: { id: webhook.id, token: `${webhook.token}`, url: webhook.url },
webhookURL: webhook.url,
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/Scripts/network/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export = {
connected: true,
profFilter: true,
compact: false,
webhook: { id: webhook.id, token: `${webhook.token}`, url: webhook.url },
webhookURL: webhook.url,
hub: { connect: { id: hub.id } },
},
});
Expand Down
19 changes: 9 additions & 10 deletions tset.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { PrismaClient } from '@prisma/client';
import { isArray } from 'lodash';

const db = new PrismaClient();

// replace old connectedList with new connectedList (do this in prod code (old one), the schema file here wont work)
(async () => {
const idk = await db.connectedList.findMany();

idk.forEach(async c => {
const svr = await db.setup.findFirst({ where: { guildId: c.serverId } });
Object.assign(svr, { serverId: svr.guildId, connected: true });
delete svr.guildId;

await db.connectedList.create({ data: svr });
});
const idk = await db.connectedList.findRaw({ filter: {} });

if (isArray(idk)) {
idk.forEach(async c => {
console.log({ id: c._id.$oid, webhook: c.webhook.url });
await db.connectedList.update({ where: { id: c._id.$oid }, data: { webhookURL: c.webhook.url } });
});
}
})();