-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
202 lines (102 loc) · 4.13 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
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
const { Client, Events, Collection, GatewayIntentBits, EmbedBuilder, codeBlock } = require(`discord.js`);
const { token } = require("./config.json");
const fs = require('node:fs');
const path = require('node:path');
//currency
const { Op } = require('sequelize');
const { Users } = require('./dbInit.js');
const currency = new Collection();
const prefix = '!';
const client = new Client({ intents:
[ GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent
]
}
);
client.once(Events.ClientReady, async () => {
//sync currency collection (seq)
const storedBalances = await Users.findAll();
storedBalances.forEach(b => currency.set(b.user_id, b));
console.log('Ready');
});
client.commands = new Collection();
//Slash command handler
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
//command handler
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else if ('execute' in command && !('data' in command)) {
client.commands.set(command.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
client.on('messageCreate', (message) => {
if (message.author.bot) {return;}
else if (message.content.includes("cry") || message.content.includes("crying")) {
client.commands.get('cryreply').execute(message);
} else {
client.commands.get("inspo").execute(message);
}
});
client.on('guildMemberAdd', (member) => {
client.commands.get("welcome").execute(member);
});
//currency commands
client.on('messageCreate', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
const messageArray = message.content.split(" ");
const argument = messageArray.slice(1);
const cmd = messageArray[0];
//First mentioned user
var target = message.mentions.users.first();
//For no mentions
if (target == undefined) {
var target = message.author;
}
const targetName = "<@" + target + ">";
if (command == "bal") {
return message.channel.send(`${targetName} has ${getBalance(target.id)}`);
} else if (command == "give") {
client.commands.get("give").execute(message, getBalance, messageArray, addBalance, target);
} else if (command == "modgive") {
const amt = Number(messageArray[1]);
message.channel.send(`${amt} given to <@${target.id}>`);
addBalance(target.id, amt);
} else if (command == "help") {
client.commands.get("help").execute(message);
}
});
client.on('messageCreate', (message) => {
if (message.author.bot) return;
client.commands.get('iamreply').execute(message);
});
//currency part 2 (helper functions)
async function addBalance(id, amount) {
const user = currency.get(id);
if (user) {
user.balance += amount;
return user.save();
}
const newUser = await Users.create({user_id: id, balance:amount});
currency.set(id, newUser);
return newUser;
}
function getBalance(id) {
const user = currency.get(id);
if (user) {
return user.balance;
} else {
return;
}
}
client.login(token);