Ask coding questions

← Back to all posts
I'm having some difficulty getting a discord bot to work
Andre1070

Firstly, in the client.login field, it doesn't recognize my token unless I actually type it into the file itself, it doesn't work if I try to put it as a secret.
Secondly, I'm having trouble inviting it to a server because it needs a redirect uri, but I don't know what I should use as a redirect uri? Would definitely appreciate some help

Voters
Andre1070
Comments
hotnewtop
cnnrde

I'm gonna go ahead and assume this is Discord.js based.

As for the token - are you using process.env["TOKEN"] with a secret named "TOKEN" in client.login?

And as for inviting the bot, open https://discord.com/api/oauth2/authorize?client_id=[CLIENTID]&permissions=8&scope=bot in a new tab, but replace [CLIENTID] with your bot's client id (which you can find in the OAuth2 section of the developer portal). This will allow you to invite the bot to a server, and it'll give the bot administrator priviledges. (note: you need the manage servers role in order to invite bots to a server)

Andre1070

@cnnrde How do I get it to recognize the token? I tried doing what you said, but it only gives me a token_invalid error, just like it was doing before.

cnnrde

@Andre1070 Does your client.login look like this: client.login(process.env['TOKEN'])? (assuming that your secret storing the token is called TOKEN

Andre1070

@cnnrde I tried changing it to that, but it didn't work...
Do I have to do something aside from creating a secret and putting it in the client.login field in order for it to work?

cnnrde

@Andre1070 What does your client.login look like, and what is the secret called?

Andre1070

@cnnrde Well, I tried lots of things. I named the secret 'soso_key' for reasons...
At first it was client.login('SOSO_KEY), then I changed it to something closer to what you suggested with client.login(process.env['SOSO_KEY']), and then changed the name of it to 'token' to see if maybe the name was the issue. Nothing worked so far. Only one that does is client.login('(the actual bot token)')

cnnrde

@Andre1070 if it's named soso_key in lowercase, you need to use client.login(process.env["soso_key"]), as secrets are case-sensitive.

Andre1070

@cnnrde No, it's named SOSO_KEY in uppercase, that was just an example.

cnnrde

@Andre1070 So your client.login looks like this: client.login(process.env["SOSO_KEY"])?

Andre1070

@cnnrde ...Weird, I definitely remember trying that one before.
I just copied and pasted that into the file and ran it. For whatever reason, it worked this time. Maybe there was some error happening with the way it was typed or something.

Okay, on to the next issue: The bot is currently running and logged into my server, but it is not responding to the command I programmed it to reply to ("!soso"). Is it usual for bots running on repl.it to take long to reply, or is there something else I need to fix?

cnnrde

@Andre1070 I think you may be using an old Discord.js tutorial. The latest version is v13, and your code looks to be using client.on('message', ...) which needs to be changed to client.on('messageCreate', ...) for Discord.js v13. Replit normally runs message events close to instantly. If you're looking for a Discord.js guide, I recommend this one, however be aware it does use some advanced features such as slash commands.

Andre1070

@cnnrde Sorry for replying so late. I'll look into this new guide you've recommended to me. Hopefully it works out, but I have any new questions, I'll be sure to ask them either here or on another programming forum.