Skip to content

Commit

Permalink
use port env variable instead
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Jun 25, 2024
1 parent f7a3e81 commit 7995771
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ IMGUR_CLIENT_ID="imgur client id" # get it from imgur, needed to parse imgur url
VOTE_WEBHOOK_URL="your vote webhook url" # for sending that someone voted for the bot (optional)
NETWORK_API_KEY="your network api key" # for posting to global chat (ask devoid)
NODE_ENV=development # change to production when deploying
DEBUG=false # set to true to enable debug logging
DEBUG=false # set to true to enable debug logging
PORT=3000 # or anything else for production
4 changes: 2 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Logger from '../utils/Logger.js';
import express from 'express';
import dblRoute from './routes/dbl.js';
import nsfwRouter from './routes/nsfw.js';
import { API_PORT } from '../utils/Constants.js';

// to start the server
export const startApi = (data: { voteManager: VoteManager }) => {
Expand All @@ -14,5 +13,6 @@ export const startApi = (data: { voteManager: VoteManager }) => {
app.use(nsfwRouter);
if (data.voteManager) app.use(dblRoute(data.voteManager));

app.listen(API_PORT, () => Logger.info(`API listening on port http://localhost:${API_PORT}.`));
const port = process.env.PORT;
app.listen(port, () => Logger.info(`API listening on port http://localhost:${port}.`));
};
14 changes: 4 additions & 10 deletions src/core/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,21 @@ export default class SuperClient extends Client {
SuperClient.instance = this;

// initialize i18n for localization
await loadLocales('locales/src/locales');
loadLocales('locales/src/locales');

// load commands
await loadCommandFiles();

await syncConnectionCache();
this._connectionCachePopulated = true;

this.scheduler.addRecurringTask(
'populateConnectionCache',
60_000 * 5,
syncConnectionCache,
);

// store network message timestamps to connectedList every minute
this.scheduler.addRecurringTask('populateConnectionCache', 60_000 * 5, syncConnectionCache);
this.scheduler.addRecurringTask('storeMsgTimestamps', 60 * 1_000, () => {
// store network message timestamps to connectedList every minute
storeMsgTimestamps(messageTimestamps);
messageTimestamps.clear();
});


await this.login(process.env.TOKEN);
}

Expand All @@ -135,7 +129,7 @@ export default class SuperClient extends Client {
async fetchGuild(guildId: Snowflake): Promise<RemoveMethods<Guild> | undefined> {
const fetch = (await this.cluster.broadcastEval(
(client, guildID) => client.guilds.cache.get(guildID),
{ context: guildId },
{ guildId, context: guildId },
)) as Guild[];

return fetch ? SuperClient.resolveEval(fetch) : undefined;
Expand Down
25 changes: 12 additions & 13 deletions src/scripts/network/storeMessageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,18 @@ export default async (
if (!message.inGuild()) return;

// store message data in db
await db.originalMessages
.create({
data: {
messageId: message.id,
authorId: message.author.id,
serverId: message.guildId,
messageReference: dbReference?.messageId,
createdAt: message.createdAt,
broadcastMsgs: { createMany: { data: messageDataObj } },
hub: { connect: { id: hubId } },
reactions: {},
},
});
await db.originalMessages.create({
data: {
messageId: message.id,
authorId: message.author.id,
serverId: message.guildId,
messageReference: dbReference?.messageId,
createdAt: message.createdAt,
broadcastMsgs: { createMany: { data: messageDataObj } },
hub: { connect: { id: hubId } },
reactions: {},
},
});
}

// store message timestamps to push to db later
Expand Down
1 change: 0 additions & 1 deletion src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const isDevBuild = process.env.NODE_ENV === 'development';

export const PROJECT_VERSION = require('../../package.json').version ?? 'Unknown';
export const CLIENT_ID = isDevBuild ? '798748015435055134' : '769921109209907241';
export const API_PORT = isDevBuild ? 3000 : 443;
export const SUPPORT_SERVER_ID = '770256165300338709';
export const VOTER_ROLE_ID = '985153241727770655';

Expand Down
7 changes: 3 additions & 4 deletions src/utils/HubLogger/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,16 @@ const genJumpLink = async (
});
if (!messageInDb) return null;


// fetch the reports server ID from the log channel's ID
const reportsServerId = SuperClient.resolveEval(
await client.cluster.broadcastEval(
async (cl, ctx) => {
async (cl, channelId) => {
const channel = (await cl.channels
.fetch(ctx.reportsChannelId)
.fetch(channelId)
.catch(() => null)) as GuildTextBasedChannel | null;
return channel?.guild.id;
},
{ context: { reportsChannelId } },
{ context: reportsChannelId },
),
);

Expand Down
4 changes: 1 addition & 3 deletions src/utils/NSFWDetection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { API_PORT } from './Constants.js';

export declare type predictionType = {
className: 'Drawing' | 'Hentai' | 'Neutral' | 'Porn' | 'Sexy';
probability: number;
Expand All @@ -10,7 +8,7 @@ export declare type predictionType = {
* @returns The predictions object
*/
export const analyzeImageForNSFW = async (imageUrl: string): Promise<predictionType[] | null> => {
const res = await fetch(`http://localhost:${API_PORT}/nsfw`, {
const res = await fetch(`http://localhost:${process.env.PORT}/nsfw`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ imageUrl }),
Expand Down
13 changes: 6 additions & 7 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,17 @@ export const modifyUserRole = async (
await cluster.broadcastEval(
async (client, ctx) => {
const guild = client.guilds.cache.get(ctx.guildId);
const role = guild?.roles.cache.find(({ id }) => id === ctx.roleId);
if (!guild) return;

if (!guild || !role) return null;
const role = await guild.roles.fetch(ctx.roleId);
if (!role) return;

// add or remove role
const member = await guild.members.fetch(ctx.userId).catch(() => null);
return await member?.roles[ctx.action](role).catch(() => null);
const member = await guild.members.fetch(ctx.userId);
await member?.roles[ctx.action](role);
},
{ guildId, context: { userId, roleId, guildId, action } },
{ context: { userId, roleId, guildId, action } },
);

return;
};

/**
Expand Down

0 comments on commit 7995771

Please sign in to comment.