Skip to content

Commit

Permalink
DraftBot-A-Discord-Adventure#69 Fix GiveBadge + added reset to GiveBadge
Browse files Browse the repository at this point in the history
  • Loading branch information
senetal committed Jun 27, 2020
1 parent de54b05 commit 9ab3991
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 72 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"moment": "^2.24.0",
"sequelize": "^5.21.7",
"sqlite": "^4.0.7",
"sqlite3": "^4.1.1"
"sqlite3": "^4.2.0"
}
}
18 changes: 15 additions & 3 deletions ressources/text/commands/giveBadgeCommand.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
"translations": {
"fr": {
"giveSuccess": "{pseudo}, vous avez donné un badge, j'espère que c'est mérité !",
"descGive": "Le badge {badge} a été donné à {player}"
"descGive": "Le badge {badge} a été donné à {player}",
"giveAlready": "{pseudo}, ce badge ne peut être assigné.",
"descGiveAlready": "{player} possède déjà le badge {badge} !",
"giveReset": "Les badges ont été reset.",
"descGiveReset":"{player} s'est vu retiré tous ses badges...",
"giveSyntaxErr":"{pseudo}, la syntaxe n'est pas bonne !",
"descGiveSyntaxErr":"Usage : `gb {badge | r} {joueur}`"
},
"en": {
"giveSuccess": "{pseudo}, you gave a badge, i hope it is well deserved !",
"descGive": "The badge {badge} has been given to {player}"
"giveSuccess": "{pseudo}, you gave a badge, I hope it is well deserved !",
"descGive": "The badge {badge} has been given to {player}",
"giveAlready": "{pseudo}, you can't give this badge.",
"descGiveAlready": "{player} already possessed {badge} !",
"giveReset": "Badges have been reset",
"descGiveReset":"{player} has lost all badges...",
"giveSyntaxErr":"{pseudo}, this is not the good syntax !",
"descGiveSyntaxErr":"Usage : `gb {badge | r} {player}`"
}
}
}
10 changes: 0 additions & 10 deletions ressources/text/commands/resetb.json

This file was deleted.

50 changes: 37 additions & 13 deletions src/commands/admin/GiveBadgeCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,48 @@
* @param {String[]} args=[] - Additional arguments sent with the command
*/
const giveBadgeCommand = async function (language, message, args) {
if ((await canPerformCommand(message, language, PERMISSION.ROLE.BADGEMANAGER)) !== true) {
return;
}
let embed = new discord.MessageEmbed();
// the author of the command is the author of the bot
let playerId = message.mentions.users.last().id;
[entity] = await Entities.getOrRegister(playerId);
if ((await canPerformCommand(message, language, PERMISSION.ROLE.BADGEMANAGER)) !== true) {
return;
}
let embed = new discord.MessageEmbed();
// the author of the command is the author of the bot
let playerId = message.mentions.users.last().id;
[entity] = await Entities.getOrRegister(playerId);

await entity.Player.giveBadge(args[0]);
await entity.Player.save();
let author;
let description;

if (args[1].length > 1) {
author = JsonReader.commands.giveBadgeCommand.getTranslation(language).giveSyntaxErr;
description = JsonReader.commands.giveBadgeCommand.getTranslation(language).descGiveSyntaxErr;

embed.setColor(JsonReader.bot.embed.default)
.setAuthor(format(JsonReader.commands.giveBadgeCommand.getTranslation(language).giveSuccess, { pseudo: message.author.username }), message.author.displayAvatarURL())
.setDescription(format(JsonReader.commands.giveBadgeCommand.getTranslation(language).descGive, { badge: args[0], player: message.mentions.users.last() }));
.setAuthor(format(author, { pseudo: message.author.username }), message.author.displayAvatarURL())
.setDescription(format(description, { badge: args[0], player: message.mentions.users.last() }));
return await message.channel.send(embed);
}

let hasAlreadyBadge = await entity.Player.giveBadge(args[0]);
await entity.Player.save();

if (hasAlreadyBadge) {
author = JsonReader.commands.giveBadgeCommand.getTranslation(language).giveAlready;
description = JsonReader.commands.giveBadgeCommand.getTranslation(language).descGiveAlready;
} else if (args[0] == global.ARGUMENTS.RESET) {
author = JsonReader.commands.giveBadgeCommand.getTranslation(language).giveReset
description = JsonReader.commands.giveBadgeCommand.getTranslation(language).descGiveReset;
} else {
author = JsonReader.commands.giveBadgeCommand.getTranslation(language).giveSuccess;
description = JsonReader.commands.giveBadgeCommand.getTranslation(language).descGiveSuccess
}

embed.setColor(JsonReader.bot.embed.default)
.setAuthor(format(author, { pseudo: message.author.username }), message.author.displayAvatarURL())
.setDescription(format(description, { badge: args[0], player: message.mentions.users.last() }));
return await message.channel.send(embed);
};

module.exports = {
'gb': giveBadgeCommand,
};
'gb': giveBadgeCommand,
};

35 changes: 0 additions & 35 deletions src/commands/admin/ResetBadge.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/core/Constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ global.ITEMTYPE = {
WEAPON: 'weapon',
ARMOR: 'armor',
OBJECT: 'object'
}

global.ARGUMENTS = {
RESET: 'reset'
}
26 changes: 16 additions & 10 deletions src/core/models/Players.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,34 @@ module.exports = (sequelize, DataTypes) => {

/**
* @param {("badgeID")} badgeID - The badgeID
* @returns {error} error - if the command is successfull or not
*/
Players.prototype.giveBadge = function(badgeID) {
if (this.badges != "") {
Players.prototype.giveBadge = function (badgeID) {
if (badgeID == global.ARGUMENTS.RESET) {
this.badges = "";
return false;
} else if (this.badges != "" && !this.badges.includes(badgeID)) {
this.badges = this.badges + "-" + badgeID;
}else{
return false;
} else if (!this.badges.includes(badgeID)){
this.badges = badgeID;
}
return false;
} else return true;
};

/**
* @param {("points")} points - A number representating the score
*/
Players.prototype.setPoints = function(points) {
Players.prototype.setPoints = function (points) {
this.score = points;
};
};

/**
* @param {("pointsWeek")} pointsWeek - A number representating the weekly score
*/
Players.prototype.setPointsWeek = function(points) {
Players.prototype.setPointsWeek = function (points) {
this.weeklyScore = points;
};
};

Players.beforeSave((instance, options) => {
instance.setDataValue('updatedAt', require('moment')().format('YYYY-MM-DD HH:mm:ss'));
Expand Down Expand Up @@ -182,8 +188,8 @@ module.exports = (sequelize, DataTypes) => {
* @param {Number} hours
*/
Players.prototype.fastForward = async function (hours) {
let moment =require('moment');
let lastReport = new moment(this.lastReportAt).subtract(hours,'h');
let moment = require('moment');
let lastReport = new moment(this.lastReportAt).subtract(hours, 'h');
this.lastReportAt = lastReport;
};

Expand Down

0 comments on commit 9ab3991

Please sign in to comment.