-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
208 lines (189 loc) · 7.5 KB
/
index.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import DiscordJS, { Intents } from "discord.js";
import * as dotenv from "dotenv";
import mysql from "mysql2";
//TODO: create a new .env
//col to const
const ub_userID = "ub_userID";
const ub_bal = "ub_bal";
const th_userID = "th_userID";
const th_time = "th_time";
const th_oper = "th_oper";
const th_postbal = "th_postbal";
//Tables to const
const UserBal = "UserBal";
const TransHist = "TransHist";
dotenv.config();
const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PWD,
database: process.env.DB_NAME,
});
console.clear();
const client = new DiscordJS.Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
const prefix = "$";
client.on("ready", () => {
console.log("China is ready");
});
client.on("messageCreate", (message) => {
let discordUID = message.author.id.toString();
let discordName = message.author.username;
let discordTag = message.author.tag;
let mention = "<@" + discordUID.toString() + ">";
if (message.content.toLowerCase().includes("n word")) {
db.connect(function (err) {
if (err) throw err;
var query = `SELECT ${ub_userID} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
if (err) throw err;
if (result.toString().length === 0) {
//if user not on DataBase => inform to register
message.channel.send(
`${mention} You are not registerd yet. Try ${prefix}register`
);
} else {
//if user exists => apply penalty -9999999
let bal = "";
var query = `SELECT ${ub_bal} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
if (err) throw err;
var output = JSON.parse(JSON.stringify(result));
bal = output[0][`${ub_bal}`];
var amount = -9999999;
let newbal = parseInt(bal) + amount;
var query = `INSERT INTO ${TransHist} (${th_userID},${th_time}, ${th_oper},${th_postbal}) VALUES ('${discordUID}', now(),${amount},${newbal})`;
db.query(query, function (err) {});
if (err) throw err;
var query = `UPDATE ${UserBal} SET ${ub_bal} = ${newbal} WHERE ${ub_userID} = ${discordUID};`;
db.query(query, function (err) {});
if (err) throw err;
message.reply(`YOU LOST CREDIT!!! -9999999`);
});
}
});
});
}
if (!message.content.startsWith(prefix) || message.author.bot) {
return;
}
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.toString().toLowerCase();
if (command === "register" || command === "reg") {
db.connect(function (err) {
if (err) throw err;
var query = `SELECT ${ub_userID} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
if (err) throw err;
if (result.toString().length === 0) {
//if user not on DataBase => insert to DataBase
var msg = "Welcome to the CCP!, " + mention;
message.channel.send(msg);
var query = `INSERT INTO ${UserBal} (${ub_userID}) VALUES (${discordUID})`;
db.query(query, function (err) {});
if (err) throw err;
} else {
//if user exists => output already exists.
message.channel.send(`You are already a member. ${mention}`);
}
});
});
}
if (command === "balance" || command === "bal") {
db.connect(function (err) {
if (err) throw err;
var query = `SELECT ${ub_userID} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
if (err) throw err;
if (result.toString().length === 0) {
//if user not on DataBase => inform to register
message.channel.send(
`${mention} You are not registerd yet. Try ${prefix}register`
);
} else {
//if user exists => output the credit of user
let bal = "";
var query = `SELECT ${ub_bal} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
var output = JSON.parse(JSON.stringify(result));
bal = output[0][`${ub_bal}`];
message.channel.send(`${mention} Your Social Credit is: ${bal}`);
});
}
});
});
}
if (command === "gain" || command === "g") {
db.connect(function (err) {
if (err) throw err;
var query = `SELECT ${ub_userID} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
if (err) throw err;
if (result.toString().length === 0) {
//if user not on DataBase => inform to register
message.channel.send(
`${mention} You are not registerd yet. Try ${prefix}register`
);
} else {
//if user exists => add and display how much was added
let bal = "";
var query = `SELECT ${ub_bal} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
if (err) throw err;
var output = JSON.parse(JSON.stringify(result));
bal = output[0][`${ub_bal}`];
var amount = 20;
let newbal = parseInt(bal) + amount;
var query = `INSERT INTO ${TransHist} (${th_userID},${th_time}, ${th_oper},${th_postbal}) VALUES ('${discordUID}', now(),${amount},${newbal})`;
db.query(query, function (err) {});
if (err) throw err;
var query = `UPDATE ${UserBal} SET ${ub_bal} = ${newbal} WHERE ${ub_userID} = ${discordUID};`;
db.query(query, function (err) {});
if (err) throw err;
message.channel.send(`${mention} +${amount} Social Credits`);
});
}
});
});
}
if (command === "lose" || command === "l") {
db.connect(function (err) {
if (err) throw err;
var query = `SELECT ${ub_userID} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
if (err) throw err;
if (result.toString().length === 0) {
//if user not on DataBase => inform to register
message.channel.send(
`${mention} You are not registerd yet. Try ${prefix}register`
);
} else {
//if user exists => substract and display how much was substracted
let bal = "";
var query = `SELECT ${ub_bal} FROM ${UserBal} WHERE ${ub_userID} = ${discordUID}`;
db.query(query, function (err, result) {
if (err) throw err;
var output = JSON.parse(JSON.stringify(result));
bal = output[0][`${ub_bal}`];
var amount = -20; //add negative number
let newbal = parseInt(bal) + amount;
var query = `INSERT INTO ${TransHist} (${th_userID},${th_time}, ${th_oper},${th_postbal}) VALUES ('${discordUID}', now(),${amount},${newbal})`;
db.query(query, function (err) {});
if (err) throw err;
var query = `UPDATE ${UserBal} SET ${ub_bal} = ${newbal} WHERE ${ub_userID} = ${discordUID};`;
db.query(query, function (err) {});
if (err) throw err;
message.channel.send(`${mention} ${amount} Social Credits`);
});
}
});
});
}
if (command === "bing") {
setTimeout(function () {
message.reply(`qiling 🍦`);
}, 1000);
}
});
client.login(process.env.TOKEN);