Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
12 changes: 7 additions & 5 deletions src/events/guild_member/guildMemberVoiceUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export const guildMemberVoiceUpdate = new Event({
}

const settings = await GuildSetting.findOne({ guildId: guild.id });
// check that logging channel ID is set
// Check that logging channel ID is set
const loggingChannelId = settings?.logging.voiceUpdatesChannelId;
if (!loggingChannelId) return;

// check that logging channel exists in guild
// Check that logging channel exists in guild
const loggingChannel = await getGuildChannel(guild, loggingChannelId);
if (!loggingChannel?.isSendable()) return;

Expand All @@ -107,7 +107,7 @@ export const guildMemberVoiceUpdate = new Event({
* @param title - Title for the embed
* @param description - description for the embed
* @param color - Color for the embed
* @returns embed builder
* @returns - Embed builder
*/
function vcLogEmbed(
member: GuildMember,
Expand All @@ -125,9 +125,10 @@ function vcLogEmbed(
}

/**
* Marks member's attendance
*
* @param channelId
* @param member
* @param channelId Voice chatroom ID
* @param member Member changing state
*/
async function markAttendance(
channelId: string,
Expand All @@ -136,6 +137,7 @@ async function markAttendance(
) {
try {
await dbConnect();
// Grabing Scheduled Event object by channel ID
const res: IScheduledEvent = (await ScheduledEvent.findOne({
channelId: channelId,
status: 2,
Expand Down
10 changes: 10 additions & 0 deletions src/events/guild_scheduled_event/guildScheduledEventDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ import {
} from "../../models/ScheduledEvent.js";
import dbConnect from "../../util/libmongo.js";

/**
* `guildScheduledEventDelete`
* handles the {@link Events.guildScheduledEventDelete}
* {@link Event}.
* Flags scheduled event as canceled in database.
*/

export const guildScheduledEventDelete = new Event({
name: Events.GuildScheduledEventDelete,
execute: async (event) => {
console.log("deleting");
await dbConnect();
// Finds event by ID
const res: IScheduledEvent = (
await ScheduledEvent.find({ eventId: event.id }).sort({ _id: -1 }).exec()
)[0] as IScheduledEvent;
// Flags event for deletion and then save
res.status = GuildScheduledEventStatus.Canceled;
res.save();
// Old record inseted into logs
await logScheduledEvent(res);
},
});
13 changes: 11 additions & 2 deletions src/events/guild_scheduled_event/guildScheduledEventUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import {
} from "../../models/ScheduledEvent.js";
import dbConnect from "../../util/libmongo.js";

/**
* `guildScheduledEventUpdate` handles the {@link Events.guildScheduledEventUpdate}
* {@link Event}.
* Updates schelude event in database.
*/
// Using the oldEvent and newEvent parameters, the function updates the currently saved oldEvent with newEvent.
export const guildScheduledEventUpdate = new Event({
name: Events.GuildScheduledEventUpdate,
execute: async (oldEvent, newEvent) => {
Expand All @@ -17,7 +23,7 @@ export const guildScheduledEventUpdate = new Event({
await dbConnect();

let res;

// After the update completes, the updated event is logged
if (oldEvent.isScheduled() && newEvent.isActive()) {
console.log("Starting Event: " + newEvent.id);
await new Promise((r) => setTimeout(r, 2000));
Expand Down Expand Up @@ -65,7 +71,7 @@ export const guildScheduledEventUpdate = new Event({
scheduledStart: newEvent.scheduledStartAt,
name: newEvent.name,
status: newEvent.status,
})) as IScheduledEvent; //maybe this should return null
})) as IScheduledEvent; // Maybe this should return null
} else {
res.recurrence = newEvent.recurrenceRule ? true : false;
res.thumbnailUrl =
Expand All @@ -80,6 +86,8 @@ export const guildScheduledEventUpdate = new Event({
}
}

// If the current event is a recurring event that is active, and the updated event is scheduled. It will update the recurring event ending today and log it.

if (!res.recurrence) {
if (oldEvent.isActive() && newEvent.isCompleted()) {
console.log("ending one time event: " + newEvent.id);
Expand All @@ -88,6 +96,7 @@ export const guildScheduledEventUpdate = new Event({
await logScheduledEvent(res);
}
} else {
// If the event is one-time and the Oldevent is marked active, and the new event is marked completed. It will update the one-time event ending today and log it.
if (oldEvent.isActive() && newEvent.isScheduled()) {
console.log("ending recurring event: " + newEvent.id);
res.endedAt = new Date(Date.now());
Expand Down
31 changes: 18 additions & 13 deletions src/features/logging/scheduledEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,32 @@ import dbConnect from "../../util/libmongo.js";
import { ScheduledEventWrapper } from "../../util/scheduledEventWrapper.js";

/**
* Records schelude event object in logs
* @param event Sheduled Event Object
* @param guild ??
* @param forceNew ??
*
* @param event
* @param guild
* @param forceNew
*/
export async function logScheduledEvent(event: IScheduledEvent) {
await dbConnect();
// Gets guild object by guild ID from event object
const guild: Guild = await client.guilds.fetch(event.guildId);
// Get settings by guild ID
const settings = await GuildSetting.findOne({ guildId: guild.id }).exec();

const logChannelId = settings?.logging.eventLogChannelId;
// Checks if log channel ID exists
if (!logChannelId) return;
// Finds channel from guid's channels cache by log Channel Id
let logChannel = guild.channels.cache.get(logChannelId);
if (!logChannel) {
logChannel = (await guild.channels.fetch(logChannelId)) ?? undefined;
}

// Check if channel type is GuildText
if (logChannel?.type !== ChannelType.GuildText) return;
let existingPost = undefined;
if (event.logMessageId) {
//console.log("finding existing post");
// console.log("finding existing post");
existingPost = logChannel.messages.cache.get(event.logMessageId);
if (!existingPost) {
existingPost = await logChannel.messages
Expand All @@ -61,12 +66,12 @@ export async function logScheduledEvent(event: IScheduledEvent) {
}
}

//console.log("fetched post");
// console.log("fetched post");
//console.log(existingPost);

if (existingPost) {
//console.log("editing existing post...");
//console.log("event ended at: " + event.endedAt);
// console.log("editing existing post...");
// console.log("event ended at: " + event.endedAt);
const { cont } = await logContainer(event);
const files = [];
if (event.thumbnailUrl === "attachment://image.jpg")
Expand All @@ -91,7 +96,7 @@ export async function logScheduledEvent(event: IScheduledEvent) {
allowedMentions: { parse: [] },
});
event.logMessageId = post.id;
//console.log("event log message id: " + event.logMessageId);
// console.log("event log message id: " + event.logMessageId);
await event.save();
}
}
Expand All @@ -102,12 +107,12 @@ export async function logScheduledEvent(event: IScheduledEvent) {
*/
async function logContainer(event: IScheduledEvent) {
const wrapper = new ScheduledEventWrapper(event);
let attendees = wrapper.attendancePercentages();
const attendees = wrapper.attendancePercentages();
const attendeesCount = wrapper.uniqueAttendees();
await wrapper.writeCsvDump();
//if attendees.length > 30 then replace inline list with text file
//todo: figure out how to generate text file
//todo: add some file output for attachments in this function; wire it up to the main log function
// If attendees.length > 30 then replace inline list with text file
// TODO: figure out how to generate text file
// TODO: add some file output for attachments in this function; wire it up to the main log function
const attendeesStr =
attendees.length > 0 && attendees.length < 30
? attendees
Expand Down
4 changes: 3 additions & 1 deletion src/models/ScheduledEvent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { GuildScheduledEventStatus, Snowflake } from "discord.js";
import mongoose, { Document, Model, Schema } from "mongoose";

/**
* {@link IScheduledEvent}
*/
export interface IScheduledEvent extends Document {
recurrence: boolean;
eventUrl: string;
Expand Down
Binary file added src/util/.DS_Store
Binary file not shown.
18 changes: 11 additions & 7 deletions src/util/scheduledEventWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ import createCsvWriter from "csv-writer";
import { GuildMember, GuildScheduledEventStatus, time } from "discord.js";
import { client } from "../index.js";
import { IScheduledEvent } from "../models/ScheduledEvent.js";

/**
*
* Wrapper class that returns a Schelude event for end user comsumption
* @returns{@link ScheduledEventWrapper}
*/
export class ScheduledEventWrapper {
event: IScheduledEvent;

statusColor = () => {
let color: number;
switch (this.event.status) {
case 1: // scheduled = completed = blue
case 1: // Scheduled = completed = blue
color = 0x3498db;
break;
case 2: // active = green
case 2: // Active = green
color = 0x57f386;
break;
case 3: // completed = blue
case 3: // Completed = blue
color = 0x3498db;
break;
case 4: // cancelled = red
case 4: // Cancelled = red
color = 0xed4245;
break;
default: // undefined = white
default: // Undefined = white
color = 0xffffff;
}

Expand Down Expand Up @@ -223,7 +227,7 @@ export class ScheduledEventWrapper {

private async getAttendeeNames(ids: string[]) {
const buffer = [];
let names: Map<string, string> = new Map();
const names: Map<string, string> = new Map();
for (let i = 0; i < Math.ceil(ids.length / 100); i++) {
const slice = ids.slice(
i * 100,
Expand Down