Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit 5f2950d

Browse files
committed
feat(twilight): added reaction pagination to files command
1 parent ef3fda5 commit 5f2950d

File tree

8 files changed

+52
-29
lines changed

8 files changed

+52
-29
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "void",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"private": true,
55
"engines": {
66
"node": ">=14"

readme.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,23 @@
2727
```
2828

2929
### Docker
30-
```sh
30+
```sh
3131
git clone https://github.com/AlphaNecron/Void.git
3232
cd Void
3333
cp config.example.toml config.toml
3434
nano config.toml # edit the config file
3535
docker pull alphanecron/void:v0
3636
docker run -p 3000:3000 -v $PWD/config.toml:/void/config.toml -d alphanecron/void:v0
37-
```
37+
```
3838

3939
### Docker compose
40-
```sh
40+
```sh
4141
git clone https://github.com/AlphaNecron/Void.git
4242
cd Void
4343
cp config.example.toml config.toml
4444
nano config.toml # edit the config file
4545
docker-compose up --build -d
46-
```
47-
46+
```
4847

4948
### Reverse proxy (nginx)
5049
```nginx
@@ -116,6 +115,14 @@
116115
### Contribution
117116
- All pull requests must be made in `dev` branch, pull requests in `v0` will be closed.
118117

118+
### Bot permissions
119+
These permissions are required for the bot to work properly (27712):
120+
- Add reactions
121+
- Read messages
122+
- Send messages
123+
- Manage messages
124+
- Embed links
125+
119126
### Todo
120127
- Discord integration
121128
- Album / Bulk upload

twilight/commands/files.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,43 @@ const files = {
1313
fileName: true,
1414
origFileName: true,
1515
mimetype: true,
16-
uploadedAt: true
16+
uploadedAt: true,
17+
userId: true
1718
}
1819
});
20+
const pages: MessageEmbed[] = [];
1921
for (let i = 0; i < all.length; i += 4) {
2022
const files = all.slice(i, i + 4);
2123
const embed = new MessageEmbed()
2224
.setTitle('Files')
23-
.setColor('#B794F4');
24-
files.forEach(file => {
25-
embed.addField(`${file.fileName}`,
25+
.setColor('#B794F4')
26+
.setFooter(`Page ${i / 4 + 1}/${Math.ceil(all.length / 4)} | Total: ${all.length}`);
27+
files.forEach(file =>
28+
embed.addField(file.fileName,
2629
`ID: ${file.id}
2730
Original file name: ${file.origFileName}
2831
Mimetype: ${file.mimetype}
2932
Uploaded at: ${new Date(file.uploadedAt).toLocaleString()}`)
30-
.setFooter(`Page ${i / 4 + 1}/${Math.ceil(all.length / 4)}`);
31-
});
32-
msg.channel.send(embed);
33+
);
34+
pages.push(embed);
3335
}
36+
// https://stackoverflow.com/a/60693028
37+
msg.channel.send(pages[0]).then(message => {
38+
if (pages.length <= 1) return;
39+
message.react('➡️');
40+
const collector = message.createReactionCollector(
41+
(reaction, user) => ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === msg.author.id,
42+
{ time: 60000 }
43+
);
44+
let i = 0;
45+
collector.on('collect', async reaction => {
46+
await message.reactions.removeAll();
47+
reaction.emoji.name === '⬅️' ? i -= 1 : i += 1;
48+
await message.edit(pages[i]);
49+
if (i !== 0) await message.react('⬅️');
50+
if (i + 1 < pages.length) await message.react('➡️');
51+
});
52+
});
3453
}
3554
};
3655

twilight/commands/shorten.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ const shorten = {
1111
syntax: '{PREFIX}shorten <url> [vanity]',
1212
scopes: ['dm', 'text'],
1313
execute: async (msg: Message, args: string[]) => {
14-
const dest = args[0];
15-
const vanity = args[1];
14+
const [dest, vanity] = args;
1615
if (!dest) return msg.channel.send('Please specify a URL to shorten');
1716
if (!dest.includes('.')) return msg.channel.send('Please specify a valid URL.');
1817
if (vanity) {
@@ -38,8 +37,8 @@ const shorten = {
3837
userId: config.bot.default_uid,
3938
},
4039
});
41-
info('URL', `User ${msg.author.username}#${msg.author.discriminator} shortened a URL: ${url.destination} (${url.id})`);
42-
global.logger.log(`User ${msg.author.username}#${msg.author.discriminator} shortened a URL: ${url.destination} (${url.id})`);
40+
info('URL', `User ${msg.author.tag} shortened a URL: ${url.destination} (${url.id})`);
41+
global.logger.log(`User ${msg.author.tag} shortened a URL: ${url.destination} (${url.id})`);
4342
msg.channel.send(`http${config.core.secure ? 's' : ''}://${config.bot.hostname}${config.shortener.route}/${url.short}`);
4443
}
4544
};

