From 94a447921ddfb1a00904d5ab62cd8e4972c438bb Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Tue, 22 Dec 2020 15:40:19 +0530 Subject: [PATCH] feat: member screening types update --- src/models/rest.ts | 1 + src/structures/message.ts | 4 ++ src/structures/textChannel.ts | 80 +++++++++++++++++++++++++++++++++++ src/test/music.ts | 14 +++++- src/test/slash-only.ts | 44 ++++++------------- src/types/guild.ts | 4 ++ src/types/slash.ts | 2 +- 7 files changed, 115 insertions(+), 34 deletions(-) diff --git a/src/models/rest.ts b/src/models/rest.ts index 95aa94b..29b708c 100644 --- a/src/models/rest.ts +++ b/src/models/rest.ts @@ -410,6 +410,7 @@ export class RESTManager { const query = method === 'get' && body !== undefined ? Object.entries(body as any) + .filter(([k, v]) => v !== undefined) .map( ([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent( diff --git a/src/structures/message.ts b/src/structures/message.ts index d8e6325..3c6501e 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -46,6 +46,10 @@ export class Message extends Base { flags?: number stickers?: MessageSticker[] + get createdAt(): Date { + return new Date(this.timestamp) + } + constructor( client: Client, data: MessagePayload, diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index 699b4ad..f63763a 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -3,6 +3,7 @@ import { Client } from '../models/client.ts' import { GuildTextChannelPayload, MessageOption, + MessagePayload, MessageReference, ModifyGuildTextChannelOption, ModifyGuildTextChannelPayload, @@ -14,6 +15,7 @@ import { CHANNEL_MESSAGE, CHANNEL_MESSAGES } from '../types/endpoint.ts' +import { Collection } from '../utils/collection.ts' import { Channel } from './channel.ts' import { Embed } from './embed.ts' import { Guild } from './guild.ts' @@ -124,6 +126,48 @@ export class TextChannel extends Channel { await res.mentions.fromPayload(newMsg) return res } + + /** + * Fetch Messages of a Channel + * @param options Options to configure fetching Messages + */ + async fetchMessages(options?: { + limit?: number + around?: Message | string + before?: Message | string + after?: Message | string + }): Promise> { + const res = new Collection() + const raws = (await this.client.rest.api.channels[this.id].messages.get({ + limit: options?.limit ?? 50, + around: + options?.around === undefined + ? undefined + : typeof options.around === 'string' + ? options.around + : options.around.id, + before: + options?.before === undefined + ? undefined + : typeof options.before === 'string' + ? options.before + : options.before.id, + after: + options?.after === undefined + ? undefined + : typeof options.after === 'string' + ? options.after + : options.after.id + })) as MessagePayload[] + + for (const raw of raws) { + await this.messages.set(raw.id, raw) + const msg = ((await this.messages.get(raw.id)) as unknown) as Message + res.set(msg.id, msg) + } + + return res + } } export class GuildTextChannel extends TextChannel { @@ -185,4 +229,40 @@ export class GuildTextChannel extends TextChannel { return new GuildTextChannel(this.client, resp, this.guild) } + + /** + * Bulk Delete Messages in a Guild Text Channel + * @param messages Messages to delete. Can be a number, or Array of Message or IDs + */ + async bulkDelete( + messages: Array | number + ): Promise { + let ids: string[] = [] + + if (Array.isArray(messages)) + ids = messages.map((e) => (typeof e === 'string' ? e : e.id)) + else { + let list = await this.messages.array() + if (list.length < messages) list = (await this.fetchMessages()).array() + ids = list + .sort((b, a) => a.createdAt.getTime() - b.createdAt.getTime()) + .filter((e, i) => i < messages) + .filter( + (e) => + new Date().getTime() - e.createdAt.getTime() <= + 1000 * 60 * 60 * 24 * 14 + ) + .map((e) => e.id) + } + + ids = [...new Set(ids)] + if (ids.length < 2 || ids.length > 100) + throw new Error('bulkDelete can only delete messages in range 2-100') + + await this.client.rest.api.channels[this.id].messages['bulk-delete'].post({ + messages: ids + }) + + return this + } } diff --git a/src/test/music.ts b/src/test/music.ts index 854aa77..bf654af 100644 --- a/src/test/music.ts +++ b/src/test/music.ts @@ -7,7 +7,8 @@ import { groupslash, CommandContext, Extension, - Collection + Collection, + GuildTextChannel } from '../../mod.ts' import { LL_IP, LL_PASS, LL_PORT, TOKEN } from './config.ts' import { @@ -69,6 +70,17 @@ class MyClient extends CommandClient { d.respond({ content: 'sub-cmd-group worked' }) } + @command() + rmrf(ctx: CommandContext): any { + if (ctx.author.id !== '422957901716652033') return + ;((ctx.channel as any) as GuildTextChannel) + .bulkDelete(3) + .then((chan) => { + ctx.channel.send(`Bulk deleted 2 in ${chan}`) + }) + .catch((e) => ctx.channel.send(`${e.message}`)) + } + @slash() run(d: Interaction): void { console.log(d.name) diff --git a/src/test/slash-only.ts b/src/test/slash-only.ts index 00bb12b..1798eee 100644 --- a/src/test/slash-only.ts +++ b/src/test/slash-only.ts @@ -1,36 +1,16 @@ -import { SlashClient, SlashBuilder } from '../models/slashClient.ts' +import { SlashClient } from '../models/slashClient.ts' +import { SlashCommandPartial } from '../types/slash.ts' import { TOKEN } from './config.ts' -const slash = new SlashClient({ token: TOKEN }) +export const slash = new SlashClient({ token: TOKEN }) -slash.commands.all().then(console.log) +// Cmd objects come here +const commands: SlashCommandPartial[] = [] -const cmd = new SlashBuilder() - .name('searchmusic') - .description('Search for music.') - .option((o) => - o.string({ name: 'query', description: 'Query to search with.' }) - ) - .option((o) => - o.string({ - name: 'engine', - description: 'Search engine to use.', - choices: [{ name: 'YouTube', value: 'youtube' }, 'Spotify'] - }) - ) - .options({ - query: { - description: 'Query UWU', - type: 3 - }, - engine: { - description: 'Engine UWU', - type: 3, - choices: [ - { name: 'YouTube', value: 'youtube' }, - { name: 'Spotify', value: 'spotify' } - ] - } - }) - -console.log(JSON.stringify(cmd.export())) +console.log('Creating...') +commands.forEach((cmd) => { + slash.commands + .create(cmd, '!! Your testing guild ID comes here !!') + .then((c) => console.log(`Created command ${c.name}!`)) + .catch((e) => `Failed to create ${cmd.name} - ${e.message}`) +}) diff --git a/src/types/guild.ts b/src/types/guild.ts index b2b7b68..6825398 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -62,6 +62,7 @@ export interface MemberPayload { premium_since?: string deaf: boolean mute: boolean + pending?: boolean } export enum MessageNotification { @@ -113,6 +114,9 @@ export type GuildFeatures = | 'FEATURABLE' | 'ANIMATED_ICON' | 'BANNER' + | 'WELCOME_SCREEN_ENABLED' + | 'MEMBER_VERIFICATION_GATE_ENABLED' + | 'PREVIEW_ENABLED' export enum IntegrationExpireBehavior { REMOVE_ROLE = 0, diff --git a/src/types/slash.ts b/src/types/slash.ts index b56f100..1bd83ba 100644 --- a/src/types/slash.ts +++ b/src/types/slash.ts @@ -50,7 +50,7 @@ export interface SlashCommandChoice { /** (Display) name of the Choice */ name: string /** Actual value to be sent in Interaction */ - value: string + value: any } export enum SlashCommandOptionType {