Skip to content

Commit

Permalink
adapt embed view (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: HunteRoi <me@tinaeldevresse.eu>
  • Loading branch information
HunteRoi and HunteRoi authored Sep 4, 2022
1 parent 5853904 commit accc92d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 66 deletions.
23 changes: 17 additions & 6 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Breaking Changes

## Support for Discord.js v14

The entire library has been ported to djs v14.

## MailboxManager class

The mailbox manager becomes a real tickets manager. It now only handles user tickets.
For compatibility purpose, a `MessageBasedMailboxManager` class has been created. This last one works the same way the manager did in v2.

## Format Title parameter

Expand All @@ -16,8 +19,8 @@ function formatTitle(id: string): string {
}

// after
function formatTitle(ticket: Ticket): string {
return `Ticket ${ticket.id}`;
function formatTitle(ticket: Ticket, guild: Guild): string {
return `Ticket ${ticket.id} - ${guild.name}`;
}
```

Expand Down Expand Up @@ -88,7 +91,15 @@ If you want to still use it, you can perform the same action through the `replyS
This event was initially created to let the user know a replt has been deleted but the deletion behaviour has ben removed so thus event has no purpose anymore.
It has thus been removed.

## options#sendToRecipient Change
## options#sendToRecipient Removal

The `sendToRecipient` property of the options has been removed. Indeed, users have the fundamental right to receive and keep a copy of the conversation with anyone.

## options#mailboxChannel Removal

The `mailboxChannel` property has been removed in profit of a `mailboxChannels` property to enable the management of several guilds by a single manager.

## options#loggingOptions#channel Removal

The `channel` property of the `loggingOptions` property has been removed in profit of a `channels` property to enable the management of several guilds by a single manager.

The `sendToRecipient` property of the options has been deprecated and changed to optional.
Indeed, users have the fundamental right to receive and keep a copy of the conversation with anyone.
2 changes: 1 addition & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const manager = new InteractionBasedMailboxManager(client, {
},
forceCloseEmoji: '❌',
replySentEmoji: '📤',
formatTitle: (ticket, guild) => `Ticket for ${guild.name}`,
formatTitle: (ticket, guild) => `Ticket ${ticket.id} for ${guild.name} (${guild.id})`,
replyMessage:
'Please use the "reply" feature to send an answer to this message.',
closedChannelPrefix: '[Closed] ',
Expand Down
84 changes: 39 additions & 45 deletions src/InteractionBasedMailboxManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
Ticket,
UserTickets,
} from './types';
import { arrowDown } from './utils/constants';
import { isNullOrWhiteSpaces, truncate } from './utils/StringUtils';

/**
Expand Down Expand Up @@ -140,7 +139,7 @@ export class InteractionBasedMailboxManager extends MailboxManager {
await Promise.all(
this.client.guilds.cache.map(async (guild: Guild) =>
guild.members.cache.has(user.id) ||
(await guild.members.fetch(user.id)) !== null
(await guild.members.fetch(user.id)) !== null
? guild
: null
)
Expand Down Expand Up @@ -211,27 +210,23 @@ export class InteractionBasedMailboxManager extends MailboxManager {
const logMessage =
typeof content === 'string'
? ({
content,
files: [
{
attachment: Buffer.from(logs.join('\n')),
name: this.options.loggingOptions.generateFilename(ticket),
},
],
} as MessageOptions)
content,
files: [
{
attachment: Buffer.from(logs.join('\n')),
name: this.options.loggingOptions.generateFilename(ticket),
},
],
} as MessageOptions)
: {
...content,
files: [
{
attachment: Buffer.from(logs.join('\n')),
name: this.options.loggingOptions.generateFilename(ticket),
},
],
};

if (this.options.loggingOptions.sendToRecipient) {
await ticket.createdBy.send(logMessage);
}
...content,
files: [
{
attachment: Buffer.from(logs.join('\n')),
name: this.options.loggingOptions.generateFilename(ticket),
},
],
};

if (this.options.loggingOptions.sendInThread && ticket.threadId !== null) {
const thread = (await this.client.channels.fetch(
Expand All @@ -247,10 +242,12 @@ export class InteractionBasedMailboxManager extends MailboxManager {
const logChannel =
typeof guildLogChannel === 'string'
? ((await this.client.channels.fetch(
guildLogChannel
)) as GuildTextBasedChannel)
guildLogChannel
)) as GuildTextBasedChannel)
: guildLogChannel;
await logChannel?.send(logMessage);

await ticket.createdBy.send(logMessage);
}

async #onCreateButtonInteraction(
Expand Down Expand Up @@ -338,8 +335,8 @@ export class InteractionBasedMailboxManager extends MailboxManager {
const guildChannel =
typeof mailboxChannel === 'string'
? ((await this.client.channels.fetch(
mailboxChannel
)) as BaseGuildTextChannel)
mailboxChannel
)) as BaseGuildTextChannel)
: mailboxChannel;
let thread: ThreadChannel | null = null;
if (
Expand Down Expand Up @@ -372,15 +369,12 @@ export class InteractionBasedMailboxManager extends MailboxManager {

const guild = await this.client.guilds.fetch(ticket.guildId!);
const header = this.options.formatTitle(ticket, guild);
const description = ticket.lastMessage.cleanContent;
const prefix =
isSentToAdmin || this.options.loggingOptions?.showSenderNames
? `${ticket.lastMessage.author.username}:\n`
: null;
const suffix = `\n\n**${this.options.replyMessage}**`;
const description = `${!isNullOrWhiteSpaces(prefix) ? prefix : ''}${
ticket.lastMessage.cleanContent
}${!isNullOrWhiteSpaces(suffix) ? suffix : ''}`;

const footer = `ID: ${ticket.lastMessage.id}`;

const replyButtonRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
Expand All @@ -401,22 +395,22 @@ export class InteractionBasedMailboxManager extends MailboxManager {

return this.options.embedOptions
? {
embeds: [
new EmbedBuilder(this.options.embedOptions)
.setAuthor({
name: header,
iconURL: isSentToAdmin ? arrowDown : undefined,
})
.setDescription(description)
.setFooter({ text: footer })
.setTimestamp(),
],
components: [replyButtonRow],
}
embeds: [
new EmbedBuilder(this.options.embedOptions)
.setAuthor({
name: isSentToAdmin ? ticket.lastMessage.author.username : guild.name,
iconURL: isSentToAdmin ? ticket.lastMessage.author.displayAvatarURL() : guild.iconURL() ?? undefined,
})
.setDescription(description)
.setFooter({ text: header })
.setTimestamp(),
],
components: [replyButtonRow],
}
: {
content: `${header}\n\n​${description}\n\n​${footer}`,
components: [replyButtonRow],
};
content: `${header}\n\n${!isNullOrWhiteSpaces(prefix) ? prefix : ''}${description}${!isNullOrWhiteSpaces(suffix) ? suffix : ''}\n\n​${footer}`,
components: [replyButtonRow],
};
}

async #generateModal(
Expand Down
8 changes: 0 additions & 8 deletions src/types/LoggingOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ export type LoggingOptions = {
*/
showSenderNames: boolean;

/**
* @deprecated will be removed in next minor update.
* Users have the right to have a copy of the conversation.
*
* @type {boolean | undefined}
*/
sendToRecipient?: boolean;

/**
* The channel in which the logs are sent.
*
Expand Down
6 changes: 0 additions & 6 deletions src/utils/constants.ts

This file was deleted.

0 comments on commit accc92d

Please sign in to comment.