Skip to content

Commit

Permalink
ok i promise it works now for realsies
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleylamont committed Feb 15, 2024
1 parent 40ebb19 commit 8d06738
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
54 changes: 34 additions & 20 deletions src/baserow-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
BaserowWebhook,
} from "./baserow-types";
import express, { Express, Request } from "express";
import { Guild, GuildMember, Snowflake } from "discord.js";
import { GuildMember, Snowflake } from "discord.js";

const baserowData: Map<number, BaserowItem> = new Map();
const MEMBER_ROLE_ID = "753524901708693558";
Expand Down Expand Up @@ -38,6 +38,11 @@ export async function refreshBaserowData() {
// Fetch data from baserow
// Store in baserowData
// Fire events for onRowCreate, onRowUpdate, onRowDelete as necessary

// Force update all discord members
await cssaGuild.fetch();
await cssaGuild.members.fetch();

const fetchedData: BaserowItem[] = [];
let nextUrl: string | null =
"https://baserow.cssa.club/api/database/rows/table/511/";
Expand Down Expand Up @@ -77,8 +82,6 @@ export async function refreshBaserowData() {
}

// Drop any members not in the fetched data
const cssaGuild = await discordClient.guilds.fetch(CSSA_SERVER_ID);
if (!cssaGuild) throw new Error("Couldn't fetch CSSA guild data.");
const memberRole = await cssaGuild.roles.fetch(MEMBER_ROLE_ID);
if (!memberRole) throw new Error("Couldn't get member role.");
const members = memberRole.members;
Expand All @@ -99,7 +102,9 @@ export async function refreshBaserowData() {
eventPromises.push(member.roles.remove(memberRole));
}

return Promise.all(eventPromises);
return Promise.all(eventPromises).then(() => {
console.log("Baserow data refreshed.");
});
}

export async function attachBaserowWebhookListener(expressApp: Express) {
Expand Down Expand Up @@ -175,20 +180,22 @@ export async function attachBaserowWebhookListener(expressApp: Express) {

class MemberNotFoundError extends Error {}

async function getGuildAndUser(
guildId: string,
rawUsername: string,
): Promise<[Guild, GuildMember]> {
async function getUser(rawUsername: string): Promise<GuildMember> {
const [username, discriminator] = transformUsername(rawUsername);
const guild = await discordClient.guilds.fetch(guildId);
if (!guild) throw new Error("Couldn't fetch guild data.");
const matchingMembers = await guild.members.fetch({ query: username });
for (const member of matchingMembers.values()) {
for (const member of cssaGuild.members.cache.values()) {
if (
member.user.username === username &&
member.user.discriminator === discriminator.toString(10)
)
return [guild, member];
return member;
}
const fetchedMembers = await cssaGuild.members.fetch({ query: username });
for (const member of fetchedMembers.values()) {
if (
member.user.username === username &&
member.user.discriminator === discriminator.toString(10)
)
return member;
}
console.log(`Couldn't find member in guild: ${username}#${discriminator}`);
throw new MemberNotFoundError("Couldn't find member in guild: " + username);
Expand All @@ -199,17 +206,24 @@ export async function performRoleUpdate(
roleType: "member" | "lifeMember",
operation: "add" | "remove",
) {
const [guild, member] = await getGuildAndUser(CSSA_SERVER_ID, username);
const role = await guild.roles.fetch(
roleType === "member" ? MEMBER_ROLE_ID : LIFE_MEMBER_ROLE_ID,
);
if (!role) throw new Error("Couldn't get role.");
const member = await getUser(username);
const roleId = roleType === "member" ? MEMBER_ROLE_ID : LIFE_MEMBER_ROLE_ID;
const alreadySatisfied =
operation === "add"
? member.roles.cache.has(roleId)
: !member.roles.cache.has(roleId);
if (alreadySatisfied) {
console.log(
`Role update not required: ${operation} ${roleType} for ${username}`,
);
return;
}
console.log(
`Performing role update: ${operation} ${roleType} for ${username}`,
);
await (operation === "add"
? member.roles.add(role)
: member.roles.remove(role));
? member.roles.add(roleId)
: member.roles.remove(roleId));
}

export async function onRowCreate(row: BaserowItem) {
Expand Down
2 changes: 0 additions & 2 deletions src/door-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export async function attachDoorServer(app: Express) {
);

// Get the channel from the CSSA discord server
const cssaGuild = await discordClient.guilds.fetch("476382037620555776");

const statusChannel =
(await cssaGuild.channels.fetch("1060799214550007849")) ?? undefined;
if (statusChannel === undefined)
Expand Down
3 changes: 2 additions & 1 deletion src/global-context.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable no-var */
// noinspection ES6ConvertVarToLetConst

import { Client, GuildMember } from "discord.js";
import { Client, Guild, GuildMember } from "discord.js";

declare global {
var discordClient: Client<true>;
var cssaGuild: Guild;
var appMaintainers: GuildMember[];
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ async function main(): Promise<void> {
// with any subsequent code using it that the value will exist and be of the correct type.
// This means that this line must be executed before any other code that uses globalThis.discordClient.
globalThis.discordClient = await initDiscord();
globalThis.cssaGuild = await discordClient.guilds.fetch(
process.env.CSSA_SERVER!,
);
await registerCommands();

// Initialise the express app and attach the door server
Expand Down
3 changes: 1 addition & 2 deletions src/server-icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export function startServerIcon(): void {

async function setNewIcon(): Promise<void> {
const newIcon = await getNewIcon();
const cssa = await discordClient.guilds.fetch("476382037620555776");
await cssa.setIcon(newIcon);
await cssaGuild.setIcon(newIcon);
}

async function getNewIcon(): Promise<Buffer> {
Expand Down

0 comments on commit 8d06738

Please sign in to comment.