Commit f267f20f authored by George Peter Banyard's avatar George Peter Banyard

Implemented give (tickets) command.

parent 2dfb3ec2
......@@ -274,6 +274,46 @@ DiscordBotClient.on('message', async message =>
}
}
case 'give':
if (message.mentions.members.size === 0) {
return {message:i18n.text.RPG_SPECIFY_USER_TO_GIVE};
} else if(Number(arguments[2]) <= 0) {
return {message:i18n.text.RPG_AMOUNT_TO_GIVE};
} else {
let receiver = message.mentions.members.first();
let amount = Number(arguments[2]);
try {
let giveData = await rpg.give(member.id, receiver.id, amount);
if (giveData === true) {
return {message:{embed:i18n.RPG_MONEY(member.displayName, receiver.displayName, amount)}};
} else {
return {message:i18n.text.DEFAULT_ERROR};
}
} catch (errorCode) {
switch (errorCode) {
case rpgConst.CODE_MEMBER_NOT_REGISTERED:
return {message:i18n.text.RPG_MEMBER_NOT_REGISTERED
.replace('{member}', receiver.displayName)};
case rpgConst.CODE_SELF_NOT_REGISTERED:
return {message:i18n.text.RPG_SELF_NOT_REGISTERED};
case rpgConst.CODE_SELF_GIVE:
return {message:i18n.text.RPG_SELF_GIVE};
case rpgConst.CODE_AMOUNT_UNAVAILABLE:
return {message:i18n.text.RPG_AMOUNT_UNAVAILABLE};
case rpgConst.CODE_DB_ERROR:
return {message:i18n.text.DB_ERROR};
default:
return {message:i18n.text.DEFAULT_ERROR};
}
}
}
return;
/** Default : Invalid command message */
default:
return {message:i18n.text.INVALID_COMMAND
......
......@@ -23,6 +23,10 @@ const text = Object.freeze({
RPG_MEMBER_ALREADY_DEAD: '{member} est déjà mort.',
RPG_SELF_MAX_HEALTH: 'Vous n\'avez pas besoin d\'être soigné.',
RPG_MEMBER_MAX_HEALTH: '{member} n\'a pas besoin d\'être soigné.',
RPG_SPECIFY_USER_TO_GIVE: 'Veuillez spécifier le joueur à qui vous voulez donner de l\'argent.',
RPG_AMOUNT_TO_GIVE: 'Veuillez indiquer la somme que vous voulez donner.',
RPG_SELF_GIVE: 'Vous ne pouvez pas vous donner vous même de l\'argent.',
RPG_AMOUNT_UNAVAILABLE: 'Vous ne pouvez pas donner plus d\'argent que vous n\'avez.',
});
const AGE_USER = (member, joinedAt) => {
......@@ -133,6 +137,12 @@ const RPG_HEAL_MEMBER_EMBED = (caster, receiver, healData) => {
.setFooter(`Il vous reste ${healData.energy} points d'énergie.`)
};
const RPG_MONEY_EMBED = (giver, receiver, amount) => {
return new Discord.RichEmbed()
.setTitle('Donation')
.addField(`${giver} a donné ${amount} :tickets: à ${receiver}.`, '\u200B')
};
/**
* Returns the placeholder where {range} get's changed into Y years MM months and DD days.
* {calendarDate} is replaced by the date's calendar date DD/MM/Y
......@@ -203,4 +213,5 @@ module.exports.RPG_ATTACK = RPG_ATTACK_EMBED;
module.exports.RPG_ATTACK_KILL = RPG_ATTACK_KILL_EMBED;
module.exports.RPG_HEAL_SELF = RPG_HEAL_SELF_EMBED;
module.exports.RPG_HEAL_MEMBER = RPG_HEAL_MEMBER_EMBED;
module.exports.RPG_MONEY = RPG_MONEY_EMBED;
module.exports.getRelativeTimeFromNow = getRelativeTimeFromNow;
......@@ -20,6 +20,8 @@ module.exports = Object.freeze({
CODE_DEFENDER_ALREADY_DEAD: 212,
CODE_CASTER_ALREADY_MAX_HEALTH: 221,
CODE_RECEIVER_ALREADY_MAX_HEALTH: 222,
CODE_SELF_GIVE: 231,
CODE_AMOUNT_UNAVAILABLE: 232,
CODE_DB_ERROR: 900,
CODE_TABLE_NOT_EXITING: 910,
});
\ No newline at end of file
......@@ -280,10 +280,59 @@ const heal = (casterId, receiverId) => {
});
};
const give = (giverId, receiverId, amount) => {
return new Promise((resolve, reject) => {
if (giverId === receiverId) {
reject(rpg.CODE_SELF_GIVE);
} else {
sqlite.get('SELECT money FROM player WHERE userId = $giverId',
{ $giverId:String(giverId) }
).then(data => {
if (data === undefined) {
reject(rpg.CODE_SELF_NOT_REGISTERED);
} else if (data['money'] < amount) {
reject(rpg.CODE_AMOUNT_UNAVAILABLE);
} else {
sqlite.get('SELECT money FROM player WHERE userId = $receiverId',
{ $receiverId:String(receiverId) }
).then(receiverData => {
if (receiverData === undefined) {
reject(rpg.CODE_MEMBER_NOT_REGISTERED);
} else {
sqlite.run(
'UPDATE player SET money = money-$amount WHERE userId = $giverId',
{ $amount:amount,$giverId:String(giverId) }
).catch((e) => {
console.error(e);
reject(rpg.CODE_DB_ERROR);
});
sqlite.run(
'UPDATE player SET money = money+$amount WHERE userId = $receiverId',
{ $amount:amount,$receiverId:String(receiverId) }
).catch((e) => {
console.error(e);
reject(rpg.CODE_DB_ERROR);
});
resolve(true);
}
}).catch((e) => {
console.error(e);
reject(rpg.CODE_DB_ERROR);
});
}
}).catch((e) => {
console.error(e);
reject(rpg.CODE_DB_ERROR);
});
}
});
};
module.exports.register = register;
module.exports.getProfil = getProfil;
module.exports.getNbPlayers = getNbPlayers;
module.exports.getList = getList;
module.exports.daily = daily;
module.exports.attack = attack;
module.exports.heal = heal;
\ No newline at end of file
module.exports.heal = heal;
module.exports.give = give;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment