DM User based on the Username

Hi! I'm trying to use the discord rest API (https://discord.com/developers/docs/resources/user#get-user), but I can't seem to find anything that allows you to DM a user based on their username. This is my current work around so far but it's not working. First, it says that the discordId variable is not defined after I return it. Second, it says that the variable Discord is not defined. How do I fix this?
import { Routes } from "discord-api-types/v9";
import { getUser } from "$lib/supabase";
import { rest } from "$lib/discord/discordBot";

const clientId = import.meta.env.VITE_CLIENT_ID;

export async function POST({ request }) {
const body = await request.json();

//get user's discord username
const user = await getUser(body.userId);
const discordUsername = user.discord;

//get user's discord ID
const response = await fetch('https://discord.com/api/v10/users/' + discordUsername);
const json = await response.json();
const discordId = json.id;

//send the user the direct message
const recipient = await rest.put(
Routes.user(Discord.ClientUser.me(clientId)),
{
data: {
id: discordId,
type: 1,
},
}
);

await rest.post(Routes.channelMessages(recipient.id), {
data: {
content: body.message,
},
});
}
import { Routes } from "discord-api-types/v9";
import { getUser } from "$lib/supabase";
import { rest } from "$lib/discord/discordBot";

const clientId = import.meta.env.VITE_CLIENT_ID;

export async function POST({ request }) {
const body = await request.json();

//get user's discord username
const user = await getUser(body.userId);
const discordUsername = user.discord;

//get user's discord ID
const response = await fetch('https://discord.com/api/v10/users/' + discordUsername);
const json = await response.json();
const discordId = json.id;

//send the user the direct message
const recipient = await rest.put(
Routes.user(Discord.ClientUser.me(clientId)),
{
data: {
id: discordId,
type: 1,
},
}
);

await rest.post(Routes.channelMessages(recipient.id), {
data: {
content: body.message,
},
});
}
6 Replies
d.js toolkit
d.js toolkit8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
seasha
seasha8mo ago
node 18.16.1 "@discordjs/rest": "^2.0.1",
duck
duck8mo ago
there is no means of fetching or dming a user based on username you can search for a guild member by username with <Guild>.members.search() or the query option of <Guild>.members.fetch(), but it would definitely be easier and require fewer api calls if you just used user ids
seasha
seasha8mo ago
yeah i dont have access to the user's id, only their username
const response = await fetch('https://discord.com/api/v10/guilds/' + guildId + '/members/search/?query=' + discordUsername);
const json = await response.json();
const discordId = json.id;

console.log(discordId);
const response = await fetch('https://discord.com/api/v10/guilds/' + guildId + '/members/search/?query=' + discordUsername);
const json = await response.json();
const discordId = json.id;

console.log(discordId);
doing this also returns "undefined" for discordId currently im doing a two step process of getting their discord id from their username and then dming the id
duck
duck8mo ago
oh right this is frontend and you're not actually using the main lib 1. you're free to use the REST client to handle member fetching 2. you'll want to reference the discord docs directly to see what the /search endpoint responds with as it's not just a single member object 3. you'll probably also want to ask further questions in ddevs as this is more of a general api topic than how to use @discordjs/rest #useful-servers though I'm still not really sure why you couldn't just store user ids rather than usernames
seasha
seasha8mo ago
it doesn't make sens ein the context of the project ill ask in the dev server, thanks!