11const Discord = require ( 'discord.js' )
22const 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
1610developer => String
1711 */
1812
13+ /**
14+ * @param {Discord.Client } client
15+ * @param {Discord.Message } message
16+ * @param {import('../index').chatbotOptions } options
17+ */
18+
1919async 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 ( / @ e v e r y o n e / 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