Skip to content

Commit e6b1044

Browse files
committed
feat: check if user has a code already
1 parent ee61320 commit e6b1044

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/commands/Promocodes/ClaimPromocode.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ class ClaimPromocode extends Command {
3939
this.messageManager.reply(message, 'Your password is invalid, or you didn\'t provide one and the pool is not open.');
4040
return this.messageManager.statuses.FAILURE;
4141
}
42+
if (this.settings.hasCodeInPool(message.author, pool)) {
43+
this.messageManager.reply(message, `<@${message.author.id}> already has a code from `);
44+
return this.messageManager.statuses.FAILURE;
45+
}
4246
if (grantedTo === null) {
4347
await this.settings.grantCode(code, message.author.id, message.author.id, platform);
4448
this.messageManager.reply(message, `Code claimed by <@${message.author.id}>`);
4549
return this.messageManager.statuses.SUCCESS;
4650
}
47-
return this.messageManager.statuses.SUCCESS;
51+
return this.messageManager.statuses.FAILURE;
4852
}
4953
}
5054

src/settings/DatabaseQueries/PromocodeQueries.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ class PromocodeQueries {
5353
return res[0].pool_restricted;
5454
}
5555

56+
async hasCodeInPool(member, pool) {
57+
const query = SQL`SELECT count(cm.code) as count
58+
FROM code_pool_member cm
59+
WHERE granted_to = ${member.id} and pool_id = ${pool};`;
60+
const res = await this.db.query(query);
61+
if (res[0].length === 0) {
62+
return undefined;
63+
}
64+
return res[0].count > 0;
65+
}
66+
5667
async isPoolPublic(id) {
5768
const query = SQL`SELECT pool_public FROM code_pool WHERE pool_id = ${id};`;
5869
const res = await this.db.query(query);

0 commit comments

Comments
 (0)