Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit 8357193

Browse files
authored
Better Error Handling
Improve chatbot and add Errors for unsupported versions
2 parents 6d9d1fb + 7368e38 commit 8357193

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

simplydjs.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
//Checking if the NodeJS version is 16 or higher
2+
if (process.version.slice(1, 3) - 0 < 16)
3+
throw new Error(
4+
`NodeJS Version 16 or newer is required, but you are using ${process.version}. See https://nodejs.org/`
5+
)
6+
7+
//Checking if Discord.js is installed, bcs why not
8+
try {
9+
require('discord.js')
10+
} catch (e) {
11+
throw new Error('Discord.JS is required for this package to run')
12+
}
13+
14+
//Getting the version of discord.js
15+
const { version: discordJSVersion } = require(require('path').join(
16+
require.resolve('discord.js'),
17+
'..',
18+
'..',
19+
'package.json'
20+
))
21+
22+
//Checking if discord.js is at version 13
23+
if (discordJSVersion.slice(0, 2) !== '13')
24+
throw new Error(
25+
`Discord.JS version 13 is required, but you are using ${discordJSVersion}. See https://www.npmjs.com/package/discord.js`
26+
)
27+
128
// General
229
module.exports.ghostPing = require('./src/ghostPing')
330

src/chatbot.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
const Discord = require('discord.js')
22
const fetch = require('node-fetch')
33

4-
/**
5-
* @param {Discord.Client} client
6-
* @param {Discord.Message} message
7-
* @param {import('../index').chatbotOptions} options
8-
*/
9-
104
/**
115
--- options ---
126
@@ -16,6 +10,12 @@ name => String
1610
developer => String
1711
*/
1812

13+
/**
14+
* @param {Discord.Client} client
15+
* @param {Discord.Message} message
16+
* @param {import('../index').chatbotOptions} options
17+
*/
18+
1919
async function chatbot(client, message, options = {}) {
2020
if (message.author.bot) return
2121
if (options && options.toggle === false) return
@@ -39,33 +39,39 @@ async function chatbot(client, message, options = {}) {
3939
//Return if the channel of the message is not a chatbot channel
4040
if (!channels.includes(message.channel.id)) return
4141

42-
const botName = options.name ?? client.user.username,
43-
developer = options.developer ?? 'Rahuletto#0243',
44-
ranges = [
45-
'\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
46-
'\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
47-
'\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
48-
]
42+
const ranges = [
43+
'\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
44+
'\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
45+
'\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
46+
]
4947

50-
let input = message.content.replace(new RegExp(ranges.join('|'), 'g'), '.')
48+
let input = message.cleanContent.replace(
49+
new RegExp(ranges.join('|'), 'g'),
50+
'.'
51+
)
5152

5253
//Replacing Emojis
53-
const hasEmoteRegex = /<a?:.+:\d+>/gm
54-
const emoteRegex = /<:.+:(\d+)>/gm
55-
const animatedEmoteRegex = /<a:.+:(\d+)>/gm
54+
input = input.replace(/<a?:.+:\d+>/gm, '')
5655

57-
const emoj = message.content.match(hasEmoteRegex)
56+
await message.channel.sendTyping()
5857

59-
input = input
60-
.replace(emoteRegex.exec(emoj), '')
61-
.replace(animatedEmoteRegex.exec(emoj), '')
58+
const url = new URL('https://api.affiliateplus.xyz/api/chatbot'),
59+
params = url.searchParams,
60+
age = new Date().getFullYear() - client.user.createdAt.getFullYear()
6261

63-
message.channel.sendTyping()
62+
params.set('message', input)
63+
params.set('ownername', options.developer ?? 'Rahuletto#0243')
64+
params.set('botname', options.name ?? client.user.username)
65+
params.set('age', age)
66+
params.set('birthyear', client.user.createdAt.getFullYear())
67+
params.set('birthdate', client.user.createdAt.toLocaleDateString())
68+
params.set('birthplace', 'Simply-DJS')
69+
params.set('location', message.guild.name)
70+
params.set('company', 'Simply-DJS')
71+
params.set('user', message.author.id)
6472

6573
// Using await instead of .then
66-
const jsonRes = await fetch(
67-
`https://api.affiliateplus.xyz/api/chatbot?message=${input}&botname=${botName}&ownername=${developer}&user=${message.author.id}&birthplace=Simply-DJS`
68-
).then((res) => res.json()) // Parsing the JSON
74+
const jsonRes = await fetch(url).then((res) => res.json()) // Parsing the JSON
6975

7076
const chatbotReply = jsonRes.message
7177
.replace(/@everyone/g, '`@everyone`') //RegExp with g Flag will replace every @everyone instead of just the first
@@ -77,9 +83,11 @@ async function chatbot(client, message, options = {}) {
7783
})
7884
} catch (err) {
7985
if (err instanceof fetch.FetchError) {
80-
message.reply({ content: '**Error:**\n```' + err + '```' }) //Catch errors that happen while fetching
81-
}
82-
console.log(`Error Occured. | chatbot | Error: ${err.stack}`)
86+
if (err.type === 'invalid-json') {
87+
message.reply({ content: '**Error:**\n```API offline```' }) //Catch errors that happen while fetching
88+
console.log(`Error Occured. | chatbot | Error: API offline`)
89+
}
90+
} else console.log(`Error Occured. | chatbot | Error: ${err.stack}`)
8391
}
8492
}
8593

0 commit comments

Comments
 (0)