-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
80 lines (73 loc) · 2.58 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require('dotenv').config()
const axios = require('axios')
const Discord = require('discord.js')
const client = new Discord.Client()
const DISCORD_BOT_TOKEN = process.env.DISCORD_BOT_TOKEN
const VLIPSY_API_KEY = process.env.VLIPSY_API_KEY
const VLIPSY_BASE_URL = 'https://vlipsy.com/vlip/'
const VLIPSY_LIMIT = 10
const DISCORD_MESSAGE_MAX_LENGTH = 2000
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async (msg) => {
try
{
console.log('Message: ' + msg);
if (msg.content.startsWith('!vliplist')) {
var search = msg.content.replace('!vliplist','').trim();
const vlips = await getVlips(search);
var index = 0;
var catalog = "\r";
vlips.forEach(function(vlip) {
catalog += `${++index}) ${vlip.id}: ${vlip.title} from ${vlip.from} - (${vlip.rating}) /${vlip.slug}\r`;
})
msg.reply(catalog.substr(0,DISCORD_MESSAGE_MAX_LENGTH))
}
else if (msg.content.startsWith('!vlipid')) {
var id = msg.content.replace('!vlipid','').trim();
const vlip = await getVlipById(id);
if (vlip) {
msg.channel.send(`${vlip.title} from ${vlip.from} - ${vlip.media.mp4.url}`);
}
}
else if (msg.content.startsWith('!vlip')) {
var search = msg.content.replace('!vlip','').trim();
const vlip = await getFirstVlip(search);
if (vlip) {
msg.channel.send(`${vlip.title} from ${vlip.from} - ${vlip.media.mp4.url}`);
}
}
}
catch (error)
{
msg.reply(`Sorry, I got nothing... `);
console.log(error);
}
});
const getFirstVlip = async (search) => {
const vlips = await getVlips(search);
if ((vlips) && (vlips.length > 0)) {
return vlips[0];
}
throw new Error('No vlip returned');
}
const getVlips = async (search) => {
let response = await axios.get(`https://apiv2.vlipsy.com/v1/vlips/search?key=${VLIPSY_API_KEY}&q=${search}&limit=${VLIPSY_LIMIT}`);
if (response.status == 200) {
if (response.data.data.length > 0) {
return response.data.data;
}
}
throw new Error('No vlips returned');
}
const getVlipById = async(id) => {
let response = await axios.get(`https://apiv2.vlipsy.com/v1/vlips/${id}?key=${VLIPSY_API_KEY}`);
if (response.status == 200) {
if (response.data.data) {
return response.data.data;
}
}
throw new Error('No vlips returned');
}
client.login(DISCORD_BOT_TOKEN);