-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (137 loc) · 5.57 KB
/
Copy pathindex.js
File metadata and controls
154 lines (137 loc) · 5.57 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require('dotenv').config();
const Commands = require('./src/commands');
const PREFIX = '!';
const db = require('./src/db');
const BonfireCache = require("./src/bonfireCache");
const Models = require("./src/db/models");
const input = process.argv.slice(-1)[0]?.toUpperCase();
const readline = require('readline');
const { mockClient } = require("./utils");
// Eris setup
const eris = require("eris");
const discordEnvironment = `DISCORD_TOKEN_${process.argv.slice(-1)[0]?.toUpperCase()}`;
const token = process.env[discordEnvironment];
const client = input !== "DEVELOP"
? new eris.Client(token)
: null;
// Cache setup
const bonfireCache = new BonfireCache(db, client);
const commands = new Commands(bonfireCache);
async function getOrCreateBonfire() {
return await bonfireCache.createBonfire().catch((err) => console.warn(err));
}
async function handleMessage(client, msg, name, isDeveloping = false) {
if (isDeveloping) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
if (!name) {
rl.question('What is your name? ', (answer) => {
name = answer;
console.log(`Oh, hi ${name}\n\n`);
return handleMessage(client, null, name, true);
});
}
rl.question('What would you like to do? ', async (answer) => {
console.log(`answer? \n`, answer)
const [command, ...args] = answer.slice(1).split(" ");
if (!commands.commandList.hasOwnProperty(command)) {
console.log("Unknown command.");
} else {
const commandLookup = commands.commandList[command];
try {
let message = "Unknown command.";
if (commandLookup) {
const user = await bonfireCache.getUser(name);
try {
message = await commandLookup.call(this, client, null, user, ...args);
} catch (err) {
console.warn(`error processing request: ${err}`);
message = 'Houston, we have a problem.';
}
user.lastPrompt = command;
}
console.log(message);
} catch (err) {
console.warn(`Had an err, boss: ${err}`);
}
}
handleMessage(client, null, name, true);
});
} else {
const botWasMentioned = msg.content.startsWith(PREFIX);
if (botWasMentioned) {
await msg.channel.sendTyping();
const [command, ...args] = msg.content.slice(1).split(" ");
if (!commands.commandList.hasOwnProperty(command)) {
client.createMessage(msg.channel.id, {
embed: {
title: `:interrobang: Unknown command.`, // Title of the embed
author: { // Author property
name: msg.author.username,
icon_url: msg.author.avatarURL
},
color: 0x008000, // Color, either in hex (show), or a base-10 integer
fields: [ // Array of field objects
{
name: "Some extra info.", // Field title
value: "Some extra value.", // Field
inline: true // Whether you want multiple fields in same line
},
{
name: "Some more extra info.",
value: "Another extra value.",
inline: true
}
],
footer: { // Footer text
text: "Created with Eris."
}
}
});
} else {
const commandLookup = commands.commandList[command];
try {
let message = "Unknown command.";
if (commandLookup) {
const user = await bonfireCache.getUser(msg.member.username);
try {
return await commandLookup.call(this, client, msg, user, ...args);
} catch (err) {
console.warn(`error processing request: ${err}`);
message = 'Houston, we have a problem.';
}
user.lastPrompt = command;
}
// await msg.channel.createMessage(message);
} catch (err) {
console.warn(`Had an err, boss: ${err}`);
}
}
}
}
}
if (input === "DEVELOP") {
console.log('we are developing.');
getOrCreateBonfire();
handleMessage(mockClient, null, null, true);
} else {
client.on('ready', async () => {
getOrCreateBonfire();
client.on('messageCreate', async (msg) => {
handleMessage(client, msg, null, false);
});
client.on('error', (error) => {
console.warn(error);
});
client.on('guildMemberAdd', async (guild, member) => {
await msg.channel.createMessage(`Hello ${member.name}! Welcome to BonfireBot!`);
});
});
client.connect();
}
module.exports = {
bonfireCache,
}