Skip to content

Commit

Permalink
Mongo refactor minor tweaks, cleanup, fixes, and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Aug 6, 2023
1 parent 6880f76 commit b760340
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 69 deletions.
13 changes: 7 additions & 6 deletions src/components/buzzwords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ export default class Buzzwords extends BotComponent {

async updateRolesSingle(member: Discord.GuildMember) {
if(ENABLED) { // eslint-disable-line @typescript-eslint/no-unnecessary-condition
// TODO: If we ever do this joke again, cache the quantiles from reflowRoles to save an expensive database
// query and computation here
const entries = await this.wheatley.database.buzzword_scoreboard.find().toArray();
const scores = entries.map(entry => entry.score).sort((a, b) => a - b);
const p90 = Buzzwords.quantile(scores, .9);
Expand Down Expand Up @@ -301,8 +303,6 @@ export default class Buzzwords extends BotComponent {
$set: {
user: id,
tag,
},
$max: {
score: points,
},
$inc: {
Expand Down Expand Up @@ -400,13 +400,14 @@ export default class Buzzwords extends BotComponent {
}
}
if(message.content.trim() == "!scoreboard") {
const scoreboard_entries = await this.wheatley.database.buzzword_scoreboard.find().toArray();
const entries = scoreboard_entries.sort((a, b) => b.score - a.score);
const scores = entries.slice(0, 15);
const scoreboard_entries = await this.wheatley.database.buzzword_scoreboard.aggregate([
{ $sort: { score: -1 } },
{ $limit: 15 }
]).toArray() as unknown as buzzword_scoreboard_entry[];
const embed = new Discord.EmbedBuilder()
.setTitle("Scoreboard");
let description = "";
for(const entry of scores) {
for(const entry of scoreboard_entries) {
const tag = entry.tag == "" ? `<@${entry.user}>` : entry.tag;
description += `${tag}: ${round(entry.score, 1)}\n`;
}
Expand Down
13 changes: 2 additions & 11 deletions src/components/modmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ export default class Modmail extends BotComponent {

override async on_ready() {
const singleton = await this.wheatley.database.get_bot_singleton();
if(!singleton.modmail_id_counter) {
await this.wheatley.database.update_bot_singleton({ modmail_id_counter: this.modmail_id_counter });
} else {
// load entries
this.modmail_id_counter = singleton.modmail_id_counter;
}
this.modmail_id_counter = singleton.modmail_id_counter;
}

async create_modmail_thread(interaction: Discord.ModalSubmitInteraction) {
Expand All @@ -59,7 +54,7 @@ export default class Modmail extends BotComponent {
const member = await this.wheatley.TCCPP.members.fetch(interaction.member.user.id);
// make the thread
const id = this.modmail_id_counter++;
await this.update_db();
await this.wheatley.database.update_bot_singleton({ modmail_id_counter: this.modmail_id_counter });
const thread = await this.wheatley.rules_channel.threads.create({
type: Discord.ChannelType.PrivateThread,
invitable: false,
Expand Down Expand Up @@ -228,10 +223,6 @@ export default class Modmail extends BotComponent {
}
}

async update_db() {
await this.wheatley.database.update_bot_singleton({ modmail_id_counter: this.modmail_id_counter });
}

async log_action(interaction_member: Discord.GuildMember | Discord.APIInteractionGuildMember | null,
title: string, body?: string) {
const [ tag, avatar ] = await (async () => {
Expand Down
8 changes: 2 additions & 6 deletions src/components/nodistractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,8 @@ export default class Nodistractions extends BotComponent {

override async on_ready() {
// load entries
for await(const entry of this.wheatley.database.nodistractions.find()) {
this.undistract_queue.push({
user: entry.user,
start: entry.start,
duration: entry.duration
});
for await(const { user, start, duration } of this.wheatley.database.nodistractions.find()) {
this.undistract_queue.push({ user, start, duration });
}

if(this.undistract_queue.length > 0) {
Expand Down
32 changes: 15 additions & 17 deletions src/components/roulette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type roulette_leaderboard_entry = {
highscore: number;
}

const LEADERBOARD_ENTRIES = 20;

/**
* "Russian roulette" game where users risk timing themselves out.
*/
Expand Down Expand Up @@ -69,20 +71,15 @@ export default class Roulette extends BotComponent {
async update_score(user_id: string) {
// todo: not efficient at all
const score = this.streaks.get(user_id)!;
const user_entry = await this.wheatley.database.roulette_leaderboard.findOne({ user: user_id });
// add / update entry
if(!user_entry) {
await this.wheatley.database.roulette_leaderboard.insertOne({
user: user_id,
await this.wheatley.database.roulette_leaderboard.updateOne({ user: user_id }, {
$setOnInsert: {
user: user_id
},
$max: {
highscore: score
});
} else {
await this.wheatley.database.roulette_leaderboard.updateOne({ user: user_id }, {
$max: {
highscore: score
}
});
}
}
}, { upsert: true });
}

async roulette(command: TextBasedCommand) {
Expand Down Expand Up @@ -137,11 +134,12 @@ export default class Roulette extends BotComponent {
.setColor(green)
.setTitle("Roulette Leaderboard");
let description = "";
for(const { user, highscore } of (
(
await this.wheatley.database.roulette_leaderboard.find().toArray()
) as unknown as roulette_leaderboard_entry[]
).sort((a, b) => b.highscore - a.highscore)) {
for(const { user, highscore } of await this.wheatley.database.roulette_leaderboard.aggregate(
[
{ $sort: { highscore: -1 } },
{ $limit: LEADERBOARD_ENTRIES }
]).toArray() as unknown as roulette_leaderboard_entry[]
) {
description += `<@${user}>: ${highscore} roll${highscore == 1 ? "" : "s"} before death\n`;
}
embed.setDescription(description);
Expand Down
14 changes: 7 additions & 7 deletions src/components/starboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default class Starboard extends BotComponent {
true,
"\n\n**[Jump to message!]($$)**"
);
const starboard_entry = await this.wheatley.database.starboard.findOne({ message: message.id });
const starboard_entry = await this.wheatley.database.starboard_entries.findOne({ message: message.id });
if(starboard_entry) {
// edit
const starboard_message = await this.wheatley.starboard_channel.messages.fetch(
Expand All @@ -178,7 +178,7 @@ export default class Starboard extends BotComponent {
content: this.reactions_string(message),
...await make_embeds()
});
await this.wheatley.database.starboard.insertOne({
await this.wheatley.database.starboard_entries.insertOne({
message: message.id,
starboard_entry: starboard_message.id
});
Expand Down Expand Up @@ -265,7 +265,7 @@ export default class Starboard extends BotComponent {
await this.handle_auto_delete(await departialize(reaction.message), reaction);
return;
}
if(await this.wheatley.database.starboard.findOne({ message: reaction.message.id })) {
if(await this.wheatley.database.starboard_entries.findOne({ message: reaction.message.id })) {
// Update counts
await this.update_starboard(await departialize(reaction.message));
} else if(
Expand All @@ -281,7 +281,7 @@ export default class Starboard extends BotComponent {
if(!await this.is_valid_channel(reaction.message.channel)) {
return;
}
if(await this.wheatley.database.starboard.findOne({ message: reaction.message.id })) {
if(await this.wheatley.database.starboard_entries.findOne({ message: reaction.message.id })) {
// Update counts
await this.update_starboard(await departialize(reaction.message));
}
Expand All @@ -295,19 +295,19 @@ export default class Starboard extends BotComponent {
return;
}
assert(old_message.id == new_message.id);
if(await this.wheatley.database.starboard.findOne({ message: old_message.id })) {
if(await this.wheatley.database.starboard_entries.findOne({ message: old_message.id })) {
// Update content
await this.update_starboard(await departialize(new_message));
}
}

override async on_message_delete(message: Discord.Message<boolean> | Discord.PartialMessage) {
const entry = await this.wheatley.database.starboard.findOne({ message: message.id });
const entry = await this.wheatley.database.starboard_entries.findOne({ message: message.id });
if(entry) {
await this.mutex.lock(message.id);
try {
await this.wheatley.starboard_channel.messages.delete(entry.starboard_entry);
await this.wheatley.database.starboard.deleteOne({ message: message.id });
await this.wheatley.database.starboard_entries.deleteOne({ message: message.id });
} finally {
this.mutex.unlock(message.id);
}
Expand Down
8 changes: 6 additions & 2 deletions src/components/the-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ export default class TheButton extends BotComponent {
},
$inc: {
score: points
}
},
},
{
upsert: true,
returnDocument: "after"
}
)).value
);
Expand All @@ -241,7 +245,7 @@ export default class TheButton extends BotComponent {
}
if(interaction.isButton() && interaction.customId == "the-button-scoreboard") {
const scores = await this.wheatley.database.button_scoreboard.aggregate([
{ $sort: { score: 1 } },
{ $sort: { score: -1 } },
{ $limit: 15 }
]).toArray() as button_scoreboard_entry[];
const embed = new Discord.EmbedBuilder()
Expand Down
25 changes: 6 additions & 19 deletions src/infra/database-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,13 @@ import { roulette_leaderboard_entry } from "../components/roulette.js";
import { buzzword_scoreboard_entry } from "../components/buzzwords.js";
import { auto_delete_threshold_notifications, starboard_entry } from "../components/starboard.js";
import { button_scoreboard_entry } from "../components/the-button.js";
import { suggestion_entry } from "../components/server-suggestion-tracker.js";
import { TRACKER_START_TIME, suggestion_entry } from "../components/server-suggestion-tracker.js";
import { link_blacklist_entry } from "../private-types.js";

export class WheatleyDatabase {
private mutex = new Mutex();
private collections = new Map<string, mongo.Collection>();

//public auto_delete_threshold_notifications: mongo.Collection;
//public button_scoreboard: mongo.Collection;
//public buzzword_scoreboard: mongo.Collection;
//public link_blacklist: mongo.Collection;
//public nodistractions: mongo.Collection<no_distraction_entry>;
//public roulette_leaderboard: mongo.Collection;
//public server_suggestions: mongo.Collection;
//public starboard: mongo.Collection;
//public wheatley_entry: mongo.Collection<wheatley_db_info>;

private constructor(
private client: mongo.MongoClient | null,
private db: mongo.Db | null,
Expand Down Expand Up @@ -71,7 +61,7 @@ export class WheatleyDatabase {
const document = {
id: "main",
server_suggestions: {
last_scanned_timestamp: 0
last_scanned_timestamp: TRACKER_START_TIME
},
modmail_id_counter: 0,
the_button: {
Expand Down Expand Up @@ -121,12 +111,9 @@ export type WheatleyDatabaseProxy = WheatleyDatabase & {
nodistractions: mongo.Collection<no_distraction_entry>;
roulette_leaderboard: mongo.Collection<roulette_leaderboard_entry>;
server_suggestions: mongo.Collection<suggestion_entry>;
starboard: mongo.Collection<starboard_entry>;
starboard_entries: mongo.Collection<starboard_entry>;
wheatley: mongo.Collection<wheatley_db_info>;
} & {
[key: string] : Promise<mongo.Collection>
};

export type CollectionProxy = mongo.Collection & {
getSingleton(): Promise<mongo.Document>;
};
// & {
// [key: string] : Promise<mongo.Collection>
//};
2 changes: 1 addition & 1 deletion src/wheatley-private

0 comments on commit b760340

Please sign in to comment.