mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-18 21:11:39 +01:00
add balance,donate,admindonate commands and minor bugfixes
This commit is contained in:
@@ -25,6 +25,14 @@ COMMAND|FUNCTION|USER|ALIASES|DISABLEABLE
|
||||
`yabai`|Random number|anyone|`yabai` `goon`|:white_check_mark:
|
||||
`seiso`|Random number|anyone|`seiso`|:white_check_mark:
|
||||
|
||||
### Qweribucks commands
|
||||
|
||||
COMMAND|FUNCTION|USER|ALIASES|DISABLEABLE
|
||||
-|-|-|-|-
|
||||
`getbalance [target]`|Get balance of target or self|anyone|`getbalance` `balance` `qbucks` `qweribucks` `wallet` `getwallet`|:white_check_mark:
|
||||
`donate {target} {amount}`|Give the targeted user some or all of your qweribucks|anyone|`donate`|:white_check_mark:
|
||||
`admindonate {target} {amount}`|Gives the targeted user amount of qweribucks|admins|`admindonate`|:white_check_mark:
|
||||
|
||||
### Item commands
|
||||
|
||||
COMMAND|FUNCTION|USER|ALIASES|DISABLEABLE
|
||||
|
||||
25
bot/commands/admindonate.ts
Normal file
25
bot/commands/admindonate.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Command, sendMessage } from ".";
|
||||
import { getUserRecord } from "../db/dbUser";
|
||||
import { changeBalance } from "../lib/changeBalance";
|
||||
import parseCommandArgs from "../lib/parseCommandArgs";
|
||||
import { User } from "../user";
|
||||
|
||||
export default new Command('admindonate', ['admindonate'], 'admin', async msg => {
|
||||
const args = parseCommandArgs(msg.messageText);
|
||||
if (!args[0]) { await sendMessage('Please specify a user', msg.messageId); return; };
|
||||
const target = await User.initUsername(args[0].toLowerCase());
|
||||
if (!target) { await sendMessage(`Chatter ${args[0]} doesn't exist`, msg.messageId); return; };
|
||||
const userRecord = await getUserRecord(target);
|
||||
if (!args[1]) { await sendMessage('Please specify the amount qweribucks you want to give', msg.messageId); return; };
|
||||
const amount = Number(args[1]);
|
||||
if (isNaN(amount)) { await sendMessage(`${args[1]} is not a valid amount`); return; };
|
||||
if (await target.itemLock()) { await sendMessage('Cannot give qweribucks: item transaction in progress', msg.messageId); return; };
|
||||
await target.setLock();
|
||||
const data = await changeBalance(target, userRecord, amount);
|
||||
if (!data) {
|
||||
await sendMessage(`Failed to give ${target.displayName} ${amount} qweribuck${amount === 1 ? '' : 's'}`, msg.messageId);
|
||||
} else {
|
||||
await sendMessage(`${target.displayName} now has ${data.balance} qweribuck${data.balance === 1 ? '' : 's'}`, msg.messageId);
|
||||
};
|
||||
await target.clearLock();
|
||||
});
|
||||
46
bot/commands/donateqbucks.ts
Normal file
46
bot/commands/donateqbucks.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Command, sendMessage } from ".";
|
||||
import type { userRecord } from "../db/connection";
|
||||
import { getUserRecord } from "../db/dbUser";
|
||||
import parseCommandArgs from "../lib/parseCommandArgs";
|
||||
import { changeBalance } from "../lib/changeBalance";
|
||||
import { User } from "../user";
|
||||
|
||||
export default new Command('donate', ['donate'], 'chatter', async (msg, user) => {
|
||||
const args = parseCommandArgs(msg.messageText);
|
||||
if (!args[0]) { await sendMessage('Please specify a user', msg.messageId); return; };
|
||||
const target = await User.initUsername(args[0].toLowerCase());
|
||||
if (!target) { await sendMessage(`Chatter ${args[0]} doesn't exist`, msg.messageId); return; };
|
||||
const targetRecord = await getUserRecord(target);
|
||||
if (!args[1]) { await sendMessage('Please specify the amount of the item you want to give', msg.messageId); return; };
|
||||
const amount = Number(args[1]);
|
||||
if (isNaN(amount) || amount < 0) { await sendMessage(`${args[1]} is not a valid amount`); return; };
|
||||
|
||||
const userRecord = await getUserRecord(user);
|
||||
if (userRecord.balance < amount) { await sendMessage(`You can't give qweribucks you don't have!`, msg.messageId); return; };
|
||||
|
||||
if (await user.itemLock() || await target.itemLock()) { await sendMessage('Cannot give qweribucks. Please try again!', msg.messageId); return; };
|
||||
|
||||
await Promise.all([
|
||||
user.setLock(),
|
||||
target.setLock()
|
||||
]);
|
||||
|
||||
const data = await Promise.all([
|
||||
await changeBalance(target, targetRecord, amount),
|
||||
await changeBalance(user, userRecord, -amount)
|
||||
]);
|
||||
|
||||
if (!data.includes(false)) {
|
||||
const { balance: newamount } = data[0] as userRecord;
|
||||
await sendMessage(`${user.displayName} gave ${amount} qweribuck${amount === 1 ? '' : 's'} to ${target.displayName}. They now have ${newamount} qweribuck${newamount === 1 ? '' : 's'}`, msg.messageId);
|
||||
} else {
|
||||
// TODO: Rewrite this section
|
||||
await sendMessage(`Failed to give ${target.displayName} ${amount} qbuck${(amount === 1 ? '' : 's')}`, msg.messageId);
|
||||
console.error(`WARNING: Qweribucks donation failed: target success: ${data[0] !== false}, donator success: ${data[1] !== false}`);
|
||||
};
|
||||
|
||||
await Promise.all([
|
||||
user.clearLock(),
|
||||
target.clearLock()
|
||||
]);
|
||||
});
|
||||
12
bot/commands/getbalance.ts
Normal file
12
bot/commands/getbalance.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Command, sendMessage } from ".";
|
||||
import { getUserRecord } from "../db/dbUser";
|
||||
import parseCommandArgs from "../lib/parseCommandArgs";
|
||||
import { User } from "../user";
|
||||
|
||||
export default new Command('getbalance', ['getbalance', 'balance', 'qbucks', 'qweribucks', 'wallet', 'getwallet'], 'chatter', async (msg, user) => {
|
||||
const args = parseCommandArgs(msg.messageText);
|
||||
const target = args[0] ? await User.initUsername(args[0].toLowerCase()) : user;
|
||||
if (!target) { await sendMessage(`Chatter ${args[0]} doesn't exist!`, msg.messageId); return; };
|
||||
const data = await getUserRecord(target);
|
||||
await sendMessage(`${target.displayName} has ${data.balance} qbuck${data.balance === 1 ? '' : 's'}`, msg.messageId);
|
||||
});
|
||||
@@ -22,8 +22,12 @@ export default new Command('give', ['give'], 'chatter', async (msg, user) => {
|
||||
if (userRecord.inventory[item.name]! < amount) { await sendMessage(`You can't give items you don't have!`, msg.messageId); return; };
|
||||
|
||||
if (await user.itemLock() || await target.itemLock()) { await sendMessage('Cannot give item. Please try again!', msg.messageId); return; };
|
||||
await user.setLock();
|
||||
await target.setLock();
|
||||
|
||||
await Promise.all([
|
||||
user.setLock(),
|
||||
target.setLock()
|
||||
]);
|
||||
|
||||
const data = await Promise.all([
|
||||
await changeItemCount(target, targetRecord, item.name, amount),
|
||||
await changeItemCount(user, userRecord, item.name, -amount)
|
||||
@@ -36,7 +40,7 @@ export default new Command('give', ['give'], 'chatter', async (msg, user) => {
|
||||
} else {
|
||||
// TODO: Rewrite this section
|
||||
await sendMessage(`Failed to give ${target.displayName} ${amount} ${item.prettyName + (amount === 1 ? '' : item.plural)}`, msg.messageId);
|
||||
console.error(`WARNING: Item donation failed: target success: ${data[0] !== false}, donator success: ${data[0] !== false}`);
|
||||
console.error(`WARNING: Item donation failed: target success: ${data[0] !== false}, donator success: ${data[1] !== false}`);
|
||||
};
|
||||
await user.clearLock();
|
||||
await target.clearLock();
|
||||
10
bot/lib/changeBalance.ts
Normal file
10
bot/lib/changeBalance.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { updateUserRecord } from "../db/dbUser";
|
||||
import { type userRecord } from "../db/connection";
|
||||
import { User } from "../user";
|
||||
|
||||
export async function changeBalance(user: User, userRecord: userRecord, amount: number): Promise<false | userRecord> {
|
||||
userRecord.balance = userRecord.balance += amount;
|
||||
if (userRecord.balance < 0) return false;
|
||||
await updateUserRecord(user, userRecord);
|
||||
return userRecord;
|
||||
};
|
||||
Reference in New Issue
Block a user