repl.it
@dotcomboom/

Croissant

Nodejs

Croissant is a multi-purpose bot with unique features like Engrish, custom emoji, Neocities stats, and even browsing Gopherspace.

fork
loading
Files
  • index.js
  • commands
  • events
  • public
  • README.md
  • Packager files
  • package-lock.json
  • package.json
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
const Discord = require("discord.js");
const client = new Discord.Client();
const fs = require("fs");
const DBL = require("dblapi.js");

if (process.env.dbkey) {
    const dbl = new DBL(process.env.dbkey, client);

    // Optional events
    dbl.on('posted', () => {
        console.log('Server count posted!');
    })

    dbl.on('error', e => {
        console.log(`Oops! ${e}`);
    })
}

const http = require('http');
const express = require('express');
const app = express();
app.use(express.static('public'));

app.listen(process.env.PORT);

const prefix = process.env.prefix; //Set our prefix

// This loop reads the /events/ folder and attaches each event file to the appropriate event.
fs.readdir("./events/", (err, files) => {
    if (err) return console.error(err);
    files.forEach(file => {
        let eventFunction = require(`./events/${file}`);
        let eventName = file.split(".")[0];
        // super-secret recipe to call events with all their proper arguments *after* the `client` var.
        client.on(eventName, (...args) => eventFunction.run(client, ...args));
    });
});

client.on("message", message => {
    if (message.author.bot) return;
    if (message.content.indexOf(prefix) !== 0) return;

    // This is the best way to define args. Trust me.
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    // The list of if/else is replaced with those simple 2 lines:
    try {
        let commandFile = require(`./commands/${command}.js`);
        commandFile.run(client, message, args);
        console.log("Message detected.")
    } catch (err) {
        console.error(err);
    }
});

client.login(process.env.TOKEN);
?