Skip to content

Commit

Permalink
chore: remove unnecessary features that require privileged intents (#352
Browse files Browse the repository at this point in the history
)

* chore: remove unnecessary features that require intents

* chore(handleTicketDelete): remove the function for retrieving messages

* chore: remove eslint warnings
  • Loading branch information
CarelessInternet authored Sep 23, 2023
1 parent faf8360 commit be7139a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 64 deletions.
2 changes: 0 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { Client, type Handler } from './types';
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_BANS
],
partials: [Constants.PartialTypes.MESSAGE, Constants.PartialTypes.REACTION],
Expand Down
8 changes: 1 addition & 7 deletions src/utils/handleTicketCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ const handleRest = async (
subject: string
) => {
try {
const onlineManagers = managers.members.filter(
(manager) => manager.presence?.status === 'online'
);
const presences = onlineManagers.map((manager) => `🟢 ${userMention(manager.id)}`);

const embeds: MessageEmbed[] = [];

const channelEmbed = new MessageEmbed()
Expand All @@ -155,8 +150,7 @@ const handleRest = async (
.setTitle('Support Ticket')
.setDescription(`${userMention(interaction.user.id)} created a new support ticket`)
.addField('Subject', subject)
.addField('Available Managers', presences?.length ? presences.join('\n') : 'Unknown', true)
.addField('Ticket Date', time(channel.createdAt ?? new Date(), 'R'), true)
.addField('Ticket Date', time(channel.createdAt ?? new Date(), 'R'))
.setTimestamp();

if (record.LogsChannel !== '0') {
Expand Down
58 changes: 3 additions & 55 deletions src/utils/handleTicketDelete.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { userMention } from '@discordjs/builders';
import {
MessageAttachment,
MessageEmbed,
type Snowflake,
TextChannel,
type ThreadChannel,
type CommandInteraction,
type MessageComponentInteraction,
type Message
type MessageComponentInteraction
} from 'discord.js';
import { version } from '../../package.json';
import type { Tables } from '../types';
Expand Down Expand Up @@ -114,33 +110,10 @@ export const handleTicketDelete = async (
if (!logsChannel?.isText()) return;
if (!logsChannel.permissionsFor(interaction.guild!.me!).has(['SEND_MESSAGES'])) return;

const allMessages = await fetchMessages(interaction.channel);
const messages = allMessages.map((msg) => {
if (msg.author.id !== interaction.client.user!.id) {
return `${msg.author.tag}: ${msg.content}\n`;
}
});

const firstMessage = allMessages[0];
const subject =
firstMessage?.author.id === interaction.client.user!.id
? firstMessage.embeds?.[0]?.fields?.[0]?.value
: 'Unknown';

const content = `Subject: ${subject}\n\n` + messages.join('');
const attachment = new MessageAttachment(
Buffer.from(content, 'utf8'),
`Ticketer-${Date.now()}.txt`
);

embed.setDescription(`${userMention(interaction.user.id)} deleted a ticket`);
embed.addField('Name of Ticket', interaction.channel.name);
embed.addField(
'Message History',
'The message history can be found in the attachment above'
);

logsChannel.send({ embeds: [embed], files: [attachment] });
logsChannel.send({ embeds: [embed] });

const member = interaction.guild!.members.resolve(authorId);

Expand All @@ -151,7 +124,7 @@ export const handleTicketDelete = async (
}`
);

member.send({ embeds: [embed], files: [attachment] }).catch(() => false);
member.send({ embeds: [embed] }).catch(() => false);
}
}

Expand All @@ -167,28 +140,3 @@ export const handleTicketDelete = async (
console.error(err);
}
};

const fetchMessages = async (
channel: TextChannel | ThreadChannel,
messages: Message[] = [],
messageId?: Snowflake
): Promise<Message[]> => {
// limit is 100: https://discord.com/developers/docs/resources/channel#get-channel-messages
const limit = 100;

const fetchedMessages = await channel.messages.fetch(
{
limit,
...(messageId && { before: messageId })
},
{ cache: false, force: true }
);
const allMessages = [...messages, ...fetchedMessages.map((msg) => msg)];

// don't fetch more than 1000 messages for rate limit reasons
if (fetchedMessages.size === limit && messages.length < 1_000 - limit) {
return fetchMessages(channel, allMessages, fetchedMessages.lastKey());
}

return allMessages.sort((a, b) => a.createdTimestamp - b.createdTimestamp);
};

0 comments on commit be7139a

Please sign in to comment.