Skip to content

Commit

Permalink
Merge pull request #366 from Member-Counter/l10n_verified-bot-edition…
Browse files Browse the repository at this point in the history
…-dev

New Crowdin updates
  • Loading branch information
eduardozgz committed Dec 20, 2023
2 parents d4ca7c1 + 708836d commit 0597b8f
Show file tree
Hide file tree
Showing 27 changed files with 232 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .env.example
Expand Up @@ -81,8 +81,9 @@ DISTRIBUTED=false
# HTTP Counter deny list
COUNTER_HTTP_DENY_LIST=

# Member Count fetching
MEMBER_COUNTS_CACHE_CHECK_SLEEP=5
MEMBER_COUNTS_CACHE_LIFETIME=3600
MEMBER_COUNTS_CACHE_LIFETIME=600

# The mongodb uri to access the db, if it is hosted in the same machine, you typically set this to mongodb://127.0.0.1:27017/memberCounter
DB_URI=mongodb://member-counter-bot-mongodb:27017/memberCounter
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "member-counter-bot",
"version": "0.0.0-dev",
"version": "0.0.0-verified-bot-edition-dev",
"engines": {
"node": ">=16.0"
},
Expand Down
13 changes: 13 additions & 0 deletions res/updateScripts/0.18.1_to_0.18.2.js
Expand Up @@ -35,6 +35,19 @@ const { DB_URI } = process.env;
`[${userResult.modifiedCount} of ${usersCollectionSize}] Documents Processed (User settings)`
);

// Guild Count cache
const cacheCollection = db.collection("guildcountcaches");
const cacheCollectionSize = await cacheCollection.countDocuments();
const cacheResult = await cacheCollection.updateMany(
{},
{ $rename: { guild: "id" } }
);

console.log(
`[${cacheResult.modifiedCount} of ${cacheCollectionSize}] Documents Processed (Guild Count cache)`
);


process.stdout.write("\nDone\n");
process.exit(0);
})();
44 changes: 44 additions & 0 deletions res/updateScripts/0.18.6_to_0.18.7.js
@@ -0,0 +1,44 @@
#!/usr/bin/env node
console.log("Updating from 0.18.6 to 0.18.7...");
require("dotenv").config();
const { MongoClient } = require("mongodb");

const { DB_URI } = process.env;

