seasha
seasha
DIAdiscord.js - Imagine an app
Created by seasha on 11/12/2023 in #djs-questions
Trouble pushing to heroku
I'm trying to deploy my discordjs application to heroku, but i keep getting this error which I don't know how to fix: version discordjs - 14.13.0 nodejs - 18.16.0 Based on the error, it seems like a discordjs sorta issue so guidance would be appreciated thank you
2023-11-12T19:04:08.069023+00:00 app[worker.1]: Error: Cannot find module './timers'
2023-11-12T19:04:08.069023+00:00 app[worker.1]: Require stack:/app/node_modules/discord.js/src/index.js
2023-11-12T19:04:08.069025+00:00 app[worker.1]: - /app/main.js
2023-11-12T19:04:08.069025+00:00 app[worker.1]: at
2023-11-12T19:04:08.069023+00:00 app[worker.1]: Error: Cannot find module './timers'
2023-11-12T19:04:08.069023+00:00 app[worker.1]: Require stack:/app/node_modules/discord.js/src/index.js
2023-11-12T19:04:08.069025+00:00 app[worker.1]: - /app/main.js
2023-11-12T19:04:08.069025+00:00 app[worker.1]: at
3 replies
DIAdiscord.js - Imagine an app
Created by seasha on 10/14/2023 in #djs-questions
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,
},
});
}
14 replies
DIAdiscord.js - Imagine an app
Created by seasha on 10/14/2023 in #djs-questions
Zlib-sync failed to resolve
Hi! So I'm trying to integrate discordjs into my Svelte application because I want the bot to send DMs to users on a button press, but I don't want it to actually have any commands. So I created this discordBot.js file as shown and then imported the sendDM() function on one of the pages:
import { getUser } from "$lib/supabase";
import { Client, GatewayIntentBits } from 'discord.js';

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
client.login(import.meta.env.VITE_BOT_TOKEN);

export async function sendDirectDM(discordId, message) {
const user = await client.users.fetch(discordId);
await user.send(message);
}

export async function getUserIdFromUsername(username) {
const user = await client.users.fetch({ username });
return user.id;
}

export async function sendDM(userId, message) {
const user = await getUser(userId);
const discordId = await getUserIdFromUsername(user.discord);
await sendDirectDM(discordId, message);
}
import { getUser } from "$lib/supabase";
import { Client, GatewayIntentBits } from 'discord.js';

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
client.login(import.meta.env.VITE_BOT_TOKEN);

export async function sendDirectDM(discordId, message) {
const user = await client.users.fetch(discordId);
await user.send(message);
}

export async function getUserIdFromUsername(username) {
const user = await client.users.fetch({ username });
return user.id;
}

export async function sendDM(userId, message) {
const user = await getUser(userId);
const discordId = await getUserIdFromUsername(user.discord);
await sendDirectDM(discordId, message);
}
But when I open the page on the localhost which calls one of these functions, it gives me the following error:
Failed to resolve import "zlib-sync" from "node_modules/.vite/deps/discord__js.js?v=f1677f66". Does the file exist?
10:35:55 AM [vite] Internal server error: Failed to resolve import "zlib-sync" from "node_modules/.vite/deps/discord__js.js?v=f1677f66". Does the file exist?
Plugin: vite:import-analysis
File: /Users/x/x/node_modules/.vite/deps/discord__js.js?v=f1677f66:50929:65
50927| }
50928| __name(getInitialSendRateLimitState, "getInitialSendRateLimitState");
Failed to resolve import "zlib-sync" from "node_modules/.vite/deps/discord__js.js?v=f1677f66". Does the file exist?
10:35:55 AM [vite] Internal server error: Failed to resolve import "zlib-sync" from "node_modules/.vite/deps/discord__js.js?v=f1677f66". Does the file exist?
Plugin: vite:import-analysis
File: /Users/x/x/node_modules/.vite/deps/discord__js.js?v=f1677f66:50929:65
50927| }
50928| __name(getInitialSendRateLimitState, "getInitialSendRateLimitState");
I'm not sure what the issue is, and guidance would be very helpful
6 replies