Skip to content

Commit

Permalink
Admin command "send Dm" #69
Browse files Browse the repository at this point in the history
  • Loading branch information
Mw3y committed Jun 14, 2020
1 parent c21929c commit a8654c1
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ressources/text/bot.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"reboot": "DraftBot",
"br": "############################################\n",
"dm": {
"supportAlert": ":love_letter: | {roleMention} **Nouveau message privé reçu !** {isBlacklisted}\n`Auteur: {username} (id : {id})`\n>>> ",
"supportAlert": ":love_letter: | {roleMention} **Nouveau message privé reçu !** {isBlacklisted}\n`Auteur: {username} (id : {id})`\n\n>>> ",
"blacklisted": "(**JOUEUR BLACKLISTÉ**)"
},
"guildJoin": {
Expand Down
4 changes: 2 additions & 2 deletions ressources/text/commands/changePrefix.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"translations": {
"fr": {
"error": "{pseudo}, hmmm... Cela n'a pas fonctionné !",
"descError": "Entrez un prefix après la commande svp",
"descError": "Entrez un préfixe après la commande svp",
"ok":"Le serveur {serverName} a désormais pour préfix : {newPrefix}"
},
"en": {
"error": "{pseudo}, hmmm... It didn't work!",
"descError": "Indiacte a prefix after the command please",
"descError": "Indicate a prefix after the command please",
"ok":"Server {serverName} has prefix : {newPrefix}"
}
}
Expand Down
18 changes: 18 additions & 0 deletions ressources/text/commands/sendPrivateMessage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"translations": {
"fr": {
"error": "{pseudo}, hmmm... Cela n'a pas fonctionné !",
"descError": "Entrez l'id du joueur ainsi qu'un message après la commande svp",
"title": ":white_check_mark: | DM envoyé à **{username}** :",
"ok":">>> ",
"signature": "\n\n- {username}"
},
"en": {
"error": "{pseudo}, hmmm... It didn't work!",
"descError": "Indiacte the user id and a message after the command please",
"title": ":white_check_mark: | DM sent to **{username}**:",
"ok":">>> ",
"signature": "\n\n- {username}"
}
}
}
53 changes: 53 additions & 0 deletions src/commands/admin/SendPrivateMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Allow an admin to change the prefix the bot use in a specific server
* @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 SendPrivateMessage = async function (language, message, args) {

if ((await canPerformCommand(message, language,
PERMISSION.ROLE.SUPPORT)) !== true) {
return;
}

var userId = args[0];
var messageToSend = args.join(' ').replace(userId, '') +
format(JsonReader.commands.sendPrivateMessage.getTranslation(language).signature, {
username: message.author.username
});

if (userId == undefined || args[1] == undefined)
return await sendErrorMessage(message, language);

const user = client.users.cache.get(userId);
let embed = new discord.MessageEmbed();
embed.setColor(JsonReader.bot.embed.default)
.setTitle(format(JsonReader.commands.sendPrivateMessage.getTranslation(language).title, {
username: user.username
}))
.setDescription(JsonReader.commands.sendPrivateMessage.getTranslation(language).ok + messageToSend)
.setImage(message.attachments.size > 0 ? [...message.attachments.values()][0].url : '');

user.send(messageToSend);
sendMessageAttachments(message, user);
return await message.channel.send(embed);
};

/**
* Send the error message for this command
* @param {module:"discord.js".Message} message - Message from the discord server
*/
async function sendErrorMessage(message, language) {
return await message.channel.send(new discord.MessageEmbed().setColor(JsonReader.bot.embed.error)
.setAuthor(format(JsonReader.commands.sendPrivateMessage.getTranslation(language).error, {
pseudo: message.author.username
}), message.author.displayAvatarURL())
.setDescription(JsonReader.commands.sendPrivateMessage.getTranslation(language).descError));

}

module.exports = {
'dm': SendPrivateMessage,
};
21 changes: 3 additions & 18 deletions src/core/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,10 @@ class Command {
id: message.author.id,
isBlacklisted: isBlacklisted ? JsonReader.bot.dm.blacklisted : ''
});


channel.send(message.author.id).catch(JsonReader.bot.getTranslation(language).noSpeakPermission);
channel.send(sentence + message.content).catch(JsonReader.bot.getTranslation(language).noSpeakPermission);
if (message.attachments.size > 0) await Command.sendMessageAttachments(message, channel);
}

/**
* Send all attachments from a message to a discord channel
* @param {module:"discord.js".Message} message - Message from the discord user
* @param {module:"discord.js".TextChannel} channel - The channel where all attachments will be sent
*/
static async sendMessageAttachments(message, channel) {
message.attachments.forEach(element => {
channel.send({
files: [{
attachment: element.url,
name: element.filename
}]
});
});
if (message.attachments.size > 0) await sendMessageAttachments(message, channel);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/core/Tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ global.idToMention = (id) => {
return '<@&' + id + '>';
}

/**
* Send all attachments from a message to a discord channel
* @param {module:"discord.js".Message} message - Message from the discord user
* @param {module:"discord.js".TextChannel} channel - The channel where all attachments will be sent
*/
global.sendMessageAttachments = (message, channel) => {
message.attachments.forEach(element => {
channel.send({
files: [{
attachment: element.url,
name: element.filename
}]
});
});
}

/**
* Generate a random rarity. Legendary is very rare and common is not rare at all
* @returns {Number}
Expand Down

0 comments on commit a8654c1

Please sign in to comment.