Skip to content

Commit

Permalink
gcreate command #62
Browse files Browse the repository at this point in the history
  • Loading branch information
BastLast committed Jun 17, 2020
1 parent 0fc5af3 commit 684f281
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 278 deletions.
36 changes: 36 additions & 0 deletions ressources/text/commands/guildCreate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"translations": {
"fr": {
"errorTitle": "{pseudo}, hmmm... Cela n'a pas fonctionné !",
"buyTitle": "{pseudo}, confirmation :",
"alreadyInAGuildException": "Vous appartenez déjà à une guilde.",
"noNameProvided": "Indiquez un nom pour votre guilde : `guildCreate VotreNomDeGuilde`.",
"invalidName": "Les règles pour le nom de la guilde sont les suivantes:\n- Utiliser au moins une lettre\n- Ne pas mettre plus de 2 espaces consécutifs\n- Les caractères spéciaux ne sont pas autorisés\n- Entre {min} et {max} caractères",
"nameAlreadyUsed" : "Le nom de guilde que vous avez choisi est déjà utilisé.",
"creationCancelled" : "La création de votre guilde a été annulée.",
"buyConfirm" : "Confirmer la création de la guilde **{guildName}** pour `{price}` :moneybag: ?",
"buyFooter" : "Attention, le staff se réserve le droit de renommer votre guilde.",
"notEnoughMoney":"Vous n'avez pas assez d'argent pour effectuer cet achat.",
"createTitle": "{pseudo}, félicitations !",
"createSuccess" : "La guilde **{guildName}** a été créée !"
},
"en": {
"errorTitle": "{pseudo}, hmmm... That didn't work!",
"buyTitle": "{pseudo}, confirmation :",
"alreadyInAGuildException": "You are already in a guild.",
"noNameProvided": "Please provide a name for your guild : `guildCreate YourGuildName`.",
"invalidName": "The rules for the guild's name are the followings:\n- Use at least one letter\n- Two spaces or more aren't allowed\n- Special characters aren't allowed\n- Between {min} and {max} characters",
"nameAlreadyUsed" : "The guild name you chose is already being used.",
"creationCancelled" : "The creation of your guild has been cancelled.",
"buyConfirm" : "Confirm that you want to create a guild named **{guildName}** for `{price}` :moneybag:?",
"buyFooter" :"Warning, the bot's staff keeps control on guild's names and may change it.",
"notEnoughMoney":"You don't have enough money to buy this.",
"createTitle": "{pseudo}, congratulation!",
"createSuccess" : "The guild **{guildName}** has been sucessfuly created."

}
},
"icon": "https://i.imgur.com/sQHBly4.png",
"guildCreationPrice" : 5000
}

156 changes: 156 additions & 0 deletions src/commands/guild/GuildCreateCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
const { Guild } = require("discord.js");

