After checking TheAaron's feedback, here is a revised version of my script, it can still be improved but it's now way more accepting when it comes to entering arguments. It also allows deck and card names with spaces in them. // Main Wrapper
on("ready", function(){
let cardBankName = "CardBank"; // Change this to the name of the card bank
let IsDebug = 0; // Enable DEBUG messages
// Listen to the chat and respond to commands
on("chat:message", function(msg){
// Listen only to API calls with the correct prefix
if(msg.type == "api" && /^!deckmaker/i.test(msg.content)) {
// Stores the message without the api prefix
let command = msg.content.replace(/!deckmaker/i, "");
let commandWords = command.split(/--/);
// Removes '--' from arguments and filter out whitespace only arguments
commandWords.forEach(element => element.split("--").pop())
commandWords = commandWords.filter(function(entry) { return /\S/.test(entry); });
if (IsDebug===1){sendChat("DEBUG", "/w GM Command words : "+commandWords.length);}
// example : !Deckmaker addCards DeckName 2 Ghost 2 Mario 1 Sirion 3 Prion
if (/addcards/i.test(commandWords[0])){
let toDeckName = commandWords[1].trim();
// Checks if the deck exists
if (findObjs({type: 'deck',name: toDeckName})[0] !== undefined)
{
sendChat("DeckMaker", "/w GM Deckmaker will now try to add cards");
// Split arguments on pipe character
let cardToCopyList = commandWords[2].split("|");
let index;
for (index = 0; index < cardToCopyList.length; index++){
// Gets the amount and cardname separated by cutting the string in two pieces. Trim in order to remove any extra spaces
let amount = cardToCopyList[index].trim().split(/\s(.+)/)[0].trim();
let cardName = cardToCopyList[index].trim().split(/\s(.+)/)[1].trim();
// Try to add the card
try {
AddSingleCard(cardName, toDeckName, Number(amount));
}
catch(error) {
sendChat("DeckMaker", "/w GM Couldn't find the card '"+cardName+"' in the card bank.");
}
}
sendChat("DeckMaker", "/w GM Completed.");
} else {
sendChat("DeckMaker", "/w GM No deck with the name '"+toDeckName+"' was found.");
}
}
// Very rough help
if (/help/i.test(commandWords[0])){
sendChat("DeckMaker", "/w GM Syntax : !Deckmaker --AddCards --DeckName --Number1 CardName1|Number2 CardName2|Number3... ");
}
}
});
function AddSingleCard(cardName, toDeckName, amount = 1){
// Find the id of the first deck with the name "toDeckName"
let toDeckId = findObjs({
type: 'deck',
name: toDeckName
})[0].id;
// Find the id of the card bank.
let fromDeckId = findObjs({
type: 'deck',
name: cardBankName
})[0].id;
CopyCardToDeck(toDeckId, fromDeckId, cardName, amount);
}
/// Makes copies of a card with the name "cardName" within the deck with the id "fromDeckId"
/// and adds an amount of them to the deck with the id "toDeckId"
function CopyCardToDeck(toDeckId, fromDeckId, cardName, amount = 1){
// Attempts to find the card.
let cardToCopy = findObjs({
type: 'card',
deckid: fromDeckId,
name: cardName
})[0];
if (IsDebug == 1){sendChat("DeckMaker", "/w GM DEBUG : " + amount + " " + cardName);}
// Create copies
let i;
for (i = 1; i < amount+1; i++)
{
// I think this is an okay way to copy stuff ?
createObj('card',{
// Names card like "BlueEyes (1)", "BlueEyes (2)" etc.
// Change naming convention here
name: cardToCopy.get('name') + "(" + i + ")",
deckid: toDeckId,
avatar: cardToCopy.get('avatar')
});
}
// Returns 1 if the function didn't blow up.
return 1;
}
sendChat("DeckMaker", "/w GM DeckMaker is ready !");
});