(async () => {
const mongoClient = await MongoClient.connect(DB_URI, {
useUnifiedTopology: true
});
const db = mongoClient.db();

// Guild Count cache
const guildCountCacheCollection = db.collection("guildcountcaches");
const guildCountCacheCollectionSize = await guildCountCacheCollection.countDocuments();

const guildCountCacheCursor = guildCountCacheCollection.find();

let guildCountCachesProcessed = 0;
while (
(await guildCountCacheCursor.hasNext()) &&
guildCountCachesProcessed < guildCountCacheCollectionSize
) {
const cachedCount = await guildCountCacheCursor.next();

cachedCount.timestamp = new Date(cachedCount.timestamp);

delete cachedCount._id;
delete cachedCount.__v;

await guildCountCacheCollection
.findOneAndUpdate({ id: cachedCount.id }, { $set: cachedCount })
.catch(console.error);

guildCountCachesProcessed++;
process.stdout.write(
`\r[${guildCountCachesProcessed} of ${guildCountCacheCollectionSize}] Documents Processed (Guild Count cache)`
);
}

process.stdout.write("\nDone\n");
process.exit(0);
})();
6 changes: 1 addition & 5 deletions src/bot.ts
Expand Up @@ -38,7 +38,6 @@ class Bot {

const Intents = Eris.Constants.Intents;
const intents: number[] = [
Intents.guildMembers,
Intents.guilds,
Intents.guildBans,
Intents.guildMessages,
Expand All @@ -47,11 +46,8 @@ class Bot {
Intents.directMessageReactions
];

if (PREMIUM_BOT)
intents.push(Intents.guildPresences, Intents.guildVoiceStates);

const erisOptions: Eris.ClientOptions = {
getAllUsers: PREMIUM_BOT,
getAllUsers: false,
guildCreateTimeout: 15000,
intents: intents.reduce((acc, cur) => acc | cur, 0),
maxShards: DISTRIBUTED ? TOTAL_SHARDS : 1,
Expand Down
10 changes: 0 additions & 10 deletions src/commands/counts.ts
Expand Up @@ -38,16 +38,6 @@ const counts: Command = {
value: await counts.processCounter("bannedMembers"),
inline: true
},
{
name: languagePack.commands.counts.bots,
value: await counts.processCounter("bots"),
inline: true
},
{
name: languagePack.commands.counts.connectedUsers,
value: await counts.processCounter("connectedmembers"),
inline: true
},
{
name: languagePack.commands.counts.channels,
value: await counts.processCounter("channels"),
Expand Down
17 changes: 15 additions & 2 deletions src/commands/status.ts
Expand Up @@ -2,6 +2,7 @@ import os from "os";
import * as packageJSON from "../../package.json";
import Command from "../typings/Command";
import embedBase from "../utils/embedBase";
import GuildCountCacheModel from "../models/GuildCountCache";

const parseUptime = (inputDate: number) => {
// inputDate must be in seconds
Expand Down Expand Up @@ -120,8 +121,20 @@ const status: Command = {
inline: true
},
{
name: "**Cached users / In guilds:**",
value: stats.userStats,
name: "**Users:**",
value:
(
await GuildCountCacheModel.aggregate([
{
$group: {
_id: null,
total: {
$sum: "$members"
}
}
}
])
)[0]?.total ?? "Unknown",
inline: true
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/counters/Twitter.ts
Expand Up @@ -20,7 +20,7 @@ const twitterClient = new Twitter({
const TwitterCounter: Counter = {
aliases: ["twitterFollowers", "twitterName"],
isPremium: true,
isEnabled: true,
isEnabled: false,
lifetime: 5 * 60 * 1000,
execute: async ({ unparsedArgs: resource }) => {
if (!TWITTER_ACCESS_TOKEN)
Expand Down
2 changes: 0 additions & 2 deletions src/counters/all.ts
Expand Up @@ -12,7 +12,6 @@ import MemberCounter from "./members";
import MembersPlayingCounter from "./membersPlaying";
import MembersConnectedCounter from "./membersConnected";
import MembersExtendedCounter from "./membersExt";
import MembersOnlineApproximatedCounter from "./membersOnlineApproximated";
import MembersWithRoleCounter from "./membersWithRole";
import MemeratorCounter from "./Memerator";
import NitroBoostersCounter from "./nitroBoosters";
Expand Down Expand Up @@ -41,7 +40,6 @@ const counters = [
MembersPlayingCounter,
MembersConnectedCounter,
MembersExtendedCounter,
MembersOnlineApproximatedCounter,
MembersWithRoleCounter,
MemeratorCounter,
NitroBoostersCounter,
Expand Down
14 changes: 13 additions & 1 deletion src/counters/bot-stats.ts
@@ -1,3 +1,4 @@
import GuildCountCacheModel from "../models/GuildCountCache";
import Counter from "../typings/Counter";

const BotStatsCounter: Counter = {
Expand All @@ -11,7 +12,18 @@ const BotStatsCounter: Counter = {
let guilds = stats.guilds;

return {
["member-counter-users"]: users,
["member-counter-users"]: (
await GuildCountCacheModel.aggregate([
{
$group: {
_id: null,
total: {
$sum: "$members"
}
}
}
])
)[0].total,
["member-counter-guilds"]: guilds
};
}
Expand Down
29 changes: 26 additions & 3 deletions src/counters/members.ts
@@ -1,12 +1,35 @@
import GuildCountCacheModel from "../models/GuildCountCache";
import Counter from "../typings/Counter";
import { CounterError } from "../utils/Constants";

const MemberCounter: Counter = {
aliases: ["members", "count"],
aliases: [
"members",
"count",
"approximatedOnlineMembers",
"offlineMembers",
"onlineMembers"
],
isPremium: false,
isEnabled: true,
lifetime: 0,
execute: async ({ guild }) => {
return guild.memberCount;
execute: async ({ guild, client }) => {
let guildCountCache = await GuildCountCacheModel.findOne({
id: guild.id
});

if (guildCountCache) {
const { members, onlineMembers } = guildCountCache;
return {
count: members,
members,
onlineMembers,
offlineMembers: members - onlineMembers,
approximatedOnlineMembers: onlineMembers
};
} else {
throw CounterError.NOT_AVAILABLE;
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/counters/membersConnected.ts
Expand Up @@ -4,7 +4,7 @@ import Eris from "eris";
const MembersConnectedCounter: Counter = {
aliases: ["connectedMembers", "membersConnected"],
isPremium: true,
isEnabled: true,
isEnabled: false,
lifetime: 0,
execute: async ({ guild, args }) => {
const targetChannels = args[0] ?? [];
Expand Down
4 changes: 1 addition & 3 deletions src/counters/membersExt.ts
Expand Up @@ -4,15 +4,13 @@ const MembersExtendedCounter: Counter = {
aliases: [
"bots",
"users",
"onlinemembers",
"offlinemembers",
"onlineusers",
"offlineusers",
"onlinebots",
"offlinebots"
],
isPremium: true,
isEnabled: true,
isEnabled: false,
lifetime: 0,
execute: async ({ guild }) => {
const counts = {
Expand Down
2 changes: 1 addition & 1 deletion src/counters/membersPlaying.ts
Expand Up @@ -3,7 +3,7 @@ import Counter from "../typings/Counter";
const MembersPlayingCounter: Counter = {
aliases: ["membersplaying"],
isPremium: true,
isEnabled: true,
isEnabled: false,
lifetime: 0,
execute: async ({ client, guild, args }) => {
let count = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/counters/membersWithRole.ts
Expand Up @@ -7,7 +7,7 @@ const MembersWithRoleCounter: Counter = {
"offlineMembersWithRole"
],
isPremium: true,
isEnabled: true,
isEnabled: false,
lifetime: 0,
execute: async ({ client, guild, args }) => {
const targetRoles: string[] = args[0];
Expand Down
15 changes: 15 additions & 0 deletions src/events/guildCreate.ts
Expand Up @@ -2,10 +2,13 @@ import getEnv from "../utils/getEnv";
import Eris from "eris";
import GuildService from "../services/GuildService";
import { availableLanguagePacks } from "../utils/languagePack";
import GuildCountCacheModel from "../models/GuildCountCache";

const { PREMIUM_BOT_ID, PREMIUM_BOT, UNRESTRICTED_MODE } = getEnv();

const guildCreate = async (guild: Eris.Guild) => {
// @ts-ignore
const client: Eris.Client = guild._client;
const guildSettings = await GuildService.init(guild.id);

if (guildSettings.blocked) {
Expand All @@ -31,6 +34,18 @@ const guildCreate = async (guild: Eris.Guild) => {
if (availableLanguagePacks.includes(languageToSet)) {
await guildSettings.setLanguage(languageToSet);
}

const guildCountCache = await GuildCountCacheModel.findOneAndUpdate(
{ id: guild.id },
{},
{ upsert: true, new: true }
);
const guildExt = await client.getRESTGuild(guild.id, true);

guildCountCache.members = guildExt.approximateMemberCount;
guildCountCache.onlineMembers = guildExt.approximatePresenceCount;
guildCountCache.timestamp = new Date();
await guildCountCache.save();
};

export default guildCreate;
4 changes: 3 additions & 1 deletion src/jobs/all.ts
Expand Up @@ -7,6 +7,7 @@ import freeMemory from "./freeMemory";
import postBotStatus from "./postBotStats";
import setBotStatus from "./setBotSatus";
import updateCounters from "./updateCounters";
import fetchMemberCounts from "./fetchMemberCounts";

const jobs: Job[] = [
checkBlockedGuilds,
Expand All @@ -15,7 +16,8 @@ const jobs: Job[] = [
freeMemory,
setBotStatus,
postBotStatus,
updateCounters
updateCounters,
fetchMemberCounts
];

export default jobs;

0 comments on commit 0597b8f

Please sign in to comment.