/**
* Allow to Create a guild
* @param {("fr"|"en")} language - Language to use in the response
* @param {module:"discord.js".Message} message - Message from the discord server
* @param {String[]} args=[] - Additional arguments sent with the command
*/
const GuildCreateCommand = async (language, message, args) => {

let entity, guild;

try {
entity = await Entities.getByArgs(args, message);
} catch (error) {
[entity] = await Entities.getOrRegister(message.author.id);
}

// search for a user's guild
try {
guild = await Guilds.getById(entity.Player.guild_id);
} catch (error) {
guild = null;
}

let embed = new discord.MessageEmbed();

if (guild !== null) { // already in a guild
embed.setColor(JsonReader.bot.embed.error)
.setAuthor(format(JsonReader.commands.guildCreate.getTranslation(language).errorTitle, {
pseudo: message.author.username
}), message.author.displayAvatarURL())
.setDescription(JsonReader.commands.guildCreate.getTranslation(language).alreadyInAGuildException);
return message.channel.send(embed);
}

let askedName = args.join(" ");

if (askedName.length < 1) { // no name provided
embed.setColor(JsonReader.bot.embed.error)
.setAuthor(format(JsonReader.commands.guildCreate.getTranslation(language).errorTitle, {
pseudo: message.author.username
}), message.author.displayAvatarURL())
.setDescription(JsonReader.commands.guildCreate.getTranslation(language).noNameProvided);
return message.channel.send(embed);
}

const regexAllowed = RegExp(/^[A-Za-z0-9 ÇçÜüÉéÂâÄäÀàÊêËëÈèÏïÎîÔôÖöÛû]+$/);
const regexSpecialCases = RegExp(/^[0-9 ]+$|( {2})+/);
if (!(regexAllowed.test(askedName) && !regexSpecialCases.test(askedName) && askedName.length >= GUILD.MIN_GUILDNAME_SIZE && askedName.length <= GUILD.MAX_GUILDNAME_SIZE)) {
embed.setColor(JsonReader.bot.embed.error)
.setAuthor(format(JsonReader.commands.guildCreate.getTranslation(language).errorTitle, {
pseudo: message.author.username
}), message.author.displayAvatarURL())
.setDescription(format(JsonReader.commands.guildCreate.getTranslation(language).invalidName, {
min: GUILD.MIN_GUILDNAME_SIZE,
max: GUILD.MAX_GUILDNAME_SIZE
}));
return message.channel.send(embed);
}

try {
guild = await Guilds.getByName(args.join(" "));
} catch (error) {
guild = null;
}

if (guild !== null) { // the name is already used
embed.setColor(JsonReader.bot.embed.error)
.setAuthor(format(JsonReader.commands.guildCreate.getTranslation(language).errorTitle, {
pseudo: message.author.username
}), message.author.displayAvatarURL())
.setDescription(JsonReader.commands.guildCreate.getTranslation(language).nameAlreadyUsed);
return message.channel.send(embed);
}

embed.setAuthor(format(JsonReader.commands.guildCreate.getTranslation(language).buyTitle, {
pseudo: message.author.username
}), message.author.displayAvatarURL());
embed.setDescription(format(JsonReader.commands.guildCreate.getTranslation(language).buyConfirm, {
guildName: askedName,
price: JsonReader.commands.guildCreate.guildCreationPrice,
}));
embed.setFooter(JsonReader.commands.guildCreate.getTranslation(language).buyFooter, null);

let msg = await message.channel.send(embed);
embed = new discord.MessageEmbed();
const filterConfirm = (reaction, user) => {
return ((reaction.emoji.name == MENU_REACTION.ACCEPT || reaction.emoji.name == MENU_REACTION.DENY) && user.id === message.author.id);
};

const collector = msg.createReactionCollector(filterConfirm, {
time: 120000,
max: 1
});

collector.on('end', async (reaction) => {

if (reaction.first()) { // a reaction exist
if (reaction.first().emoji.name == MENU_REACTION.ACCEPT) {

if (entity.Player.money > JsonReader.commands.guildCreate.guildCreationPrice) {
embed.setColor(JsonReader.bot.embed.error)
.setAuthor(format(JsonReader.commands.guildCreate.getTranslation(language).errorTitle, {
pseudo: message.author.username
}), message.author.displayAvatarURL())
.setDescription(JsonReader.commands.guildCreate.getTranslation(language).notEnoughMoney);
return message.channel.send(embed);
}

const newGuild = await Guilds.create({
name: askedName,
chief_id: entity.id
});

entity.Player.guild_id = newGuild.id;
entity.Player.addMoney(-JsonReader.commands.guildCreate.guildCreationPrice);

await Promise.all([
entity.save(),
entity.Player.save()
]);

embed.setAuthor(format(JsonReader.commands.guildCreate.getTranslation(language).createTitle, {
pseudo: message.author.username
}), message.author.displayAvatarURL());
embed.setDescription(format(JsonReader.commands.guildCreate.getTranslation(language).createSuccess, {
guildName: askedName
}));
return message.channel.send(embed);
}
}

//Cancel the creation
embed.setColor(JsonReader.bot.embed.error)
.setAuthor(format(JsonReader.commands.guildCreate.getTranslation(language).errorTitle, {
pseudo: message.author.username
}), message.author.displayAvatarURL())
.setDescription(JsonReader.commands.guildCreate.getTranslation(language).creationCancelled);
message.channel.send(embed);

});

await msg.react(MENU_REACTION.ACCEPT);
await msg.react(MENU_REACTION.DENY);

};


module.exports = {
"guildCreate": GuildCreateCommand,
"gCreate": GuildCreateCommand,
"gc": GuildCreateCommand
};


Loading

0 comments on commit 684f281

Please sign in to comment.