Skip to content

Commit

Permalink
fix(network): fix ratelimit issue by using different ips for each web…
Browse files Browse the repository at this point in the history
…hook send

(hopefully)
  • Loading branch information
dev-737 committed Feb 1, 2024
1 parent b459abe commit 591c635
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ TOKEN="discord bot token here"
DATABASE_URL="mongodb connection url here" # get it from mongodb atlas or use local mongodb
TOPGG_API_KEY="your topgg bot access token>" # not required if you don't have a bot on top.gg.
TENOR_KEY="tenor api key" # required for posting gifs in global chat.
SENTRY_AUTH_TOKEN="your sentry auth token" # get it from devoid
NODE_ENV=development # change to production when deploying
SENTRY_DSN="your sentry dsn link" # get it by making a sentry account
IMGUR_CLIENT_ID="imgur client id" # get it from imgur
VOTE_WEBHOOK_URL="your vote webhook url" # for sending that someone voted for your bot
NETWORK_API_KEY="your network api key" # for posting to global chat (ask devoid)
NODE_ENV=development # change to production when deploying
28 changes: 7 additions & 21 deletions src/managers/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { check as checkProfanity, censor } from '../utils/Profanity.js';
import { HubSettingsBitField } from '../utils/BitFields.js';
import { parseTimestampFromId, replaceLinks, wait } from '../utils/Utils.js';
import { t } from '../utils/Locale.js';
import Logger from '../utils/Logger.js';
import sendMessage from '../scripts/network/sendMessage.js';

export interface NetworkMessage extends Message {
censoredContent: string;
Expand Down Expand Up @@ -160,24 +160,6 @@ export default class NetworkManager extends Factory {
if (index % 50) await wait(1000);

try {
// parse the webhook url and get the webhook id and token
// fetch the webhook from discord
let webhook = message.client.webhooks.get(connection.webhookURL);
if (!webhook) {
webhook = new WebhookClient(
{ url: connection.webhookURL },
{
rest: {
rejectOnRateLimit: (r) => {
Logger.warn('Webhook rate limited: %O', r);
return false;
},
},
},
);
message.client.webhooks.set(connection.webhookURL, webhook);
}

const reply = referenceInDb?.broadcastMsgs.find(
(msg) => msg.channelId === connection.channelId,
);
Expand Down Expand Up @@ -240,9 +222,13 @@ export default class NetworkManager extends Factory {
};
}
// send the message
const messageOrError = await webhook.send(messageFormat);
const messageOrError = await sendMessage(messageFormat, connection.webhookURL);

// return the message and webhook URL to store the message in the db
return { messageOrError, webhookURL: connection.webhookURL } as NetworkWebhookSendResult;
return {
messageOrError: messageOrError,
webhookURL: connection.webhookURL,
} as NetworkWebhookSendResult;
}
catch (e) {
// return the error and webhook URL to store the message in the db
Expand Down
17 changes: 17 additions & 0 deletions src/scripts/network/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { APIMessage, WebhookMessageCreateOptions } from 'discord.js';

export default async (message: WebhookMessageCreateOptions, webhookUrl: string) => {
const res = await fetch('https://interchat-networkwebhook.vercel.app/api/send', {
method: 'PUT',
body: JSON.stringify(message),
headers: {
authorization: `${process.env.NETWORK_API_KEY}`,
'x-webhook-url': webhookUrl,
'Content-Type': 'application/json',
},
});

const resBody = await res.json();

return res.status === 200 ? (resBody.result as APIMessage) : String(resBody.error);
};

0 comments on commit 591c635

Please sign in to comment.