· 6 years ago · Oct 30, 2019, 09:24 PM
1const Discord = require("discord.js");
2const client = new Discord.Client();
3const Eris = require('eris'); //
4const Database = require('better-sqlite3'); //
5const db = new Database('star.db'); //
6const config = require("./config.json");
7const sql = require("sqlite");
8
9sql.open("./scores.sqlite");
10
11client.music = require("discord.js-musicbot-addon");
12db.prepare('CREATE TABLE IF NOT EXISTS starids (msgid TEXT PRIMARY KEY, starid TEXT NOT NULL)').run(); //
13
14client.on('ready', () => {
15 console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
16 client.user.setActivity(`with ${client.users.size} balls.`, { type: 'PLAYING' })
17 client.music.start(client, {
18 // Set the api key used for YouTube!
19 youtubeKey: "xxx",
20
21 // The PLAY command Object.
22 play: {
23 // Usage text for the help command.
24 usage: "{{prefix}}play some tunes",
25 // Whether or not to exclude the command from the help command.
26 exclude: false
27 },
28
29 // Make it so anyone in the voice channel can skip the
30 // currently playing song.
31 anyoneCanSkip: true,
32
33 // Make it so the owner (you) bypass permissions for music.
34 ownerOverMember: true,
35 ownerID: "95702308515487744",
36
37 botPrefix: "=",
38
39 // The cooldown Object.
40 cooldown: {
41 // This disables the cooldown. Not recommended.
42 enabled: false
43 }
44});
45})
46
47client.on('messageReactionAdd', async (message, emoji, user) => {
48
49 if (emoji.name !== '⭐') return;
50
51 const channel = client.getChannel(message.channel.id);
52 const starboard = channel.guild.channels.find(c => c.name.toLowerCase() === 'shaqboard');
53
54 if (channel.nsfw || !starboard || channel.id === starboard.id) return;
55
56 const msg = await channel.getMessage(message.id);
57 const stars = (await msg.getReaction('⭐', msg.reactions['⭐'].count)).filter(u => u.id !== msg.author.id && !client.users.get(u.id).bot).length;
58
59 if (stars < 2) return;
60
61 if (msg.content.length === 0 && msg.attachments.length === 0 && (!msg.embeds[0] || msg.embeds[0].type !== 'image')) return;
62
63 const starId = await getMessageFromDatabase(msg.id);
64
65 if (!starId) {
66 if (!stars) return;
67
68 const starMsg = await starboard.createMessage({
69 content: `**__Starboard:__ ${stars} ⭐ - <#${msg.channel.id}> ${msg.author.username}#${msg.author.discriminator} has made it!**`,
70 embed: {
71 color: 16775619,
72 footer: {
73 icon_url: "https://cdn2.iconfinder.com/data/icons/circle-icons-1/64/star-512.png",
74 text: `You're a star! | ${msg.id}`
75 },
76 author: {
77 name: `${msg.author.username}#${msg.author.discriminator} - Starboard`,
78 icon_url: msg.author.avatarURL,
79 },
80 image: resolveAttachment(msg),
81 timestamp: new Date(),
82 fields: [
83 {
84 name: "Message Content",
85 value: `${msg.content || '**__Image: No Text.__**'}`
86 },
87 {
88 name: "Jump To Message",
89 value: `[Click Here](https://discordapp.com/channels/493152507414052867/${msg.channel.id}/${msg.id})`
90 },
91 ],
92 },
93 });
94
95
96
97 db.prepare('INSERT INTO starids VALUES (?, ?)').run(msg.id, starMsg.id);
98 } else {
99 const starMessage = await starboard.getMessage(starId);
100 if (!starMessage) return;
101 await starMessage.edit(`**__Starboard:__** ${stars} ⭐ - <#${msg.channel.id}> ${msg.author.username}#${msg.author.discriminator} has made it!`);
102 }
103});
104
105client.on('messageReactionRemove', async (message, emoji, user) => {
106 if (emoji.name !== '⭐') return;
107
108 const channel = client.getChannel(message.channel.id);
109 const starboard = channel.guild.channels.find(c => c.name.toLowerCase() === 'shaqboard');
110
111 if (!starboard || channel.id === starboard.id) return;
112
113 const msg = await channel.getMessage(message.id);
114 const starId = await getMessageFromDatabase(msg.id);
115 if (!starId) return;
116
117 const starMessage = await starboard.getMessage(starId);
118 if (!starMessage) return;
119
120 if (!msg.reactions['⭐']) {
121 db.prepare('DELETE FROM starids WHERE msgid = ?').run(msg.id);
122 return await starMessage.delete();
123 }
124
125 const stars = (await msg.getReaction('⭐', msg.reactions['⭐'].count)).filter(u => u.id !== msg.author.id && !client.users.get(u.id).bot).length;
126
127 if (!stars) {
128 db.prepare('DELETE FROM starids WHERE msgid = ?').run(msg.id);
129 return await starMessage.delete();
130 }
131
132 await starMessage.edit(`$**__Starboard:__** ${stars} ⭐ - <#${msg.channel.id}> ${msg.author.username}#${msg.author.discriminator} has made it!`);
133});
134
135function getMessageFromDatabase(msgid) {
136 return (db.prepare('SELECT * FROM starids WHERE msgid = ?').get(msgid) || {}).starid;
137}
138
139function resolveAttachment(msg) {
140 if (msg.attachments.length > 0 && msg.attachments[0].width) {
141 return msg.attachments[0];
142 } else if (msg.embeds.length > 0 && msg.embeds[0].type === 'image') {
143 return msg.embeds[0].image || msg.embeds[0].thumbnail;
144 } else {
145 return null;
146 }
147}