-3

I'm trying to write a Discord bot that contains several features. The main ones that are relevant to this question is a command that adds a number to a array, and one that removes a number from an array provided it exists. While I've already gotten a working "AddNumber" command completed, my main issue is in the "RemoveNumber" code. I'm using the arr.indexOf function to determine if it exists inside the array currently. However, whenever I attempt the function I get the error: UnhandledPromiseRejectionWarning: TypeError: arr.indexOf is not a function. While I have found several solutions online, and attempted most of them, none of them helped. How can I fix this? Alternatively, is there another way to check?

Relevant Code (More can be provided if needed!):

// Main File (Where the array is made, passed to needed files using async)
const arr = [
    "1",
];
// End main file
// Command File
if(arr.indexOf(gametoend) === -1){
    message.reply('that game doesn\'t exist! Did you put in the wrong number?');
} else {
    message.channel.send('Placeholder success');
}
// End command file

Update: This is the full error text that pops up. For the question I changed the array name from inprogressgames to arr, but that isn't the case here, hence the different name. Image link

Update 2: Put the code in a pastebin, hopefully it helps. Link

GTink911
  • 5
  • 6
  • 3
    upload the declaration of the array – ATP Jan 07 '21 at 19:19
  • 2
    You provided the wrong piece of code ^^ your error says `inprogressgames.indexOf`, while you provided `arr.indexOf`. Please show the piece of code where the error happens – Jannik Schmidtke Jan 07 '21 at 19:22
  • https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript – Maze Jan 07 '21 at 19:24
  • @JannikSchmidtke That was on purpose to simplify the given code, I just forgot to edit it in that section. Fixed. – GTink911 Jan 07 '21 at 20:23
  • @ATP Its just a blank array usually, but for testing I added a single number. Fixed – GTink911 Jan 07 '21 at 20:24
  • 1
    The code you provided doesn't produce that error. – A. R. Jan 07 '21 at 20:27
  • it works fine https://jsfiddle.net/ctvrs2wm/ – ATP Jan 07 '21 at 20:29
  • I can provide a screenshot of the issue if you want. Note this is running locally, as a Discord bot, and not through a browser. – GTink911 Jan 07 '21 at 20:33
  • 1
    We don't have much to go on without the full code but the error means that at some point `arr` isn't an `array` so the method `indexOf` won't work. Find out when `arr` isn't an array and you will solve the issue. – Cjmarkham Jan 07 '21 at 20:33
  • @CarlMarkham I've used arrays in several other places during my project, all which worked perfectly fine, and they were built the same way as this one. Additionally, I can use ```arr.push``` to send a number to the array, and it works perfectly fine. Definitely an array. – GTink911 Jan 07 '21 at 20:44
  • I can guarantee it's not an array when you're calling `indexOf`, and from a quick test, it looks like it may be an `integer` https://jsfiddle.net/dwvh9zmg/ But without the full code, I'm just guessing. – Cjmarkham Jan 07 '21 at 20:54
  • Did a test, came back as an array. Will toss all my code in a pastebin and link it in the original question. – GTink911 Jan 07 '21 at 20:56
  • Updated the post @CarlMarkham – GTink911 Jan 07 '21 at 21:08

1 Answers1

1

Looking at the code in the paste bin I can see that arr isn't an Array. This is because of how you are calling the execute command:

await command.execute(message, args, client, arr)

The signature of the execute command only takes 3 arguments:

async execute(message, args, arr) {

Because of this, arr is actually the discord client. Either remove the client from the list of arguments you specify when calling execute or add it to the list of parameters that execute requires.

You can verify this by console.loging arr just before you do the indexOf

enter image description here

There is another small issue I ran in to while debugging this and it's when you try to remove from the array later. Array.remove doesn't exist so I suggest using something like Array.slice

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
  • The client argument is essential in another part of my code - how would I improve this code so I can use more than 3 arguments? – GTink911 Jan 07 '21 at 21:45
  • Add it to the list of arguments that the execute command takes: `async execute(message, args, client, arr) {` – Cjmarkham Jan 07 '21 at 21:46