forked from ciderapp/Cider-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
213 lines (192 loc) · 9.14 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const { Client, Collection, Intents } = require('discord.js');
const Discord = require('discord.js');
let auth = require('./local').token()
let express = require('./integrations/express')
const cheerio = require('cheerio');
const fetch = require('node-fetch');
const deploy = require('./deploy-commands.js');
const { MessageEmbed } = require('discord.js');
const mongo = require('./integrations/mongo');
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, Discord.Intents.FLAGS.GUILD_PRESENCES, Discord.Intents.FLAGS.GUILD_MEMBERS]
});
client.commands = new Collection();
client.interactions = new Collection();
replies = [];
const fs = require('node:fs');
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
console.log("Registered Command: ", command.data.name);
}
const interactionFiles = fs.readdirSync('./interactions').filter(file => file.endsWith('.js'));
for (const file of interactionFiles) {
const interaction = require(`./interactions/${file}`);
// Set a new item in the Collection
// With the key as the interaction name and the value as the exported module
client.interactions.set(interaction.data.name, interaction);
console.log("Registered Interaction: ", interaction.data.name);
}
const replyFiles = fs.readdirSync('./replies').filter(file => file.endsWith('.json'));
for (const file of replyFiles) {
let reply = require(`./replies/${file}`);
// Set a new item in the Collection
// With the key as the reply name and the value as the exported module
replies.push(reply);
console.log("Registered Reply: ", reply.name);
}
let cider_guild = "843954443845238864"
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag} at`);
console.log(Date())
mongo.init()
});
client.on('presenceUpdate', async(oldMember, newMember) => {
//If role not found in guild, do nothing.
try { if (oldMember.guild.id !== cider_guild || newMember.guild.id !== cider_guild) return } catch (e) { return }
// or else it'll go BONK
const role = newMember.guild.roles.cache.get("932784788115427348");
let using_cider = false
for (const activity of newMember.activities) {
// 911790844204437504 - Cider
// 886578863147192350 - Apple Music
if (activity && (activity.applicationId === ("911790844204437504") || (activity.applicationId === ("886578863147192350")))) {
let listenerinfo = {
userid: newMember.userId,
userName: newMember.member.user.username,
songName: activity.details,
artistName: String(activity.state).split("by ")[1],
}
mongo.logRPMetadata(listenerinfo)
if (newMember.member._roles.includes("932784788115427348")) { // user already has listening role, no need to change roles
console.log("\x1b[2m", "Listener updated -", listenerinfo)
return // not changing any roles, just a log
} else {
console.log('\x1b[35m%s\x1b[0m', "Listener added -", listenerinfo)
using_cider = true // code below will handle it
break
}
}
}
if (using_cider) {
try {
newMember.member.roles.add(role) // add listening on cider role
if (!newMember.member._roles.includes("932816700305469510")) {
try {
newMember.member.roles.add("932816700305469510")
} catch (e) {
console.log("An error occurred while adding role. ", e)
} // Add Cider User role.
}
} catch (e) {
console.log("An error occurred. ", e)
}
} else { // Remove role if exists or ignore.
try {
if (newMember.member._roles.includes("932784788115427348")) {
try {
newMember.member.roles.remove("932784788115427348"); // remove listening on cider role
} catch (e) {
console.log("An error occurred on role removal. ", e)
}
let rmlistenerinfo = {
userid: newMember.userId,
userName: newMember.member.user.username,
dateRemoved: Date()
}
console.log("\x1b[33m%s\x1b[0m", "Listener removed -", rmlistenerinfo)
}
} catch (e) {
console.log(e)
}
}
})
client.on('messageCreate', async message => {
const overrideRegex = new RegExp(/^\!/g);
if (message.author.bot) return
if((!message.member._roles.includes("848363050205446165") && !message.member._roles.includes("932811694751768656") && !message.member.id.includes("345021804210814976")) || overrideRegex.test(message.toString())) { // exclude dev team and donators
for(reply of replies) {
var regex = new RegExp(`${reply.name}`, "gi");
if(regex.test(message.toString())) {
console.log("\x1b[32m%s\x1b[0m", "Reply triggered:", reply.name)
mongo.replyCounter(reply.name)
message.react("✅")
const embed = new Discord.MessageEmbed()
.setColor(reply.color)
.setTitle(`${reply.title}`)
.setDescription(`${reply.description}`)
.setFooter({ text: "Requested by " + message.member.user.username, iconURL: message.member.user.avatarURL() })
.setTimestamp()
message.channel.send({embeds: [embed]}).then(msg => {
setTimeout(() => msg.delete(), reply.timeout)
})
}
}
} else if (message.content.match(/^(?!cider:\/\/).+(music\.apple\.com)([^\s]+)/gi)) {
const link = message.content.match(/^(?!cider:\/\/).+(music\.apple\.com)([^\s]+)/gi)
console.log("[Link] Creating redirect embed.")
try {
fetch(link).catch(e => console.log("[Link] Error creating redirect embed."))
.then(result => result.text()).catch(e => null)
.then(html => {
const $ = cheerio.load(html)
const title = $('meta[property="og:title"]').attr('content') || $('title').text() || $('meta[name="title"]').attr('content')
const description = $('meta[property="twitter:description"]').attr('content') || $('meta[name="twitter:description"]').attr('content')
const image = $('meta[property="og:image"]').attr('content') || $('meta[property="og:image:url"]').attr('content')
const modlink = link[0].replace('https://', '')
const play_link = "https://cider.sh/p?" + modlink
const view_link = "https://cider.sh/o?" + modlink
const embed = new Discord.MessageEmbed()
.setColor('#fb003f')
.setTitle(title)
.setURL(link.toString())
.setThumbnail(image)
.setDescription(description)
.setFooter({ text: "Shared by " + message.author.username, iconURL: message.author.avatarURL() })
.setTimestamp()
const interaction = new Discord.MessageActionRow()
.addComponents(
new Discord.MessageButton()
.setLabel('Play In Cider')
.setStyle('LINK')
.setURL(play_link),
new Discord.MessageButton()
.setLabel('View In Cider')
.setStyle('LINK')
.setURL(view_link)
)
try {
message.delete()
return message.channel.send({ embeds: [embed], components: [interaction] });
} catch (e) {}
}).catch(e => null)
} catch (e) {}
}
})
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
mongo.commandCounter(interaction.commandName)
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isSelectMenu()) return;
const iAction = client.interactions.get(interaction.customId);
if (!iAction) return;
try {
await iAction.execute(interaction);
} catch (error) {
console.error(error);
await iAction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(auth)