feat(twilight): basic logging

This commit is contained in:
AlphaNecron 2021-10-09 20:04:01 +07:00
parent d9342a356f
commit d7fb2707be
15 changed files with 57 additions and 38 deletions

View File

@ -53,6 +53,7 @@ export default function URLs() {
showToast('error', 'Couldn\'t shorten the url', res.error);
else
copyUrl(res);
updateUrls();
setBusy(false);
actions.setSubmitting(false);
};

View File

@ -35,7 +35,8 @@ async function handler(req: NextApiReq, res: NextApiRes) {
password
}
});
info('URL', `User ${user.username} (${user.id}) shortened a URL: ${url.destination} (${url.id})`);
info('URL', `User ${user.username} (${user.id}) shortened a URL: ${url.destination} (${url.id})`);
global.logger.logUrl(url, user.username);
return res.json({
url: `http${config.core.secure ? 's' : ''}://${req.headers.host}${config.shortener.route}/${url.short}`
});

View File

@ -62,6 +62,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
});
await writeFile(join(process.cwd(), cfg.uploader.directory, file.fileName), req.file.buffer);
info('FILE', `User ${user.username} (${user.id}) uploaded a file: ${file.fileName} (${file.id})`);
global.logger.logFile(file, user.username);
const baseUrl = `http${cfg.core.secure ? 's' : ''}://${req.headers.host}`;
return res.json({
url: `${baseUrl}/${file.slug}`,

View File

@ -14,13 +14,15 @@ async function handler(req: NextApiReq, res: NextApiRes) {
id: req.body.id
}
});
if (!userToDelete) return res.status(404).end(JSON.stringify({ error: 'User not found' }));
if (!userToDelete) return res.error('User not found');
await prisma.user.delete({
where: {
id: userToDelete.id
}
});
delete userToDelete.password;
info('USER', `Deleted user ${userToDelete.username} (${userToDelete.id})`);
global.logger.logUser('delete', userToDelete);
return res.json(userToDelete);
} else if (req.method === 'POST') {
const { username, password, isAdmin } = req.body as { username: string, password: string, isAdmin: boolean };
@ -43,6 +45,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
});
delete newUser.password;
info('USER', `Created user ${newUser.username} (${newUser.id})`);
global.logger.logUser('create', newUser);
return res.json(newUser);
} else {
const all = await prisma.user.findMany({

View File

@ -7,7 +7,6 @@ const files = {
command: 'files',
description: 'View files',
syntax: '{PREFIX}files',
scopes: ['dm', 'text'],
execute: async (msg: Message) => {
const all = (await prisma.file.findMany({
select: {

View File

@ -7,7 +7,6 @@ const help = {
command: 'help',
description: 'Show this message',
syntax: '{PREFIX}help',
scopes: ['dm', 'text'],
execute: async (msg: Message) => {
const embed = defaultEmbed().setTitle('Help');
commands.forEach(command =>

View File

@ -9,7 +9,6 @@ const shorten = {
command: 'shorten',
description: 'Shorten a URL',
syntax: '{PREFIX}shorten <url> [vanity]',
scopes: ['dm', 'text'],
execute: async (msg: Message, args: string[]) => {
const [dest, vanity] = args;
if (!dest) return msg.channel.send('Please specify a URL to shorten');
@ -27,7 +26,7 @@ const shorten = {
const user = await prisma.user.findFirst({
where: {
id: 1
},
}
});
const rand = generate(config.shortener.length);
const url = await prisma.url.create({
@ -35,10 +34,10 @@ const shorten = {
short: vanity ? vanity : rand,
destination: schemify(dest),
userId: config.bot.default_uid,
},
}
});
info('URL', `User ${msg.author.tag} shortened a URL: ${url.destination} (${url.id})`);
global.logger.log(`User ${msg.author.tag} shortened a URL: ${url.destination} (${url.id})`);
global.logger.logUrl(url, msg.author.tag);
msg.channel.send(`http${config.core.secure ? 's' : ''}://${config.bot.hostname}${config.shortener.route}/${url.short}`);
}
};

View File

@ -9,7 +9,6 @@ const stats = {
command: 'stats',
description: 'View server stats',
syntax: '{PREFIX}stats',
scopes: ['dm', 'text'],
execute: async (msg: Message) => {
const size = await sizeOfDir(join(process.cwd(), config.uploader.directory));
const userCount = await prisma.user.count();

View File

@ -8,17 +8,17 @@ import generate, { emoji, zws } from '../../src/lib/generators';
import { info, error } from '../../src/lib/logger';
import mimetypes from '../../src/lib/mimetypes';
import prisma from '../../src/lib/prisma';
import schemify from 'url-schemify';
const upload = {
command: 'upload',
description: 'Upload a new file',
syntax: '{PREFIX}upload <url> [generator]',
scopes: ['dm', 'text'],
execute: async (msg: Message, args: string[]) => {
if (!args[0]) return msg.channel.send('No URL.');
let buffer, res;
try {
res = await fetch(args[0]);
res = await fetch(schemify(args[0]));
if (!res.ok) return msg.channel.send('Unable to fetch the file.');
buffer = await res.buffer();
}
@ -66,7 +66,7 @@ const upload = {
});
await writeFile(join(process.cwd(), config.uploader.directory, file.fileName), buffer);
info('FILE', `User ${msg.author.tag} uploaded a file: ${file.fileName} (${file.id})`);
global.logger.log(`User ${msg.author.tag} uploaded a file: ${file.fileName}`);
global.logger.logFile(file, msg.author.tag);
msg.channel.send(`http${config.core.secure ? 's' : ''}://${config.bot.hostname}/${file.slug}`);
}
};

View File

@ -7,7 +7,6 @@ const urls = {
command: 'urls',
description: 'View urls',
syntax: '{PREFIX}urls',
scopes: ['dm', 'text'],
execute: async (msg: Message) => {
const all = (await prisma.url.findMany({
select: {

View File

@ -8,7 +8,6 @@ const user = {
command: 'user',
description: 'Create a new user or delete an existing user',
syntax: '{PREFIX}user create <username> <password>\n{PREFIX}user delete <id>',
scopes: ['dm'],
execute: async (msg: Message, args: string[]) => {
const action = args[0];
if (!['create','delete'].includes(action)) {
@ -33,14 +32,12 @@ const user = {
password: hashed,
}
});
const embed = defaultEmbed()
.addFields(
{ name: 'id', value: newUser.id },
{ name: 'Username', value: newUser.username }
);
global.logger.log(embed);
msg.channel.send(embed.addField('Token', newUser.token));
info('USER',`${msg.author.username}#${msg} created a user: ${newUser.username}`);
msg.channel.send(defaultEmbed()
.setTitle('User created')
.addField('ID', newUser.id)
.addField('Username', newUser.username));
info('USER',`${msg.author.tag} created a user: ${newUser.username}`);
return global.logger.logUser(action, newUser);
}
case 'delete': {
const id = parseInt(args[1]);
@ -54,13 +51,12 @@ const user = {
});
}
catch (err) { return msg.channel.send(`Failed to delete user with id: ${id}\nError: ${err.meta?.cause}`); }
global.logger.log(`User deleted: ${userToDelete.username} (${userToDelete.id})`);
msg.channel.send(defaultEmbed()
.setTitle('User deleted')
.setFooter(`By: ${msg.author.tag}`)
.setFooter(`By: ${msg.author.tag}`)
.addField('Username', userToDelete.username, true));
.addField('ID', userToDelete.id)
.addField('Username', userToDelete.username));
info('USER',`${msg.author.tag} deleted a user: ${userToDelete.username} (${userToDelete.id})`);
return global.logger.logUser(action, userToDelete);
}
}
}

View File

@ -7,7 +7,6 @@ const users = {
command: 'users',
description: 'View users',
syntax: '{PREFIX}users',
scopes: ['dm', 'text'],
execute: async (msg: Message) => {
const all = (await prisma.user.findMany({
select: {

View File

@ -32,9 +32,7 @@ client.on('message', (msg: Message) => {
const cmd = args.shift().toString().toLowerCase();
commands.forEach(command => {
if (command.command === cmd)
if (command.scopes.includes(msg.channel.type))
command.execute(msg, args, client);
else msg.channel.send(`This command can only be executed in ${command.scopes.map(scope => `\`${scope}\``).join(' or ')}`);
command.execute(msg, args, client);
});
}
});

View File

@ -3,6 +3,7 @@ import config from '../../src/lib/config';
import { name, version } from '../../package.json';
import { avatarUrl } from '../twilight';
import { defaultEmbed } from '../utils/utils';
import prisma from '../../src/lib/prisma';
export class Logger {
channel: TextChannel;
@ -14,12 +15,37 @@ export class Logger {
this.channel = client.channels.cache.find(c => c.type === 'text' && c.id === config.bot.log_channel) as TextChannel;
}
logFile(file: any, user: any) {
logFile(file: { id, fileName, origFileName, mimetype, uploadedAt, slug }, username: string) {
if (!this.channel) return;
this.channel.send(defaultEmbed(`By ${username}`)
.setTitle('File uploaded')
.setThumbnail(`http${config.core.secure ? 's' : ''}://${config.bot.hostname}/${file.fileName}`)
.addField('ID', file.id)
.addField('File name', file.fileName)
.addField('Original file name', file.origFileName || 'None')
.addField('Mime type', file.mimetype)
.addField('Uploaded at', file.uploadedAt)
.addField('View', `http${config.core.secure ? 's' : ''}://${config.bot.hostname}${config.uploader.raw_route}/${file.fileName}`));
}
logUrl(url: any, user: any) {
logUser(action: 'create' | 'update' | 'delete', user: { id, username, isAdmin }) {
if (!this.channel) return;
this.channel.send(defaultEmbed()
.setTitle(`User ${action}d`)
.addField('ID', user.id)
.addField('Username', user.username)
.addField('Role', user.isAdmin ? 'Administrator' : 'User'));
}
logUrl(url: { id, short, destination, createdAt, password }, username: string) {
if (!this.channel) return;
this.channel.send(defaultEmbed(`By ${username}`)
.setTitle('URL shortened')
.addField('ID', url.id)
.addField('Destination', url.destination)
.addField('Created at', url.createdAt)
.addField('Has password', url.password ? 'yes' : 'no')
.addField('Go', `http${config.core.secure ? 's' : ''}://${config.bot.hostname}${config.shortener.route}/${url.short}`));
}
log(msg: string) {

View File

@ -7,9 +7,8 @@ export function pagify(title: string, items: any[], msg: Message): Function {
const pages: MessageEmbed[] = [];
for (let i = 0; i < items.length; i += 6) {
const sliced = items.slice(i, i + 6);
const embed = defaultEmbed()
const embed = defaultEmbed(`Page ${i / 6 + 1}/${Math.ceil(items.length / 6)} | Total: ${items.length}`)
.setTitle(title)
.setFooter(`${name}@${version} | Page ${i / 6 + 1}/${Math.ceil(items.length / 6)} | Total: ${items.length}`, `http${config.core.secure ? 's' : ''}://${config.bot.hostname}/logo.png`);
sliced.forEach(item =>
embed.addField(item.name, item.value)
);
@ -36,5 +35,5 @@ export function pagify(title: string, items: any[], msg: Message): Function {
};
}
export const defaultEmbed = () => new MessageEmbed().setColor('#B794F4')
.setAuthor('Twilight', avatarUrl).setTimestamp().setFooter(`${name}@${version}`, `http${config.core.secure ? 's' : ''}://${config.bot.hostname}/logo.png`);
export const defaultEmbed = (footer?: string) => new MessageEmbed().setColor('#B794F4')
.setAuthor('Twilight', avatarUrl).setTimestamp().setFooter(`${name}@${version}${footer ? ` | ${footer}` : ''}`, `http${config.core.secure ? 's' : ''}://${config.bot.hostname}/logo.png`);