-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (96 loc) · 3.2 KB
/
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
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
const config = require("./config.json");
const token = require("./config.json");
const Discord = require("discord.js");
const colours = require("./colours.json");
const fs = require("fs");
const bot = new Discord.Client();
bot.commands = new Discord.Collection();
let coins = require("./coins.json");
const ops = {
active: new Map(),
}
fs.readdir("./commands/", (err, files) => {
if(err) console.log(err);
let jsfile = files.filter(f => f.split(".").pop() === "js");
if(jsfile.length <= 0){
console.log("Couldn't find commands.");
return;
}
jsfile.forEach((f, i) =>{
let props = require(`./commands/${f}`);
console.log(`${f} loaded!`);
bot.commands.set(props.help.name, props);
});
});
bot.on("ready", async () => {
console.log(`Bot Logging at discord ${bot.user.tag}!`);
bot.user.setActivity('[嘉神 | JS機械人] | 輸入/help', { type: 'STREAMING' , url:'https://www.twitch.tv/xstarriver'});
});
bot.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let prefixes = JSON.parse(fs.readFileSync("./config.json", "utf8"));
if(!prefixes[message.guild.id]){
prefixes[message.guild.id] = {
prefixes: config.prefix
};
}
let xpAdd = Math.floor(Math.random() * 7) + 8;
console.log(xpAdd);
function set_up (message,xpfile,xpAdd) {
xp = require(`./json/${xpfile}`);
console.log(xpfile,message.author.id,xpAdd);
if(!xp[message.author.id]){
xp[message.author.id] = {
xp: 0,
level: 1
};
}
let curxp = xp[message.author.id].xp;
let curlvl = xp[message.author.id].level;
let nxtLvl = xp[message.author.id].level * 300;
xp[message.author.id].xp = curxp + xpAdd;
if(nxtLvl <= xp[message.author.id].xp){
xp[message.author.id].level = curlvl + 1;
let lvlup = new Discord.RichEmbed()
.setTitle("等級上升!!")
.setColor(colours.orange_dark)
.setThumbnail(message.author.displayAvatarURL)
.addField("新等級", curlvl + 1);
message.channel.send(lvlup);
}
fs.writeFile(`./json/${xpfile}`, JSON.stringify(xp), (err) => {
if(err) console.log(err)
});
}
fs.readdir("./json", function(err, items) {
let xpfile;
let xp;
for (var i=0; i<items.length & xpfile == null; i++) {
xp = require(`./json/${items[i]}`);
if(xp[message.author.id]){
set_up(message,items[i],xpAdd);
xpfile = items[i]
};
};
if (xpfile == null & items.length > 0){
xpfile = items.length - 1
xp = require(`./json/xp_${xpfile}.json`);
if ( Object.keys(xp).length < 61){
set_up(message,`xp_${xpfile}.json`,xpAdd);
};
} else if (xpfile == null){
fs.writeFile(`./json/xp_${items.length}.json`, "{}", (err) => {
set_up(message,`xp_${items.length}.json`,xpAdd);
});
};
});
let prefix = prefixes[message.guild.id].prefixes;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
if (message.content.startsWith(prefix)){
let commandfile = bot.commands.get(cmd.slice(prefix.length));
if(commandfile) commandfile.run(bot,message,args, ops);};
});
bot.login(config.token);