Skip to content

Commit

Permalink
Added giveaway tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBThai committed Sep 25, 2023
1 parent a7617c9 commit 5a805c0
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/commands/commandList/admin/creategiveaway.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module.exports = new CommandInterface({
manager: true,

execute: async function () {
await this.giveaway.createGiveaway(this.msg.channel.id);
await this.giveaway.createGiveaway.bind(this)(this.msg.channel.id, this.msg.author, false);
},
});
88 changes: 88 additions & 0 deletions src/commands/commandList/admin/giveGiveawayTicket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* OwO Bot for Discord
* Copyright (C) 2023 Christopher Thai
* This software is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
* For more information, see README.md and LICENSE
*/

const CommandInterface = require('../../CommandInterface.js');

module.exports = new CommandInterface({
alias: ['givegiveawayticket', 'ggt'],

owner: true,
admin: true,
manager: true,

execute: async function (p) {
const list = await parseUsers(p);
p.send(list.success + '\n\n' + list.failed);
},
});

async function parseUsers(p) {
let success = '';
let failed = '';
const lines = p.args.join(' ').split(/\n+/gi);
for (let line of lines) {
const args = line
.replace(/[^ \d]/gi, ' ')
.trim()
.split(/\s+/gi);
try {
let result = await giveTicket(p, args[0], args[1]);
if (result) {
success += `\`[${result.count}] [${result.user.id}] ${p.getUniqueName(result.user)}\`\n`;
} else {
failed += `\`failed for [${args.join(', ')}]\`\n`;
}
} catch (err) {
console.error(err);
failed += `failed for [${args.join(', ')}]\n`;
}
}
return { success, failed };
}

async function giveTicket(p, id, count = 1) {
//Parse id
if (!p.global.isUser(id) && !p.global.isUser('<@' + id + '>')) {
p.errorMsg(', Invalid user id: ' + id);
return;
}

// Parses count
if (count && p.global.isInt(count)) count = parseInt(count);
if (!count) {
p.errorMsg(', invalid # of tickets');
return;
}

let type = 'giveaway_tickets';
let name = 'Giveaway Ticket';
if (Math.abs(count) > 1) name += 's';
let emoji = p.config.emoji.perkTicket.giveaway;

// Fetch uid first
const uid = await p.global.getUid(id);

// Query result
let sql = `INSERT INTO user_item (uid, name, count) VALUES (${uid}, '${type}', ${count}) ON DUPLICATE KEY update count = count + ${count};`;
await p.query(sql);

// Send msgs
let user;
if (count > 0)
user = await p.sender.msgUser(
id,
`${emoji} **|** Thank you! You received **${count} ${emoji} ${name}**!`
);
else
user = await p.sender.msgUser(
id,
`${emoji} **|** Sorry about that. You have lost **${Math.abs(count)} ${emoji} ${name}**.`
);

if (user && !user.dmError) return { user, count };
else await p.errorMsg(', Failed to message user for ' + id);
}
Loading

0 comments on commit 5a805c0

Please sign in to comment.