-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
159 lines (97 loc) · 3.39 KB
/
Copy pathbot.js
File metadata and controls
159 lines (97 loc) · 3.39 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
155
156
157
158
159
'use strict';
const Discord = require('discord.js');
const fs = require('fs');
const { type } = require('os');
const BOT_TOKEN = `NzMwMDEyNjcxODUxNjI2NTQ3.XwRTgA.Jzk906lm3ybWKsQvsmd9Y19vWk4`;
const client = new Discord.Client();
const prefix = 'wdnf';
const accountsFileName = './accounts.json';
function getBalanceFile () {
console.log("getBalanceFile");
let accounts = fs.readFileSync(accountsFileName, 'utf8', (err, accounts) => {
if (err) {
console.log("File read failed:", err);
}
return (accounts);
});
return (JSON.parse(accounts));
}
function getBalance(user) {
let balance = 0;
const accounts = getBalanceFile();
if(accounts.hasOwnProperty(user)) {
balance = accounts[user][balance];
} else {
balance = 0;
}
return balance;
}
function register (user) {
user = JSON.stringify(user).replace('<@!', '<@');
return {
[user]: {
balance: 0,
transactions: []
}
}
}
function pay (from, to, amount) {
parseInt(amount);
if(amount > 0) {
let accounts = getBalanceFile();
if(!(accounts.hasOwnProperty(from))) {
const newuser = register(from);
accounts.push(newuser);
}
if(!(accounts.hasOwnProperty(to))) {
const newuser = register(to);
accounts.push(newuser);
}
console.log(accounts);
accounts[from][balance] = accounts[from][balance] - amount;
accounts[to][balance] = accounts[to][balance] + amount;
fs.writeFileSync(accountsFileName, JSON.stringify(accounts, null, 4), function writeJSON(err) {
if (err) return console.log(err);
console.log(JSON.stringify(accounts));
console.log('writing to ' + accountsFileName);
});
return true;
} else {
console.log(typeof amount);
console.log(amount);
return false;
}
}
client.once('ready', () => {
console.log('Bot is ready!');
});
client.on('message', message => {
// client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length + 1).split(' ');
// console.log(args);
const command = args.shift().toLowerCase();
if(command == 'pay') {
const reciever = args[0].replace('<@!', '<@');
const amount = args[1];
const from = JSON.stringify(message.author.id).replace('<@!', '<@');
if((getBalance(from) - amount) <= 0) {
if(pay(message.author, args[0], args[1])) {
message.channel.send(`${reciever} + ${amount} WDNF`);
} else {
message.channel.send(`An error occured, the transaction is invalid.`);
}
} else {
message.channel.send(`Debt is not allowed, ${message.author}`);
}
} else if (command == 'bal') {
const balance = getBalance(message.author);
console.log(balance);
message.channel.send(`${message.author}'s balance is ${balance}`);
} else if (command == 'help') {
message.channel.send(`WDNF - the Web Developers & Friends bot \n help / pay / bal / top`);
} else if (command == 'top') {
message.channel.send(`This will display the richest members.`);
}
});
client.login(BOT_TOKEN);