Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-ayf committed Dec 20, 2021
1 parent de990db commit 39c22d7
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/expand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Client, Message, MessageEmbed, NewsChannel, Permissions, TextChannel, ThreadChannel } from "discord.js";

import { Client, GuildTextBasedChannel, Message, MessageEmbed, NewsChannel, Permissions, TextChannel, ThreadChannel } from "discord.js";

export default function expand(client: Client, message: Message) {
async function getMessage(url: string): Promise<[TextChannel | NewsChannel | ThreadChannel, Message]> {
async function getMessage(url: string): Promise<[GuildTextBasedChannel, Message]> {
let ids = url.split('/').slice(4);
if (ids[0] != message.guild.id) throw new Error(`\`${url}\`\nis not from this server. I could not expand it.`);

Expand All @@ -15,9 +14,7 @@ export default function expand(client: Client, message: Message) {
return [channel, await channel.messages.fetch(ids[2])];
}

return async (url: string) => {
let [channel, target] = await getMessage(url);

function createEmbeds(channel: GuildTextBasedChannel, target: Message) {
let name = target.member ? target.member.displayName : target.author.username;

let channelDesc = '';
Expand All @@ -44,8 +41,14 @@ export default function expand(client: Client, message: Message) {
let embeds = target.embeds;
if (embeds.length) embed.description += `\n(${embeds.length} ${embeds.length == 1 ? 'embed follows.' : 'embeds follow.'})`;

message.channel.send({
embeds: [embed, ...embeds]
}).catch(e => console.error(e));
return [embed, ...embeds];
}

return async (url: string) => {
let [channel, target] = await getMessage(url);
let embeds = createEmbeds(channel, target);

message.channel.send({ embeds })
.catch(e => console.error(e));
};
}

0 comments on commit 39c22d7

Please sign in to comment.