forked from Yamboy1/DiscordMarkupLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (90 loc) · 4.26 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
const Discord = require('discord.js')
const client = new Discord.Client({ disableEveryone: true })
const simple = require('./lib/commandDisplayHandler')
const parser = require('./lib/stringParser')
const fs = require('fs-extra')
const cheerio = require('cheerio')
const chalk = require('chalk')
const _ = require('lodash');
const commands = new Set()
let botPrefix = null
let owner = null
if (!fs.existsSync('./markup/bot.dml')) {
fs.createFileSync('./markup/bot.dml')
fs.writeFileSync('./markup/bot.dml', fs.readFileSync('./generated/bot.txt'))
console.error(chalk.red('We notcied your bot.dml file was missing, so DML automatically generated it for you. Please rerun your script'))
}
if (!fs.existsSync('./markup/commands/')) {
fs.createFileSync('./markup/commands/ping.dml')
fs.writeFileSync('./markup/commands/ping.dml', fs.readFileSync('./generated/ping.txt'))
console.error(chalk.red('We noticed your commands directory was missing, so we automatically generated it along with an example ping script for you'))
}
const $ = cheerio.load(fs.readFileSync('./markup/bot.dml'))
fs.readdir('./markup/commands/', (err, files) => {
files.forEach(file => {
if (!file.endsWith('.dml')) return
console.log(chalk.blue.bgGreen(`Loaded file: ${chalk.red(file)}`))
let commandName = file.split(".")[0];
commands.add(commandName)
});
})
client.on('message', async message => {
if (message.channel.type == "dm") return
if (!message.content.startsWith(botPrefix) || message.author.bot) return
const args = message.content.slice(Object.keys(botPrefix).length).trim().split(/ +/g)
const command = args.shift().toLowerCase()
if (!commands.has(command)) return
const $$ = cheerio.load(parser.parse(`./markup/commands/${command}.dml`, client, message))
const responseE = $$('response');
if ($$('script').get()[0] != undefined) {
const scripts = $$('script').get()[0].children[0].data
try {
eval(scripts)
} catch (error) {
throw new Error(chalk.red('DML Script Tag Error'))
}}
if (responseE.length === 0) return console.log(chalk.bgWhite.red('Command Missing <response> element!'))
if (args.length === 0 || $$('arg').length === 0) return simple.embed('response', $$, responseE, message)
else {
$$('arg').each((i, el) => {
if (args[0] > $$('arg').length) return
elem = $$(el)
if (args[0] === elem.attr('value')) {
const argE = elem
simple.embed(`arg[value=${i + 1}]`, $$, argE, message)
}
})
}
})
client.on('ready', () => {
let presenceE = $('settings > presence').first()
let presenceType;
if (!presenceE.text()) console.log(chalk.green.underline('No presence entered!'))
if (!presenceE.attr('type')) presenceType = 'PLAYING'
else (presenceType = presenceE.attr('type'))
client.user.setActivity(presenceE.text(), {type: `${presenceType}`})
let startupE = $('startup').first()
console.log(chalk.green.underline('Discord Markup Language Has Launched Successfully!'))
try {
if (!startupE.attr('channel')) console.log(chalk.yellow.inverse('! No startup channel set in: bot.dml !'))
else client.channels.get(startupE.attr('channel')).send((startupE.attr('embed') === 'true') ? new Discord.RichEmbed().setDescription(startupE.text()).setColor(startupE.attr('color')) : startupE.text())
botPrefix = $('settings > prefix').text()
owner = $('settings > owner').text().split(',')
_.each(owner, (value) => {
console.log(`${chalk.yellow(client.users.get(value).tag)} Is A Bot Owner!`)
})
}
catch (e) {
if (e.message.includes('send')) console.log(chalk.yellow.inverse(`Error sending message to startup channel. Possibly invalid channel ID?\nParser error: ${e.message}`))
if (e.message.includes('tag')) console.log(chalk.yellow.inverse(`Error grabbing owner usernames. Possibly invalid user ID?\nParser error: ${e.message}`))
}
})
client.on('error', (err) => {
console.log(err)
})
client.on('warn', (warn) => {
console.log(warn)
})
client.login($('settings > token').text()).catch(() => {
console.error(chalk.red(`${chalk.blue($('settings > token').text())} is an incorrect bot token!`))
})