Skip to content

Commit

Permalink
Merge pull request #2 from Rodentman87/0.11.0
Browse files Browse the repository at this point in the history
0.11.0
  • Loading branch information
Rodentman87 authored Aug 9, 2022
2 parents b0211ea + f03fffe commit db6d620
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 359 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slashasaurus",
"version": "0.10.4",
"version": "0.11.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
Expand All @@ -12,21 +12,20 @@
"@types/node": "^17.0.21",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"discord.js": "^13.7.0",
"discord.js": "^14.0.1",
"eslint": "^8.16.0",
"prettier": "^2.3.2",
"rimraf": "^3.0.2",
"typescript": "4.5.5"
},
"peerDependencies": {
"discord.js": "^13.7.0"
"discord.js": "^14.0.1"
},
"repository": {
"type": "git",
"url": "https://github.com/Rodentman87/slashasaurus.git"
},
"dependencies": {
"@discordjs/builders": "^0.15.0",
"@discordjs/rest": "^0.5.0",
"discord-api-types": "^0.33.0"
}
Expand Down
8 changes: 4 additions & 4 deletions src/ContextMenuBase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
MessageContextMenuInteraction,
UserContextMenuInteraction,
MessageContextMenuCommandInteraction,
UserContextMenuCommandInteraction,
} from 'discord.js';
import type { LocalizationMap } from 'discord-api-types/v10';
import { SlashasaurusClient } from './SlashasaurusClient';
Expand All @@ -16,11 +16,11 @@ type ContextCommandOptions<T extends 'MESSAGE' | 'USER'> = {
export type ContextMenuHandlerType<T extends 'MESSAGE' | 'USER'> =
T extends 'MESSAGE'
? (
interaction: MessageContextMenuInteraction,
interaction: MessageContextMenuCommandInteraction,
client: SlashasaurusClient
) => void
: (
interaction: UserContextMenuInteraction,
interaction: UserContextMenuCommandInteraction,
client: SlashasaurusClient
) => void;

Expand Down
2 changes: 2 additions & 0 deletions src/OptionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type StringChoiceResolvableType =

interface StringOptionsData extends BaseApplicationCommandOptionsData<string> {
readonly type: StringChoiceResolvableType;
readonly minLength?: number;
readonly maxLength?: number;
}

interface StringChoiceOptionsData
Expand Down
80 changes: 30 additions & 50 deletions src/Page.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { EmbedBuilder } from '@discordjs/builders';
import {
BaseCommandInteraction,
ButtonInteraction,
CommandInteraction,
ButtonInteraction,
InteractionWebhook,
Message,
MessageActionRow,
MessageActionRowComponent,
MessageButton,
MessageComponentInteraction,
MessageEmbed,
MessageOptions,
MessagePayload,
SelectMenuInteraction,
TextBasedChannel,
WebhookEditMessageOptions,
ActionRowBuilder,
APIEmbed,
MessageActionRowComponentBuilder,
Embed,
} from 'discord.js';
import { MessageButtonStyles } from 'discord.js/typings/enums';
import { PageActionRow, PageButton, PageSelect } from './PageComponents';
import { SlashasaurusClient } from './SlashasaurusClient';
import { MaybePromise } from './utilityTypes';
Expand All @@ -42,6 +41,7 @@ type PageComponentRows = (PageComponentArray | PageActionRow)[];
export interface RenderedPage
extends Omit<MessageOptions, 'nonce' | 'components'> {
components?: PageComponentRows;
embeds?: (APIEmbed | EmbedBuilder)[];
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -128,7 +128,7 @@ export abstract class Page<
}

sendAsReply(
interaction: MessageComponentInteraction | BaseCommandInteraction,
interaction: MessageComponentInteraction | CommandInteraction,
ephemeral = false
) {
this.client.replyToInteractionWithPage(this, interaction, ephemeral);
Expand Down Expand Up @@ -205,12 +205,13 @@ export type DeserializeStateFn<
export function pageComponentRowsToComponents<P, S>(
rows: PageComponentRows,
page: Page<P, S>
): MessageActionRow[] {
): ActionRowBuilder<MessageActionRowComponentBuilder>[] {
page.clearHandlers();
const pageId = page.constructor.pageId;
return rows
.map((row) => {
const actionRow = new MessageActionRow<MessageActionRowComponent>();
const actionRow =
new ActionRowBuilder<MessageActionRowComponentBuilder>();
if (row instanceof PageActionRow) {
row.children.forEach((component) => {
actionRow.addComponents(
Expand All @@ -228,8 +229,9 @@ export function pageComponentRowsToComponents<P, S>(
}
return actionRow;
})
.filter<MessageActionRow>(
(e): e is MessageActionRow => e instanceof MessageActionRow
.filter<ActionRowBuilder<MessageActionRowComponentBuilder>>(
(e): e is ActionRowBuilder<MessageActionRowComponentBuilder> =>
e instanceof ActionRowBuilder
);
}

Expand All @@ -243,11 +245,7 @@ function componentToDjsComponent<P, S>(
`~${pageId};${page.registerHandler(component.handler)}`
);
} else {
return new MessageButton({
...component,
style: MessageButtonStyles.LINK,
type: 'BUTTON',
});
return component.toDjsComponent();
}
}

Expand Down Expand Up @@ -284,18 +282,14 @@ export function compareMessages(

// Check Embeds
if (
// @ts-expect-error There's some weird stuff with generic intersections that breaks here, but this is fine
a.embeds.filter((e) => e.type === 'rich').length !== (b.embeds ?? []).length
a.embeds.filter((e) => e.data.type === 'rich').length !==
(b.embeds ?? []).length
)
return false;
if (a.embeds.length > 0) {
if (
!(b.embeds ?? []).every((bEmbedData, index) => {
const bEmbed =
bEmbedData instanceof MessageEmbed
? bEmbedData
: new MessageEmbed(bEmbedData);
// return bEmbed.equals(a.embeds[index]);
const bEmbed = 'data' in bEmbedData ? bEmbedData.data : bEmbedData;
return embedsAreEqual(a.embeds[index], bEmbed);
})
) {
Expand All @@ -306,15 +300,9 @@ export function compareMessages(
return true;
}

function embedsAreEqual(
a: MessageComponentInteraction['message']['embeds'][number],
b: MessageEmbed
) {
if (a.type !== 'rich') return true;

function embedsAreEqual(a: Embed, b: APIEmbed) {
if (
a.title !== (b.title ? b.title.trim() : b.title) ||
a.type !== b.type ||
a.description !== (b.description ? b.description.trim() : b.description) ||
a.url !== b.url ||
(a.color ?? 0) !== b.color
Expand All @@ -329,35 +317,25 @@ function embedsAreEqual(

// Compare authors
const headerIconUrl =
a.author &&
('iconURL' in a.author
? a.author.iconURL
: 'icon_url' in a.author
? a.author.icon_url
: undefined);
a.author && ('iconURL' in a.author ? a.author.iconURL : undefined);
if (
a.author &&
b.author &&
(a.author.name !== b.author.name?.trim() ||
headerIconUrl !== b.author.iconURL ||
headerIconUrl !== b.author.icon_url ||
a.author.url !== b.author.url)
)
return false;
else if ((a.author && !b.author) || (b.author && !a.author)) return false;

// Compare footers
const footerIconUrl =
a.footer &&
('iconURL' in a.footer
? a.footer.iconURL
: 'icon_url' in a.footer
? a.footer.icon_url
: undefined);
a.footer && ('iconURL' in a.footer ? a.footer.iconURL : undefined);
if (
a.footer &&
b.footer &&
(a.footer?.text !== b.footer?.text?.trim() ||
footerIconUrl !== b.footer?.iconURL)
footerIconUrl !== b.footer?.icon_url)
)
return false;
else if ((a.footer && !b.footer) || (b.footer && !a.footer)) return false;
Expand All @@ -377,11 +355,13 @@ function embedsAreEqual(
return false;

// Compare fields
if ((a.fields ?? []).length !== b.fields.length) return false;
return (a.fields ?? []).every(
const aFields = a.fields ?? [];
const bFields = b.fields ?? [];
if (aFields.length !== bFields.length) return false;
return aFields.every(
(f, i) =>
f.inline === b.fields[i].inline &&
f.name === b.fields[i].name?.trim() &&
f.value === b.fields[i].value?.trim()
f.inline === bFields[i].inline &&
f.name === bFields[i].name?.trim() &&
f.value === bFields[i].value?.trim()
);
}
Loading

0 comments on commit db6d620

Please sign in to comment.