-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,51 @@ | ||
import { Client, Message, MessageEmbed, Permissions } from "discord.js"; | ||
import { Client, Message, MessageEmbed, NewsChannel, Permissions, TextChannel, ThreadChannel } from "discord.js"; | ||
|
||
|
||
export default function expand(client: Client, msg: Message) { | ||
return async msgurl => { | ||
let ids = msgurl.split('/').slice(4); | ||
if (ids[0] != msg.guild.id) { | ||
msg.channel.send(`\`${msgurl}\`\nis not from this server. I could not expand it.`) | ||
.catch(e => console.error(e)); | ||
return; | ||
} | ||
|
||
let cnl = await msg.guild.channels.fetch(ids[1]); | ||
let allowed = cnl.permissionsFor(client.user).has(Permissions.FLAGS.READ_MESSAGE_HISTORY); | ||
if (!cnl.isText()) { | ||
msg.channel.send(`I didn't have permission to see \n\`${msgurl}\`.\nI could not expand it.`) | ||
.catch(e => console.error(e)); | ||
return; | ||
} | ||
if (!allowed) { | ||
msg.channel.send(`I didn't have permission to see \n\`${msgurl}\`.\nI could not expand it.`) | ||
.catch(e => console.error(e)); | ||
return; | ||
} | ||
|
||
let target = await cnl.messages.fetch(ids[2]); | ||
|
||
let name; | ||
if (target.member) { | ||
name = target.member.displayName; | ||
}else{ | ||
name = target.author.username; | ||
} | ||
export default function expand(client: Client, message: Message) { | ||
async function getMessage(url: string): Promise<[TextChannel | NewsChannel | ThreadChannel, 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.`); | ||
|
||
let channel = await message.guild.channels.fetch(ids[1]); | ||
if (!(channel.isText() || channel.isThread())) throw new Error(`\`${channel}\` is not a text channel.`); | ||
|
||
let allowed = channel.permissionsFor(client.user).has(Permissions.FLAGS.READ_MESSAGE_HISTORY); | ||
if (!allowed) throw new Error(`I didn't have permission to see \n\`${url}\`.\nI could not expand it.`); | ||
|
||
return [channel, await channel.messages.fetch(ids[2])]; | ||
} | ||
|
||
return async (url: string) => { | ||
let [channel, target] = await getMessage(url); | ||
|
||
let name = target.member ? target.member.displayName : target.author.username; | ||
|
||
let channel_name = ''; | ||
if (cnl.parent.parent) { | ||
channel_name += cnl.parent.parent.name + ' > '; | ||
} | ||
if (cnl.parent) { | ||
channel_name += cnl.parent.name + ' > '; | ||
} | ||
channel_name += cnl.name; | ||
let channelDesc = ''; | ||
if (channel.parent.parent) channelDesc += channel.parent.parent.name + ' > '; | ||
if (channel.parent) channelDesc += channel.parent.name + ' > '; | ||
channelDesc += channel.name; | ||
|
||
let msgembed = new MessageEmbed({ | ||
let embed = new MessageEmbed({ | ||
author: { | ||
name: name, | ||
icon_url: target.author.avatarURL() | ||
icon_url: target.author.displayAvatarURL() | ||
}, | ||
description: target.content, | ||
timestamp: target.createdAt, | ||
footer: { | ||
text: channel_name, | ||
icon_url: msg.guild.iconURL() | ||
text: channelDesc, | ||
icon_url: message.guild.iconURL() | ||
} | ||
}); | ||
|
||
let attach = target.attachments.find(att => att.width > 0); | ||
if (attach) msgembed.setImage(attach.url); | ||
if (attach) embed.setImage(attach.url); | ||
|
||
let embeds = target.embeds; | ||
if (embeds.length) msgembed.description += `\n(${embeds.length} ${embeds.length == 1 ? 'embed follows.' : 'embeds follow.'})`; | ||
if (embeds.length) embed.description += `\n(${embeds.length} ${embeds.length == 1 ? 'embed follows.' : 'embeds follow.'})`; | ||
|
||
msg.channel.send({ | ||
embeds: [msgembed].concat(embeds) | ||
message.channel.send({ | ||
embeds: [embed, ...embeds] | ||
}).catch(e => console.error(e)); | ||
}; | ||
} | ||
} |