feat(twilight): added reaction pagination to `files` command

This commit is contained in:
AlphaNecron 2021-10-08 19:11:50 +07:00
parent ef3fda59bb
commit 5f2950d289
8 changed files with 52 additions and 29 deletions

View File

@ -1,6 +1,6 @@
{
"name": "void",
"version": "0.4.0",
"version": "0.4.1",
"private": true,
"engines": {
"node": ">=14"

View File

@ -27,24 +27,23 @@
```
### Docker
```sh
```sh
git clone https://github.com/AlphaNecron/Void.git
cd Void
cp config.example.toml config.toml
nano config.toml # edit the config file
docker pull alphanecron/void:v0
docker run -p 3000:3000 -v $PWD/config.toml:/void/config.toml -d alphanecron/void:v0
```
```
### Docker compose
```sh
```sh
git clone https://github.com/AlphaNecron/Void.git
cd Void
cp config.example.toml config.toml
nano config.toml # edit the config file
docker-compose up --build -d
```
```
### Reverse proxy (nginx)
```nginx
@ -116,6 +115,14 @@
### Contribution
- All pull requests must be made in `dev` branch, pull requests in `v0` will be closed.
### Bot permissions
These permissions are required for the bot to work properly (27712):
- Add reactions
- Read messages
- Send messages
- Manage messages
- Embed links
### Todo
- Discord integration
- Album / Bulk upload

View File

@ -13,24 +13,43 @@ const files = {
fileName: true,
origFileName: true,
mimetype: true,
uploadedAt: true
uploadedAt: true,
userId: true
}
});
const pages: MessageEmbed[] = [];
for (let i = 0; i < all.length; i += 4) {
const files = all.slice(i, i + 4);
const embed = new MessageEmbed()
.setTitle('Files')
.setColor('#B794F4');
files.forEach(file => {
embed.addField(`${file.fileName}`,
.setColor('#B794F4')
.setFooter(`Page ${i / 4 + 1}/${Math.ceil(all.length / 4)} | Total: ${all.length}`);
files.forEach(file =>
embed.addField(file.fileName,
`ID: ${file.id}
Original file name: ${file.origFileName}
Mimetype: ${file.mimetype}
Uploaded at: ${new Date(file.uploadedAt).toLocaleString()}`)
.setFooter(`Page ${i / 4 + 1}/${Math.ceil(all.length / 4)}`);
});
msg.channel.send(embed);
);
pages.push(embed);
}
// https://stackoverflow.com/a/60693028
msg.channel.send(pages[0]).then(message => {
if (pages.length <= 1) return;
message.react('➡️');
const collector = message.createReactionCollector(
(reaction, user) => ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === msg.author.id,
{ time: 60000 }
);
let i = 0;
collector.on('collect', async reaction => {
await message.reactions.removeAll();
reaction.emoji.name === '⬅️' ? i -= 1 : i += 1;
await message.edit(pages[i]);
if (i !== 0) await message.react('⬅️');
if (i + 1 < pages.length) await message.react('➡️');
});
});
}
};

View File

@ -11,8 +11,7 @@ const shorten = {
syntax: '{PREFIX}shorten <url> [vanity]',
scopes: ['dm', 'text'],
execute: async (msg: Message, args: string[]) => {
const dest = args[0];
const vanity = args[1];
const [dest, vanity] = args;
if (!dest) return msg.channel.send('Please specify a URL to shorten');
if (!dest.includes('.')) return msg.channel.send('Please specify a valid URL.');
if (vanity) {
@ -38,8 +37,8 @@ const shorten = {
userId: config.bot.default_uid,
},
});
info('URL', `User ${msg.author.username}#${msg.author.discriminator} shortened a URL: ${url.destination} (${url.id})`);
global.logger.log(`User ${msg.author.username}#${msg.author.discriminator} shortened a URL: ${url.destination} (${url.id})`);
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})`);
msg.channel.send(`http${config.core.secure ? 's' : ''}://${config.bot.hostname}${config.shortener.route}/${url.short}`);
}
};

View File

@ -65,8 +65,8 @@ const upload = {
}
});
await writeFile(join(process.cwd(), config.uploader.directory, file.fileName), buffer);
info('FILE', `User ${msg.author.username}#${msg.author.discriminator} uploaded a file: ${file.fileName} (${file.id})`);
global.logger.log(`User ${msg.author.username}#${msg.author.discriminator} uploaded a file: ${file.fileName}`);
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}`);
msg.channel.send(`http${config.core.secure ? 's' : ''}://${config.bot.hostname}/${file.slug}`);
}
};

View File

@ -15,8 +15,7 @@ const user = {
}
switch (action) {
case 'create': {
const username = args[1];
const password = args[2];
const [_, username, password] = args;
const existing = await prisma.user.findFirst({
where: {
username
@ -64,7 +63,7 @@ const user = {
.addField('Username', userToDelete.username, true);
global.logger.log(embed);
msg.channel.send(embed);
info('USER',`${msg.author.username}#${msg} created a user: ${userToDelete.username}`);
info('USER',`${msg.author.tag} created a user: ${userToDelete.username}`);
}
}
}

View File

@ -12,15 +12,13 @@ export const commands = [];
client.once('ready', () => {
info('BOT', 'Twilight is ready');
global.logger = new Logger(client);
global.logger.log(new MessageEmbed()
.setTitle('Twilight is ready')
.setColor('#B794F4'));
global.logger.log('Twilight is ready');
readdir(`${__dirname}/commands`, (err, files) => {
if(err) error('BOT', err.message);
files.forEach(file => {
if (file.toString().includes('.ts')) {
import(`${__dirname}/commands/${file.toString()}`).then(command => commands.push(command.default));
info('COMMAND', `Loaded command: ${file.toString().split('.').slice(0, -1)}`);}
info('COMMAND', `Loaded command: ${file.toString().split('.').shift()}`);}
});
});
});
@ -33,7 +31,7 @@ client.on('message', (msg: Message) => {
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.join(' or ')}`);
else msg.channel.send(`This command can only be executed in ${command.scopes.map(scope => `\`${scope}\``).join(' or ')}`);
});
}
});

View File

@ -1,9 +1,10 @@
import { Client, MessageEmbed, TextChannel } from 'discord.js';
import config from '../../src/lib/config';
import { name, version } from '../../package.json';
export class Logger {
channel: TextChannel;
constructor(client: Client) {
if (!config.bot.log_channel) return;
this.channel = client.channels.cache.find(c => c.type === 'text' && c.id === config.bot.log_channel) as TextChannel;
@ -11,6 +12,6 @@ export class Logger {
log(msg: MessageEmbed | string) {
if (!this.channel) return;
this.channel.send(typeof msg === 'string' ? new MessageEmbed().setTitle(msg).setColor('#B794F4').setTimestamp() : msg);
this.channel.send(typeof msg === 'string' ? new MessageEmbed().setTitle(msg).setColor('#B794F4').setTimestamp().setFooter(`${name}@${version}`) : msg);
}
}