Oauth2 bot reply

Im stuck if somebody can help: I got the info from the Oauth2, but now im lost as to how to send back a dm to the user
fetch('https://discordapp.com/api/v6/users/@me', {
method: 'GET',
headers: { 'Authorization': `Bearer ${json.access_token}` }
}).then(response => response.json()).then(resp => {MakeappThread(resp.id)})
fetch('https://discordapp.com/api/v6/users/@me', {
method: 'GET',
headers: { 'Authorization': `Bearer ${json.access_token}` }
}).then(response => response.json()).then(resp => {MakeappThread(resp.id)})
the above is fine, im getting the info. this the function that supposed to put the bot instance on
function MakeappThread(UID, servers){

client.on('ready', async () => {

const user = client.users.cache.get(UID);
user.send("HELLO")

});
}
function MakeappThread(UID, servers){

client.on('ready', async () => {

const user = client.users.cache.get(UID);
user.send("HELLO")

});
}
the function is firing, the client doesnt go on
7 Replies
monbrey
monbrey3y ago
I mean, OAuth2 and a bot are really completely different things What client? Where is it logged in? What makes it ready?
Mulo
MuloOP3y ago
whenever I use client.whatever, Ineed to be inside a
client.on(...
client.on(...
in this file I have many listeners
client.on('ready', async interaction => { .. })
client.on('interactionCreate', async interaction => { .. })
client.on('messageCreate', async (msg) => {.. })
etc..
client.on('ready', async interaction => { .. })
client.on('interactionCreate', async interaction => { .. })
client.on('messageCreate', async (msg) => {.. })
etc..
all the client.user, cache methods have to be used inside those client.on main function listeners in this same file i have the backend express routes that handle the auth2
app.get('/apps/application', async (req, res) => {
...
fetch('https://discordapp.com/api/v6/users/@me', {
method: 'GET',
headers: { 'Authorization': `Bearer ${json.access_token}` }
}).then(response => response.json()).then(resp => console.log(resp.id))
}
app.get('/apps/application', async (req, res) => {
...
fetch('https://discordapp.com/api/v6/users/@me', {
method: 'GET',
headers: { 'Authorization': `Bearer ${json.access_token}` }
}).then(response => response.json()).then(resp => console.log(resp.id))
}
i cannot use client.user or whatever inside an express endpoint, unless inside a .then() i make a new instance of the bot online, but that sounds weird the bot logins at the bottom of this file `client.login(token) I know Oauth2 and bot are 2 different things (that is why i put the backend and the bot in the same file) @yinoguns which scope do you mean
Yinoguns
Yinoguns3y ago
JavaScript scope
Mulo
MuloOP3y ago
anywhere in the module?
Yinoguns
Yinoguns3y ago
Basic JS. If the client is in scope, then it is usable. Events are mearly the expected cause of needing to envoke the client in some way.
monbrey
monbrey3y ago
all the client.user, cache methods have to be used inside those client.on main function listeners
This is technically true but that doesn't mean your ready event is going to fire again just because an OAuth2 endpoint called a function Assuming you do have a client in scope, it's probably already fired ready at this point
function MakeappThread(UID, servers){
if(client.isReady()) {
// send message
}
}
function MakeappThread(UID, servers){
if(client.isReady()) {
// send message
}
}
you would just need to check that its ready, then use it
Mulo
MuloOP3y ago
holy crap is working i thought i needed to be insice a client scope thanks guys
Want results from more Discord servers?
Add your server