How do I send random messages? - discord

(Now... yes, I know that the title might be nonsense.)
So let's say I have 6 embeds. You know, like... a game die.
I want to send one of these embeds once I run the command $roll.
Here's how I coded it:
message.reply(`${ one || two || three || four || five || six }`);
However, every time I run the command, it only says One... Here's an image.
Help me with this, please.

When you use one || two || three || four || five || six this, It is always one because one is always true.
So if you wanna send a random value, try this code
const choices = [one, two, three, four, five, six]
message.reply(choices[Math.floor(Math.random() * choices.length)])

function randomnumb() {
var rand = [
"one",
"two",
"three",
"four",
"five",
"six"
];
return rand[Math.floor(Math.random() * rand.length)];
}
message.channel.send(randomnumb());

Related

UITextChecker code not giving correct result

I am using the standard method I think to check a word for validity. I have a 4 part array that is joined to make the word being verified.
I have this placed in my touches began func in a SpriteKit scene however it always gives me the answer "CORRECT SPELLING" even when it is not.
Curiously when I put the exact same bit of code into my did move to view func so it runs when the game starts it works perfectly well. The code is exactly the same except instead of the joined array I just create a let word = "WORD" before the code. So I am guessing the problem must be something to do with my joined array??
This is my code
let word = wordArray.joined()
let textChecker = UITextChecker()
let misspelledRange = textChecker.rangeOfMisspelledWord(
in: word, range: NSRange(0..<word.utf16.count), startingAt: 0, wrap: false, language: "en_US")
if misspelledRange.location != NSNotFound,
let guesses = textChecker.guesses(
forWordRange: misspelledRange, in: word, language: "en_US")
{
print("Guess: \(String(describing: guesses.first))") //Output is: Guess: Optional("Tester")
} else {
print("\(word) CORRECT SPELLING")
}
Turned out my issue was that UITextChecker does not like full caps!
Only accepts lower case spellings

How can I tie independent values to strings in an array (Swift)?

So I'm practicing coding and I've set up an array of strings. What I want to happen is to have a function randomly select a string and if a certain string is selected, it would print out one message, and if any of the other strings are selected, it would print out a different one. I think setting up the messages could be done w/an if-else function, but I'm stuck on the earlier step of figuring out how to identify which string is called.
I did some searching and found this thread Get Random String from an Array and I copied the randomization code from it. I'm a bit overwhelmed with where to go from there.
What type of function/identifier etc would I need to set up so that the strings are uniquely identified?
class ViewController: UIViewController {
// Make collection of games and if Monsters is called, print "Take a break." If a diff string is called, print "Do your homework."
#IBAction func random(_ sender: UIButton) {
let games = ["Cards Against Humanity", "Mario Kart", "Halo", "Pixeljunk Monsters"]
let randomNumber = Int(arc4random_uniform(UInt32(games.count)))
_ = games[randomNumber]
}
} // end of class
Your expectation is not very clear from your question, but I guess you are beginner and need a help with processing randomly selected string.
#IBAction func random(_ sender: UIButton) {
let games = ["Cards Against Humanity", "Mario Kart", "Halo", "Pixeljunk Monsters"]
let randomNumber = Int(arc4random_uniform(UInt32(games.count)))
let randomlyPickedString = games[randomNumber]
if randomlyPickedString == "Cards Against Humanity" {
print("Print anything you want about Cards Against Humanity")
}
else if randomlyPickedString == "Mario Kart" {
print("Print anything you want about Mario Kart")
}
else if randomlyPickedString == "Halo" {
print("Print anything you want about Halo")
}
else {
print("Print anything you want about Pixeljunk Monsters")
}
}
If its a finite set of values in array, you can even decide to use switch

Randomly grab string from array, use as placeholder text, and equate to answer string in swift

I have two UITextFields that get the placeHolder.text randomly from an array. The part I am stuck on, is having an element in an array of answers, correspond to the placeHolder text.
Meaning: placeHolder.text is a question, user text input is the answer, and we check that the user answer is the same as the hard coded value.
sample code:
let questionArray = ["name of your cat?", "name of your other cat?"]
let questionArray2 = { more stupid questions ]
let answerArray = ["cat damen", "diabetes"]
let answerArray2 = [ more stupid answers ]
var answerContainer: String()
var answerContainer2: String()
uitextField1.placeHolder = questionArray.randomElement()
uitextField2.placeHolder = questionArray2.randomElement()
Heres where I get stupid :
func answerGenerator {
if UItextField1.placeHolder == questionArray[0] {
answerContainer = answerArray[0]
}
}
func submit(_ sender: UIButton) {
if uitextField.text == answerContainer && uitextField2.text == answerContainer2 {
do good stuff
}
}
I tried using a dictionary, but got stuck. I also thought about an Enum / Switch Case, but also got stuck as I have two textFields that need the placeHolder generated randomly.
so what happens is the actual answer that's supposed to be allocated to its question, maybe it happens, but I'm not grabbing it correctly to compare what the user will type in the textfield.
Its so easy, but I'm overthinking this.

Replacing variables names dynamically like string parts

I'd like to optimise my code using a single function to process multiple possibilities depending on the situation: here it is different currencies.
I know how to do it when I use strings with the \(something) syntax and I'd like something similar for variables.
Here is my current situation for USD:
if WalletViewController.currencyUSD == true {
if CurrentDifferenceFunctions.buyOrSellBitcoinUSD == 1 {
if let lastBitcoinBuyPrice = UserDefaults.standard.object(forKey: "lastBitcoinBuyPrice\(currentCurrency)") as? Double {
CurrentDifferenceFunctions.savedBitcoinPrice = lastBitcoinBuyPrice
}
} else if CurrentDifferenceFunctions.buyOrSellBitcoinUSD == 2 {
if let lastBitcoinSellPrice = UserDefaults.standard.object(forKey: "lastBitcoinSellPrice\(currentCurrency)") as? Double {
CurrentDifferenceFunctions.savedBitcoinPrice = lastBitcoinSellPrice
}
}
}
How can I replace USD everytime it appears by another currency when needed and then be able to use the same function for different currencies? Would using arrays help here?
I have a few dozens cryptos and as many currencies to process, any help would be gold!
PS: I'm sorry about the redundant WalletViewController.currencyUSD == true but I like to keep my work literal until I fully understand the logic.

AS3 Display random word from array onto stage

I have an array of words in my code. What I'm hoping to do is display, at random, one word from the array onto the stage. How is this achievable?
For this you can use Math.random().
Returns a pseudo-random number n, where 0 <= n < 1.
function getRandomWord(array:Array):String
{
var wordIndex:int=Math.floor(Math.random() * array.length);
return array[wordIndex:int];
}
This function can be used to dynamically set the value of the text box on the stage:
myTextField_txt.text = getRandomWord(wordArray);
You can read more about Math.Random() here in the documentation:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Math.html#random()
var myWords: Array = ["DOG", "CAT", "RABBIT", "HORSE", "COW"]
var randomNumber: int = (Math.Random() * myWords.length);
stage.addEventListener(MouseEvent.CLICK, getRandom);
function getRandom(e: MouseEvent) {
myTextField.text = myWords[randomNumber].toString();
randomNumber = (Math.Random() * myWords.length);
}
You'll need a dynamic text field with the instance name of "myTextField".
Also, in the properties of the text field embed the font to ensure it doesn't cause any issues.
Then add this code to the frame.
Ctrl + Enter to test the movie. Then click anywhere on the stage.
Good luck!

Resources