57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
import Discord from 'discord.js';
|
|
const client = new Discord.Client();
|
|
|
|
import User, { Connection } from '../class/User';
|
|
import NotificationMessage from '../class/NotificationMessage';
|
|
|
|
let run = async (message: NotificationMessage, connection: Connection) => {
|
|
if (!process.env.BOT_TOKEN) return; // Only run when client is enabled
|
|
if (!client.readyAt) await new Promise(r => client.once('ready', r)) as void; // Wait for client to log in
|
|
|
|
try {
|
|
const channel = await client.channels.fetch(connection.channelID).catch(()=>{}) as Discord.DMChannel;
|
|
if (!channel) return console.log(`Could not find channel: ${channel?.id}`);
|
|
|
|
let embed = new Discord.MessageEmbed()
|
|
.setAuthor(message.title)
|
|
.setDescription(message.body)
|
|
.setColor(message.color);
|
|
|
|
if (message.footer) embed.setFooter(message.footer);
|
|
|
|
// Couldn't figure out how to attach the images in the assets folder, so I use hardcoded URLs instead
|
|
if (message.isImportant) {
|
|
embed.setAuthor(embed.author.name, 'https://cdn.discordapp.com/attachments/637695358373199879/824676476405940284/info.png');
|
|
}
|
|
|
|
if (message.isError) {
|
|
embed.color = 0xff0000;
|
|
embed.setAuthor(embed.author.name, 'https://cdn.discordapp.com/attachments/637695358373199879/824676465051828334/error.png' );
|
|
}
|
|
|
|
if (!embed.timestamp) embed.setTimestamp();
|
|
channel.send(embed)
|
|
.catch(console.error);
|
|
} catch(e) {
|
|
console.warn(e);
|
|
|
|
// If error didn't occur while sending an error message, attempt to send an error to the user.
|
|
if (!message.isError) {
|
|
let msg = new NotificationMessage()
|
|
.setTitle('Error during delivery')
|
|
.setBody(`\`\`\`js\n${e}\`\`\``);
|
|
|
|
run(msg.error(), connection)
|
|
.catch(console.warn);
|
|
}
|
|
}
|
|
}
|
|
|
|
let init = async () => {
|
|
if (!process.env.BOT_TOKEN) return console.log('Discord: No token specified');
|
|
|
|
client.login(process.env.BOT_TOKEN);
|
|
client.on('ready', () => console.log(`Discord Bot: Logged in as ${client.user.tag}`));
|
|
}
|
|
|
|
export { run, init }; |