twilight/commands/upload.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ const upload = {
6565
}
6666
});
6767
await writeFile(join(process.cwd(), config.uploader.directory, file.fileName), buffer);
68-
info('FILE', `User ${msg.author.username}#${msg.author.discriminator} uploaded a file: ${file.fileName} (${file.id})`);
69-
global.logger.log(`User ${msg.author.username}#${msg.author.discriminator} uploaded a file: ${file.fileName}`);
68+
info('FILE', `User ${msg.author.tag} uploaded a file: ${file.fileName} (${file.id})`);
69+
global.logger.log(`User ${msg.author.tag} uploaded a file: ${file.fileName}`);
7070
msg.channel.send(`http${config.core.secure ? 's' : ''}://${config.bot.hostname}/${file.slug}`);
7171
}
7272
};

twilight/commands/user.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ const user = {
1515
}
1616
switch (action) {
1717
case 'create': {
18-
const username = args[1];
19-
const password = args[2];
18+
const [_, username, password] = args;
2019
const existing = await prisma.user.findFirst({
2120
where: {
2221
username
@@ -64,7 +63,7 @@ const user = {
6463
.addField('Username', userToDelete.username, true);
6564
global.logger.log(embed);
6665
msg.channel.send(embed);
67-
info('USER',`${msg.author.username}#${msg} created a user: ${userToDelete.username}`);
66+
info('USER',`${msg.author.tag} created a user: ${userToDelete.username}`);
6867
}
6968
}
7069
}

twilight/twilight.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ export const commands = [];
1212
client.once('ready', () => {
1313
info('BOT', 'Twilight is ready');
1414
global.logger = new Logger(client);
15-
global.logger.log(new MessageEmbed()
16-
.setTitle('Twilight is ready')
17-
.setColor('#B794F4'));
15+
global.logger.log('Twilight is ready');
1816
readdir(`${__dirname}/commands`, (err, files) => {
1917
if(err) error('BOT', err.message);
2018
files.forEach(file => {
2119
if (file.toString().includes('.ts')) {
2220
import(`${__dirname}/commands/${file.toString()}`).then(command => commands.push(command.default));
23-
info('COMMAND', `Loaded command: ${file.toString().split('.').slice(0, -1)}`);}
21+
info('COMMAND', `Loaded command: ${file.toString().split('.').shift()}`);}
2422
});
2523
});
2624
});
@@ -33,7 +31,7 @@ client.on('message', (msg: Message) => {
3331
if (command.command === cmd)
3432
if (command.scopes.includes(msg.channel.type))
3533
command.execute(msg, args, client);
36-
else msg.channel.send(`This command can only be executed in ${command.scopes.join(' or ')}`);
34+
else msg.channel.send(`This command can only be executed in ${command.scopes.map(scope => `\`${scope}\``).join(' or ')}`);
3735
});
3836
}
3937
});

twilight/utils/logger.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Client, MessageEmbed, TextChannel } from 'discord.js';
22
import config from '../../src/lib/config';
3+
import { name, version } from '../../package.json';
34

45
export class Logger {
56
channel: TextChannel;
6-
7+
78
constructor(client: Client) {
89
if (!config.bot.log_channel) return;
910
this.channel = client.channels.cache.find(c => c.type === 'text' && c.id === config.bot.log_channel) as TextChannel;
1011
}
1112

1213
log(msg: MessageEmbed | string) {
1314
if (!this.channel) return;
14-
this.channel.send(typeof msg === 'string' ? new MessageEmbed().setTitle(msg).setColor('#B794F4').setTimestamp() : msg);
15+
this.channel.send(typeof msg === 'string' ? new MessageEmbed().setTitle(msg).setColor('#B794F4').setTimestamp().setFooter(`${name}@${version}`) : msg);
1516
}
1617
}

0 commit comments

Comments
 (0)