-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (143 loc) · 3.95 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
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
const fs = require('fs');
const Discord = require('discord.js');
const Sequelize = require('sequelize');
const { prefix, token } = require('./config.json');
const getPlayerCount = require('./playerCount.js');
// create a new Discord client
const client = new Discord.Client();
client.commands = new Discord.Collection();
// Database connection information
const sequelize = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
// SQLite only
storage: 'database.sqlite',
});
// Create "Clan" table
const clan = sequelize.define('clan', {
clanId: {
primaryKey: true,
autoIncrement: true,
type: Sequelize.INTEGER,
unique: true,
allowNull: false,
},
name: {
type: Sequelize.STRING,
unique: true,
defaultValue: '',
allowNull: false,
},
ownerUserId: {
type: Sequelize.STRING,
unique: true,
defaultValue: 0,
allowNull: false,
},
roleId: {
type: Sequelize.STRING,
unique: true,
defaultValue: 0,
allowNull: false,
},
memberCount: {
type: Sequelize.INTEGER,
defaultValue: 0,
allowNull: false,
},
prefix: {
type: Sequelize.STRING,
allowNull: true,
},
});
// Create "Member" table
const member = sequelize.define('member', {
memberId: {
primaryKey: true,
autoIncrement: true,
type: Sequelize.INTEGER,
unique: true,
allowNull: false,
},
username: {
type: Sequelize.STRING,
unique: true,
defaultValue: '',
allowNull: false,
},
memberUserId: {
type: Sequelize.STRING,
unique: true,
defaultValue: 0,
allowNull: false,
},
clanName: {
type: Sequelize.STRING,
unique: false,
allowNull: true,
foreignKey: clan.name,
},
});
// Create "PlayerCount" table
const playerCount = sequelize.define('playerCount', {
id: {
primaryKey: true,
autoIncrement: true,
type: Sequelize.INTEGER,
unique: true,
allowNull: false,
},
playerPeak: {
type: Sequelize.INTEGER,
unique: false,
defaultValue: 0,
allowNull: false,
},
});
// Find all commands
const commandFiles = fs
.readdirSync('./commands/clan')
.filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/clan/${file}`);
// set a new item in the Collection
// with the key as the command name and the value as the exported module
client.commands.set(command.name, command);
}
// when the client is ready, run this code
// this event will only trigger one time after logging in
client.once('ready', async () => {
console.log('Ready!');
// set playing status
await client.user.setActivity('Fanix.io');
// sync the tables
clan.sync();
member.sync();
playerCount.sync();
});
client.on('message', message => {
if (message.content.startsWith('!startPlayerCount')) {
message.channel.send('Successfully started the Player Count updater!');
setInterval(() => {
getPlayerCount.getCurrentPlayers(message, playerCount);
}, 30000);
}
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) {
return message.channel.send(
`Unknown command. Use **${prefix}help** to get a list of all commands.`,
);
} else {
try {
client.commands.get(command).execute(message, args, clan, member);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
}
});
// login to Discord with your app's token
client.login(token);