Merge branch 'main' of https://github.com/pandaninjas/bot into main

This commit is contained in:
PandaNinjas 2023-01-17 08:57:45 -08:00
commit 5181e31012
No known key found for this signature in database
GPG key ID: 0BD7553044D47763
2 changed files with 21 additions and 10 deletions

View file

@ -10,14 +10,14 @@ import { reuploadCommands } from './_reupload';
import * as BuildConfig from './constants'; import * as BuildConfig from './constants';
import { parseLog } from './logs'; import { parseLog } from './logs';
import { getLatestMinecraftVersion } from './utils/remoteVersions'; import { getLatestMinecraftVersion } from './utils/remoteVersions';
import { expandDiscordLink } from "./utils/resolveMessage"; import { expandDiscordLink } from './utils/resolveMessage';
import { membersCommand } from './commands/members'; import { membersCommand } from './commands/members';
import { starsCommand } from './commands/stars'; import { starsCommand } from './commands/stars';
import { modrinthCommand } from './commands/modrinth'; import { modrinthCommand } from './commands/modrinth';
import { tagsCommand } from './commands/tags'; import { tagsCommand } from './commands/tags';
import { jokeCommand } from './commands/joke'; import { jokeCommand } from './commands/joke';
import { roryCommand } from "./commands/rory"; import { roryCommand } from './commands/rory';
import random from 'just-random'; import random from 'just-random';
import { green, bold, yellow, cyan } from 'kleur/colors'; import { green, bold, yellow, cyan } from 'kleur/colors';
@ -122,7 +122,7 @@ client.on('interactionCreate', async (interaction) => {
await tagsCommand(interaction); await tagsCommand(interaction);
} else if (commandName === 'joke') { } else if (commandName === 'joke') {
await jokeCommand(interaction); await jokeCommand(interaction);
} else if (commandName === "rory") { } else if (commandName === 'rory') {
await roryCommand(interaction); await roryCommand(interaction);
} }
} }

View file

@ -6,11 +6,11 @@ import {
EmbedBuilder, EmbedBuilder,
type Message, type Message,
ThreadChannel, ThreadChannel,
} from "discord.js"; } from 'discord.js';
function findFirstImage(message: Message): string | undefined { function findFirstImage(message: Message): string | undefined {
const result = message.attachments.find((attach) => { const result = message.attachments.find((attach) => {
return attach.contentType?.startsWith("image/"); return attach.contentType?.startsWith('image/');
}); });
if (result == undefined) { if (result == undefined) {
return undefined; return undefined;
@ -23,16 +23,20 @@ export async function expandDiscordLink(message: Message): Promise<void> {
const re = const re =
/(https?:\/\/)?(?:canary\.|ptb\.)?discord(?:app)?\.com\/channels\/(?<server_id>\d+)\/(?<channel_id>\d+)\/(?<message_id>\d+)/g; /(https?:\/\/)?(?:canary\.|ptb\.)?discord(?:app)?\.com\/channels\/(?<server_id>\d+)\/(?<channel_id>\d+)\/(?<message_id>\d+)/g;
let execResult = re.exec(message.content); let execResult = re.exec(message.content);
while (!(execResult == null || execResult.groups == undefined)) { while (!(execResult == null || execResult.groups == undefined)) {
if (execResult.groups.server_id != message.guildId) { if (execResult.groups.server_id != message.guildId) {
continue; // do not let the bot leak messages from one server to another continue; // do not let the bot leak messages from one server to another
} }
const channel = await message.guild?.channels.fetch( const channel = await message.guild?.channels.fetch(
execResult.groups.channel_id execResult.groups.channel_id
); );
if (channel == undefined || channel == null || !channel.isTextBased()) { if (channel == undefined || channel == null || !channel.isTextBased()) {
continue; continue;
} }
if (channel instanceof ThreadChannel) { if (channel instanceof ThreadChannel) {
if ( if (
!channel.parent?.members?.some((user) => user.id == message.author.id) !channel.parent?.members?.some((user) => user.id == message.author.id)
@ -44,10 +48,12 @@ export async function expandDiscordLink(message: Message): Promise<void> {
continue; // do not reveal a message to a user who can't see it continue; // do not reveal a message to a user who can't see it
} }
} }
try { try {
const messageToShow = await channel.messages.fetch( const messageToShow = await channel.messages.fetch(
execResult.groups.message_id execResult.groups.message_id
); );
const builder = new EmbedBuilder() const builder = new EmbedBuilder()
.setAuthor({ .setAuthor({
name: `${messageToShow.author.username}#${messageToShow.author.discriminator}`, name: `${messageToShow.author.username}#${messageToShow.author.discriminator}`,
@ -58,27 +64,32 @@ export async function expandDiscordLink(message: Message): Promise<void> {
builder.setDescription(messageToShow.content); builder.setDescription(messageToShow.content);
} }
if (messageToShow.attachments.size > 0) { if (messageToShow.attachments.size > 0) {
let attachmentsString = ""; let attachmentsString = '';
messageToShow.attachments.forEach((value) => { messageToShow.attachments.forEach((value) => {
attachmentsString += `[${value.name}](${value.url}) `; attachmentsString += `[${value.name}](${value.url}) `;
}); });
builder.addFields({ name: "Attachments", value: attachmentsString });
builder.addFields({ name: 'Attachments', value: attachmentsString });
const firstImage = findFirstImage(messageToShow); const firstImage = findFirstImage(messageToShow);
if (firstImage != undefined) { if (firstImage != undefined) {
builder.setImage(firstImage); builder.setImage(firstImage);
} }
} }
const row = new ActionRowBuilder().addComponents(
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder() new ButtonBuilder()
.setLabel("Jump to original message") .setLabel('Jump to original message')
.setStyle(ButtonStyle.Link) .setStyle(ButtonStyle.Link)
.setURL(messageToShow.url) .setURL(messageToShow.url)
); );
message.channel.send({ embeds: [builder], components: [row] });
await message.channel.send({ embeds: [builder], components: [row] });
} catch (e) { } catch (e) {
console.error(e); console.error(e);
continue; continue;
} }
execResult = re.exec(message.content); execResult = re.exec(message.content);
} }
} }