-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
171 lines (152 loc) · 5.41 KB
/
bot.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//MAKE SURE DATABASE IS OPEN BEFORE SQL IS CALLED
const Discord = require('discord.js');
const Database = require('better-sqlite3');
const client = new Discord.Client();
let db = new Database("inventory.db");
let initDatabase = db.prepare("CREATE TABLE IF NOT EXISTS inventory(name text, items text)");
initDatabase.run();
let insertNewPlayer = db.prepare("INSERT INTO inventory(name, items) VALUES (?,?)");
let checkPlayerInventory = db.prepare("SELECT items FROM inventory WHERE name = ?");
let addPlayerInventory = db.prepare("UPDATE inventory SET items = (?) WHERE name = (?)")
db.close();
var feedback = '';
var timer;
client.on('ready', () => {
console.log('I am fully charged!');
});
const prefix = '>';
client.on('message', message => {
if (message.author.bot) return;
if (message.content.startsWith(prefix)) {
let args = message.content.substring(1).split(' ');
let cmd = args[0];
let subcmd = '';
if (args.length > 1) {
subcmd = args[1];
}
if (cmd == "status") {
message.reply('I\'m online! Hello!');
}
else if (cmd == "help") {
message.channel.send('Current commands: \n**status:** Shows if bot is online (if you can read this then don\'t bother lol) \n**help:** Displays this menu\n**roll:** Rolls a die/dice, syntax is >roll [number of dice]d[number of sides] \n')
}
/**
DICE ROLL FUNCTION
**/
else if (cmd == "roll") {
let check = /^\d+$/;
//
if (subcmd != '') {
if (subcmd.split('')[0] == 'd') {
let sides = subcmd.split('d')[1]
if ( check.test(sides) ) {
if (sides > 1) {
message.reply("You rolled: **" + String( Math.ceil(Math.random()*sides) ) + ".**" )
}
else {
message.reply('The number of sides needs to be above 1!')
}
}
else {
message.reply('You need to give a correct value for the number of sides! (Must be above 1 and be a number)')
}
}
//
else if (check.test(subcmd.split('d')[0]) && check.test(subcmd.split('d')[1])) {
if (subcmd.split('d')[0] < 1) {
message.reply('The number of dice rolled must be 1 or above!')
}
else {
let dice = subcmd.split('d')[0]
let sides = subcmd.split('d')[1]
if (sides > 1) {
let results = 'Your rolls are: **';
for (let n = 0; n < dice; n++) {
results = results.concat( String( Math.ceil(Math.random()*sides)))
if (n+1<dice) {
results = results.concat(", ")
}
}
results = results.concat('.**')
message.reply( results )
}
else {
message.reply('The number of sides needs to be above 1!')
}
}
}
//
else {
message.reply('You need to use the correct format for this! (e.g. >roll 2d20 OR >roll d12)')
}
}
else {
message.reply('You didn\'t give any arguments for this command!')
}
}//closes if statement checking if cmd is 'roll'
/**
INVENTORY MANAGEMENT COMMANDS
**/
else if (cmd == "addPlayer") {
if (message.member.roles.cache.some(role => role.name === 'Realm Master')) {
if (subcmd != '') {
let db = new Database("inventory.db");
if (db.open) {
feedback = insertNewPlayer.run(String(subcmd), '');
message.channel.send('Player successfully added.');
db.close();
}
}
else {
message.channel.send('You need to provide a valid player!')
}
}
else {
message.channel.send('You must have the Realm Master role to use this!')
}
}//closes if statement for 'add'
else if (cmd == "checkInventory") {
if (subcmd != '') {
const db = new Database("inventory.db");
feedback = '';
feedback = checkPlayerInventory.get(String(subcmd));
if (feedback != '') {
message.channel.send('Current inventory items: ' + String(feedback.items));
clearInterval(timer);
db.close();
}
}
else {
message.channel.send('You need to provide a valid player!');
}
}//closes if statement for 'checkInventory'
else if (cmd == "addItem") {
if (subcmd != '') {
const db = new Database("inventory.db");
feedback = '';
feedback = checkPlayerInventory.get(String(subcmd));
function waitUntilComplete() {
if (feedback != '') {
if (feedback.items == '') {
addPlayerInventory.run( String(args[2]), String(subcmd));
}
else {
addPlayerInventory.run( (String(feedback.items) +", "+ String(args[2])), String(subcmd));
}
db.close();
clearInterval(timer);
}
}//endfunction
timer = setInterval(waitUntilComplete, 100);
message.channel.send('Added item to inventory.')
}
else {
message.channel.send('Syntax is invalid, make sure you are adding an item correctly')
}
}//closes if statement for 'addItem'
};//closes if statement checking for '>'
if (message.content == 'meow') {
message.channel.send('meow');
};
});//close client.on
client.login(process.env.BOT_TOKEN);