feat: add scheduled events unit tests

This commit is contained in:
Skillz4Killz
2021-11-17 03:21:53 +00:00
committed by GitHub
parent 6fce04fc16
commit f08edcd5fc
10 changed files with 291 additions and 162 deletions

View File

@@ -1,12 +1,30 @@
import { Bot } from "../../../bot.ts";
import { CreateScheduledEvent, ScheduledEvent } from "../../../types/guilds/scheduledEvents.ts";
import {
CreateScheduledEvent,
ScheduledEvent,
ScheduledEventEntityType,
ScheduledEventPrivacyLevel,
} from "../../../types/guilds/scheduledEvents.ts";
/** Create a guild scheduled event in the guild. A guild can have a maximum of 100 events with `SCHEDULED` or `ACTIVE` status at any time. */
export async function createScheduledEvent(bot: Bot, guildId: bigint, options: CreateScheduledEvent) {
// TODO: validate name length
// TODO: validate description length
// TODO: validate location length
// TODO: validate speaker ids length
if (!bot.utils.validateLength(options.name, { min: 1, max: 100 }))
throw new Error("Name must be between 1-100 characters.");
if (options.description && !bot.utils.validateLength(options.description, { max: 1000 }))
throw new Error("Description must be below 1000 characters.");
if (options.location) {
if (!bot.utils.validateLength(options.location, { max: 100 }))
throw new Error("Location must be below 100 characters.");
if (options.entityType === ScheduledEventEntityType.Voice)
throw new Error("Location can not be provided for a Voice event.");
}
if (options.entityType === ScheduledEventEntityType.External) {
if (!options.scheduledEndTime) throw new Error("A scheduled end time is required when making an External event.");
if (!options.location) throw new Error("A location is required when making an External event.");
}
if (options.scheduledStartTime && options.scheduledEndTime && options.scheduledStartTime > options.scheduledEndTime) {
throw new Error("Cannot schedule event to end before starting.");
}
const event = await bot.rest.runMethod<ScheduledEvent>(
bot.rest,
@@ -14,15 +32,12 @@ export async function createScheduledEvent(bot: Bot, guildId: bigint, options: C
bot.constants.endpoints.GUILD_SCHEDULED_EVENTS(guildId),
{
channel_id: options.channelId?.toString(),
entity_metadata:
options.location || options.speakerIds
? { location: options.location, speakerIds: options.speakerIds?.map((id) => id.toString()) }
: undefined,
entity_metadata: options.location ? { location: options.location } : undefined,
name: options.name,
description: options.description,
scheduled_start_time: new Date(options.scheduledStartTime).toISOString(),
scheduled_end_time: options.scheduledEndTime ? new Date(options.scheduledEndTime).toISOString() : undefined,
privacy_level: options.privacyLevel,
privacy_level: options.privacyLevel || ScheduledEventPrivacyLevel.GuildOnly,
entity_type: options.entityType,
}
);

View File

@@ -1,5 +1,5 @@
import { Bot } from "../../../bot.ts";
import { EditScheduledEvent, ScheduledEvent } from "../../../types/guilds/scheduledEvents.ts";
import { EditScheduledEvent, ScheduledEvent, ScheduledEventEntityType } from "../../../types/guilds/scheduledEvents.ts";
/** Modify a guild scheduled event. To start or end an event, use this endpoint to modify the event's status. */
export async function editScheduledEvent(
@@ -8,21 +8,23 @@ export async function editScheduledEvent(
eventId: bigint,
options: Partial<EditScheduledEvent>
) {
// TODO: validate name length
// TODO: validate description length
// TODO: validate location length
// TODO: validate speaker ids length
if (options.name && !bot.utils.validateLength(options.name, { min: 1, max: 100 }))
throw new Error("Name must be between 1-100 characters.");
if (options.description && !bot.utils.validateLength(options.description, { max: 1000 }))
throw new Error("Description must be below 1000 characters.");
if (options.location && !bot.utils.validateLength(options.location, { max: 100 }))
throw new Error("Location must be below 100 characters.");
if (options.scheduledStartTime && options.scheduledEndTime && options.scheduledStartTime > options.scheduledEndTime) {
throw new Error("Cannot schedule event to end before starting.");
}
const event = await bot.rest.runMethod<ScheduledEvent>(
bot.rest,
"patch",
bot.constants.endpoints.GUILD_SCHEDULED_EVENT(guildId, eventId),
{
channel_id: options.channelId?.toString(),
entity_metadata:
options.location || options.speakerIds
? { location: options.location, speakerIds: options.speakerIds?.map((id) => id.toString()) }
: undefined,
channel_id: options.channelId === null ? null : options.channelId?.toString(),
entity_metadata: options.location ? { location: options.location } : undefined,
name: options.name,
description: options.description,
scheduled_start_time: options.scheduledStartTime ? new Date(options.scheduledStartTime).toISOString() : undefined,

View File

@@ -5,7 +5,7 @@ export function processRateLimitedPaths(rest: RestManager) {
const now = Date.now();
for (const [key, value] of rest.ratelimitedPaths.entries()) {
rest.debug(`[REST - processRateLimitedPaths] Running for of loop.`);
rest.debug(`[REST - processRateLimitedPaths] Running for of loop. ${value.resetTimestamp - now}`);
// IF THE TIME HAS NOT REACHED CANCEL
if (value.resetTimestamp > now) continue;

View File

@@ -85,7 +85,8 @@ export function transformGuild(
),
id: guildId,
ownerId: bot.transformers.snowflake(payload.guild.owner_id),
// WEIRD EDGE CASE WITH BOT CREATED SERVERS
ownerId: payload.guild.owner_id ? bot.transformers.snowflake(payload.guild.owner_id) : 0n,
permissions: payload.guild.permissions ? bot.transformers.snowflake(payload.guild.permissions) : 0n,
afkChannelId: payload.guild.afk_channel_id ? bot.transformers.snowflake(payload.guild.afk_channel_id) : undefined,
widgetChannelId: payload.guild.widget_channel_id

View File

@@ -20,7 +20,6 @@ export function transformScheduledEvent(
scheduledStartTime: Date.parse(payload.scheduled_start_time),
scheduledEndTime: payload.scheduled_end_time ? Date.parse(payload.scheduled_end_time) : undefined,
entityId: payload.entity_id ? bot.transformers.snowflake(payload.entity_id) : undefined,
speakerIds: payload.entity_metadata?.speaker_ids?.map((id) => bot.transformers.snowflake(id)),
creator: payload.creator ? bot.transformers.user(bot, payload.creator) : undefined,
name: payload.name,
@@ -58,8 +57,6 @@ export interface DiscordenoScheduledEvent {
entityType: ScheduledEventEntityType;
/** any additional id of the hosting entity associated with event */
entityId?: bigint;
/** the speakers of the stage channel */
speakerIds?: bigint[];
/** location of the event */
location?: string;
/** the user that created the scheduled event */

View File

@@ -34,15 +34,14 @@ export interface ScheduledEvent {
}
export enum ScheduledEventPrivacyLevel {
/** the scheduled event is public and available in discovery */
Public = 1,
/** the scheduled event is public and available in discovery. DISCORD DEVS DISABLED THIS! WILL ERROR IF USED! */
// Public = 1,
/** the scheduled event is only accessible to guild members */
GuildOnly,
GuildOnly = 2,
}
export enum ScheduledEventEntityType {
None,
StageInstance,
StageInstance = 1,
Voice,
External,
}
@@ -55,8 +54,6 @@ export enum ScheduledEventStatus {
}
export interface ScheduledEventEntityMetadata {
/** the speakers of the stage channel */
speakerIds?: string[];
/** location of the event */
location?: string;
}
@@ -82,8 +79,6 @@ export interface ScheduledEventUserAdd {
export interface CreateScheduledEvent {
/** the channel id of the scheduled event */
channelId?: bigint;
/** the speakers of the stage channel */
speakerIds?: bigint[];
/** location of the event */
location?: string;
/** the name of the scheduled event */
@@ -95,7 +90,7 @@ export interface CreateScheduledEvent {
/** the time the scheduled event will end if it does end. */
scheduledEndTime?: number;
/** the privacy level of the scheduled event */
privacyLevel: ScheduledEventPrivacyLevel;
privacyLevel?: ScheduledEventPrivacyLevel;
/** the type of hosting entity associated with a scheduled event */
entityType: ScheduledEventEntityType;
}
@@ -103,8 +98,6 @@ export interface CreateScheduledEvent {
export interface EditScheduledEvent {
/** the channel id of the scheduled event */
channelId: bigint;
/** the speakers of the stage channel */
speakerIds: bigint[];
/** location of the event */
location: string;
/** the name of the scheduled event */
@@ -126,8 +119,6 @@ export interface EditScheduledEvent {
export interface EditScheduledEvent {
/** the channel id of the scheduled event */
channelId: bigint;
/** the speakers of the stage channel */
speakerIds: bigint[];
/** location of the event */
location: string;
/** the name of the scheduled event */