Made Clients available globally via exports and sanitized channel names
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
const { configDotenv } = require('dotenv');
|
||||
const { MongoClient } = require('mongodb');
|
||||
const TwitchApi = require("node-twitch").default
|
||||
const tClient = new TwitchApi({
|
||||
client_id: process.env.TTV_ID,
|
||||
client_secret: process.env.TTV_SECRET
|
||||
});
|
||||
configDotenv
|
||||
const mClient = new MongoClient(process.env.MONGO_URL);
|
||||
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
||||
const { tClient, mClient } = require('../../index');
|
||||
|
||||
function escapeMarkdown(input){
|
||||
return input.replace(/([*_#\[!\]()`>+\-.|])/g, '\\$1');
|
||||
}
|
||||
async function SubscribeChannel(interaction) {
|
||||
const channelName = encodeURI(await interaction.options.getString('channel')).toLowerCase()
|
||||
const userID = interaction.user.id
|
||||
@@ -17,7 +14,7 @@ async function SubscribeChannel(interaction) {
|
||||
//console.log(await tClient.searchChannels({query: `${channelName}`})) // returns too many results and not exact match first
|
||||
} catch (error) {
|
||||
return interaction.reply({
|
||||
content: `**[ERROR]:** Could not validate channel name for *${channelName}*!\r\n${error}`,
|
||||
content: `**[ERROR]:** Could not validate channel name for *${escapeMarkdown(channelName)}*!\r\n${error}`,
|
||||
ephemeral: true
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
@@ -30,9 +27,9 @@ async function SubscribeChannel(interaction) {
|
||||
const find = await channelsColl.find({ channel: channelName }).toArray()
|
||||
|
||||
if (find[0]) {
|
||||
if (find[0].users.includes(userID)) {
|
||||
if (find[0].subscribers.includes(userID)) {
|
||||
return interaction.reply({
|
||||
content: `**[WARNING]:** You are already subscribed to *${channelName}*!`,
|
||||
content: `**[WARNING]:** You are already subscribed to *${escapeMarkdown(channelName)}*!`,
|
||||
ephemeral: true
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
@@ -48,7 +45,7 @@ async function SubscribeChannel(interaction) {
|
||||
channel: channelName,
|
||||
},
|
||||
$push: {
|
||||
users: userID
|
||||
subscribers: userID
|
||||
}
|
||||
}, {
|
||||
upsert: true,
|
||||
@@ -64,7 +61,7 @@ async function SubscribeChannel(interaction) {
|
||||
}
|
||||
//console.log(res) // debug
|
||||
return interaction.reply({
|
||||
content: `**[SUCCESS]:** You are now subscribed to *${channelName}*!`,
|
||||
content: `**[SUCCESS]:** You are now subscribed to *${escapeMarkdown(channelName)}*!`,
|
||||
ephemeral: true
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
@@ -81,9 +78,9 @@ async function UnsubscribeChannel(interaction) {
|
||||
const find = await channelsColl.find({ channel: channelName }).toArray()
|
||||
|
||||
if (find[0]) {
|
||||
if (!find[0].users.includes(userID)) {
|
||||
if (!find[0].subscribers.includes(userID)) {
|
||||
return interaction.reply({
|
||||
content: `**[WARNING]:** You were not subscribed to *${channelName}*!`,
|
||||
content: `**[WARNING]:** You were not subscribed to *${escapeMarkdown(channelName)}*!`,
|
||||
ephemeral: true
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
@@ -99,12 +96,17 @@ async function UnsubscribeChannel(interaction) {
|
||||
channel: channelName,
|
||||
},
|
||||
$pull: {
|
||||
users: userID
|
||||
subscribers: userID
|
||||
}
|
||||
}, {
|
||||
upsert: true,
|
||||
returnDocument: 'after'
|
||||
})
|
||||
if(!res.subscribers[0]){
|
||||
const newRes = await channelsColl.findOneAndDelete({
|
||||
channel: channelName
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
return interaction.reply({
|
||||
content: `**[ERROR]:** There was an issue with the database!\r\n${error}`,
|
||||
@@ -113,9 +115,11 @@ async function UnsubscribeChannel(interaction) {
|
||||
interaction.deleteReply()
|
||||
}, 5000))
|
||||
}
|
||||
|
||||
|
||||
//console.log(res) // debug
|
||||
return interaction.reply({
|
||||
content: `**[SUCCESS]:** You are no longer subscribed to *${channelName}*!`,
|
||||
content: `**[SUCCESS]:** You are no longer subscribed to *${escapeMarkdown(channelName)}*!`,
|
||||
ephemeral: true
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
@@ -128,19 +132,29 @@ async function ListChannel(interaction) {
|
||||
const db = mClient.db('notifyme')
|
||||
const channelsColl = db.collection('channels')
|
||||
|
||||
const find = await channelsColl.find({ users: userID }).toArray()
|
||||
const find = await channelsColl.find({ subscribers: userID }).toArray()
|
||||
if (!find[0]) {
|
||||
return interaction.reply({
|
||||
content: `You are not subscribed to any channels!`,
|
||||
ephemeral: true
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
}, 5000))
|
||||
}
|
||||
var channelList = []
|
||||
find.forEach((document) => {
|
||||
channelList.push(document.channel)
|
||||
channelList.push(escapeMarkdown(document.channel))
|
||||
})
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle('Lists of Subscribed Channels:')
|
||||
.setTitle('List of Subscribed Channels:')
|
||||
.setDescription(channelList.join("\n"))
|
||||
|
||||
return interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
})
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
}, 60000))
|
||||
}
|
||||
|
||||
async function InfoChannel(interaction) {
|
||||
@@ -164,16 +178,18 @@ async function InfoChannel(interaction) {
|
||||
.setURL(`https://twitch.tv/${channelName}`)
|
||||
let footer = ""
|
||||
liveData.tags.forEach((tag) => {
|
||||
footer += `${footer?',':''} #${tag}`
|
||||
footer += `#${tag} `
|
||||
})
|
||||
embed.setFooter({
|
||||
text: footer
|
||||
})
|
||||
embed.setFooter({
|
||||
text: footer
|
||||
})
|
||||
|
||||
return interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
})
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
}, 60000))
|
||||
} else {
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle(`${channelName} is currently offline!`)
|
||||
@@ -189,7 +205,7 @@ async function InfoChannel(interaction) {
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return interaction.reply({
|
||||
content: `**[ERROR]:** Could not validate channel name for *${channelName}*!\r\n${error}`,
|
||||
content: `**[ERROR]:** Could not validate channel name for *${escapeMarkdown(channelName)}*!\r\n${error}`,
|
||||
ephemeral: true
|
||||
}).then(setTimeout(() => {
|
||||
interaction.deleteReply()
|
||||
|
||||
Reference in New Issue
Block a user