forked from ChristopherBThai/Discord-OwO-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7617c9
commit 5a805c0
Showing
6 changed files
with
342 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.