This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
184 lines (175 loc) · 7.52 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const discord = require( 'discord.js' );
const client = new discord.Client();
const fs = require( 'fs' );
const WaifuApi = require( "./api/WaifuApi" );
const config = JSON.parse( fs.readFileSync( "config.json", "utf-8" ) );
const waifuApi = new WaifuApi( config.mywaifulist_key );
const pfp = 'https://github.com/rjworks/WaifuPls/blob/master/assets/pfp.jpg?raw=true';
const github = 'https://github.com/rjworks/WaifuPls';
const modPerms = ["ADMINISTRATOR","MANAGE_MESSAGES"];
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity({
type: "WATCHING",
name: config.prefix + "help"
});
});
client.on( 'message', msg => {
if(msg.content === `<@!${client.user.id}>`){
msg.reply(help())
return;
}
if ( msg.content.startsWith( config.prefix )) {
const args = msg.content.slice(config.prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
switch ( command ){
case "random":
waifuApi.getRandomWaifu().then((data) => {
// normally this happens when there's too much requests
if(data == null){
msg.reply("oop! Pls slow down :)")
return;
}
waifuApi.getWaifu(data.slug).then((fullData) =>
msg.reply(embed("[Random waifu]", fullData)));
})
break;
case "daily":
waifuApi.getDailyWaifu().then((data) => {
// normally this happens when there's too much requests
if(data == null){
msg.reply("oop! Pls slow down :)")
return;
}
waifuApi.getWaifu(data.slug).then((fullData) =>
msg.reply(embed("[Waifu of the day]", fullData)));
})
break;
case "best":
waifuApi.getBestWaifusThisSeason().then((data) => {
// normally this happens when there's too much requests
if(data == null){
msg.reply("oop! Pls slow down :)")
return;
}
for(let i = 0; i < 10; i++){
waifuApi.getWaifu(data[i].slug).then((fullData) => {
msg.reply(embed("[Best waifu of the season]", fullData));
})
}
})
break;
case "trash":
waifuApi.getTrashWaifusThisSeason().then((data) => {
// normally this happens when there's too much requests
if(data == null){
msg.reply("oop! Pls slow down :)")
return;
}
for(let i = 0; i < 10; i++){
waifuApi.getWaifu(data[i].slug).then((fullData) => {
msg.reply(embed("[Most trash waifu of the season]", fullData));
})
}
})
break;
case "popular":
waifuApi.getPopularWaifusThisSeason().then((data) => {
// normally this happens when there's too much requests
if(data == null){
msg.reply("oop! Pls slow down :)")
return;
}
for(let i = 0; i < 10; i++){
waifuApi.getWaifu(data[i].slug).then((fullData) => {
msg.reply(embed("[Most popular waifu of the season]", fullData));
})
}
})
break;
// todo
// case "search":
// const searchTerm = args[0];
// if(searchTerm === undefined){
// msg.reply("usage: " + config.prefix + "search <waifu>")
// return;
// }
// waifuApi.searchForWaifu(searchTerm).then((waifu) => {
// console.log(waifu)
// })
// break;
case "setprefix":
for ( let perm of modPerms ) {
if ( !msg.guild.member( msg.author ).hasPermission( perm ) ) {
msg.reply( config.no_perm );
return false;
}
}
if ( args[ 0 ] === undefined ) {
msg.reply( "usage: " + config.prefix + "setprefix <prefix>" )
return;
}
config.prefix = args[ 0 ];
fs.writeFileSync( "config.json", JSON.stringify( config ) );
msg.reply( "my prefix has been update to ``" + config.prefix + "``" )
break;
case "help":
default:
msg.reply( help() )
break;
}
}
} );
const embed = (title, data, color = "#f59542") => {
let fields =
[{
name: `Anime`,
value: `[${data.data.series.name}](https://mywaifulist.moe/series/+${data.data.series.slug})` + " - " + data.data.series.description.substring(0, 300) + '...',
},
{name: 'Likes', value: data.data.likes, inline: true},
{name: 'Trash', value: data.data.trash, inline: true},
];
const extraData = {
"age": " y/o", "weight": "kg", "height": "cm", "popularity_rank": "",
"birthday_year": "", "birthday_month": "", "birthday_day": ""
};
Object.keys(extraData).forEach(i => {
if(data.data[i] !== undefined && data.data[i] !== null && data.data[i] !== 0 && data.data[i] !== ''){
fields.push({
name: i[0].toUpperCase() + i.slice(1).replace("_", " "),
value: i === "popularity_rank" ? '#' + data.data[i] : data.data[i] + extraData[i],
inline: true
})
}
})
return new discord.MessageEmbed()
.setColor(color)
.setTitle(title)
.setDescription(data.data.description.substring(0, 300) + '...')
.setURL(data.data.url)
.setAuthor('WaifuPls', pfp, github)
.addFields(fields)
.setThumbnail(pfp)
.setImage(data.data.display_picture)
.setTimestamp()
.setFooter( 'WaifuPls', pfp )
}
const help = () => {
return new discord.MessageEmbed()
.setColor('#f59542')
.setTitle("Available commands")
.setAuthor('WaifuPls', pfp, github)
.addField(`${config.prefix}help`, "Sends the list of available commands", true)
.addField(`${config.prefix}random`, "Sends a random waifu", true)
.addField(`${config.prefix}daily`, "Sends the waifu of the day", true)
.addField(`${config.prefix}best`, "Sends the top 10 best waifus of the season", true)
.addField(`${config.prefix}popular`, "Sends the top 10 most popular waifus of the season", true)
.addField(`${config.prefix}trash`, "Sends the top 10 most trash waifus of the season", true)
.addField(`${config.prefix}search <waifu>`, "Sends information about the given waifu (TODO)", true)
.addField(`${config.prefix}setprefix <prefix>`, "(MOD ONLY) Sets the bot's prefix", true)
.addField(`${config.prefix}animepls`, "Sends a random anime (TODO)", true)
.setThumbnail(pfp)
.setTimestamp()
.setFooter('WaifuPls', pfp)
}
client.login( config.bot_token );