refactor!: move dirs outside of src/ (#2032)

This commit is contained in:
Skillz4Killz
2022-02-11 04:49:53 -05:00
committed by GitHub
parent 471ef5cb6c
commit 8aaea9f339
594 changed files with 84 additions and 66 deletions
@@ -0,0 +1,51 @@
import { Bot } from "../../../bot.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) {
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,
"post",
bot.constants.endpoints.GUILD_SCHEDULED_EVENTS(guildId),
{
channel_id: options.channelId?.toString(),
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 || ScheduledEventPrivacyLevel.GuildOnly,
entity_type: options.entityType,
reason: options.reason,
},
);
return bot.transformers.scheduledEvent(bot, event);
}
@@ -0,0 +1,10 @@
import { Bot } from "../../../bot.ts";
/** Delete a scheduled event. */
export async function deleteScheduledEvent(bot: Bot, guildId: bigint, eventId: bigint) {
await bot.rest.runMethod<undefined>(
bot.rest,
"delete",
bot.constants.endpoints.GUILD_SCHEDULED_EVENT(guildId, eventId),
);
}
@@ -0,0 +1,43 @@
import { Bot } from "../../../bot.ts";
import { EditScheduledEvent, ScheduledEvent } 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(
bot: Bot,
guildId: bigint,
eventId: bigint,
options: Partial<EditScheduledEvent>,
) {
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 === 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,
scheduled_end_time: options.scheduledEndTime ? new Date(options.scheduledEndTime).toISOString() : undefined,
privacy_level: options.privacyLevel,
entity_type: options.entityType,
status: options.status,
reason: options.reason,
},
);
return bot.transformers.scheduledEvent(bot, event);
}
@@ -0,0 +1,19 @@
import { Bot } from "../../../bot.ts";
import { ScheduledEvent } from "../../../types/guilds/scheduledEvents.ts";
/** Get a guild scheduled event. */
export async function getScheduledEvent(
bot: Bot,
guildId: bigint,
eventId: bigint,
options?: { withUserCount?: boolean },
) {
const event = await bot.rest.runMethod<ScheduledEvent>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_SCHEDULED_EVENT(guildId, eventId),
{ with_user_count: options?.withUserCount || false },
);
return bot.transformers.scheduledEvent(bot, event);
}
@@ -0,0 +1,57 @@
import { Bot } from "../../../bot.ts";
import { DiscordenoMember, DiscordenoUser } from "../../../transformers/member.ts";
import { GetScheduledEventUsers } from "../../../types/guilds/scheduledEvents.ts";
import { GuildMember } from "../../../types/members/guildMember.ts";
import { User } from "../../../types/users/user.ts";
import { Collection } from "../../../util/collection.ts";
export async function getScheduledEventUsers(
bot: Bot,
guildId: bigint,
eventId: bigint,
options?: GetScheduledEventUsers & { withMember?: false },
): Promise<Collection<bigint, DiscordenoUser>>;
export async function getScheduledEventUsers(
bot: Bot,
guildId: bigint,
eventId: bigint,
options?: GetScheduledEventUsers & { withMember: true },
): Promise<Collection<bigint, { user: DiscordenoUser; member: DiscordenoMember }>>;
export async function getScheduledEventUsers(
bot: Bot,
guildId: bigint,
eventId: bigint,
options?: GetScheduledEventUsers,
): Promise<
Collection<bigint, DiscordenoUser> | Collection<bigint, { user: DiscordenoUser; member: DiscordenoMember }>
> {
// TODO: is the guild member omit user
const result = await bot.rest.runMethod<{ user: User; member?: GuildMember }[]>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_SCHEDULED_EVENT_USERS(guildId, eventId),
{
limit: options?.limit,
with_members: options?.withMember,
},
);
if (!options?.withMember) {
return new Collection(
result.map((res) => {
const user = bot.transformers.user(bot, res.user);
return [user.id, user];
}),
);
}
return new Collection(
result.map((res) => {
const user = bot.transformers.user(bot, res.user);
const member = bot.transformers.member(bot, res.member!, guildId, user.id);
return [user.id, { member, user }];
}),
);
}
@@ -0,0 +1,23 @@
import { Bot } from "../../../bot.ts";
import { DiscordenoScheduledEvent } from "../../../transformers/scheduledEvent.ts";
import { GetScheduledEvents, ScheduledEvent } from "../../../types/guilds/scheduledEvents.ts";
import { Collection } from "../../../util/collection.ts";
/** Get a list of guild scheduled event for the given guild. */
export async function getScheduledEvents(bot: Bot, guildId: bigint, options?: GetScheduledEvents) {
const events = await bot.rest.runMethod<ScheduledEvent[]>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_SCHEDULED_EVENTS(guildId),
{
with_user_count: options?.withUserCount,
},
);
return new Collection<bigint, DiscordenoScheduledEvent>(
events.map((e) => {
const event = bot.transformers.scheduledEvent(bot, e);
return [event.id, event];
}),
);
}