LunarRage | DNA
LunarRage | DNA
DIAdiscord.js - Imagine an app
Created by LunarRage | DNA on 11/15/2023 in #djs-questions
Issue with Fetching User Details from Reactions in Discord.js
Hi! I'm encountering a challenge with retrieving user details from message reactions. Below is a detailed breakdown of my setup and the problem: 1. Discord.js and Node Versions: - Discord.js Version: 14.13.0 - Node Version: 18.18.0 2. Bot Configuration: - My bot is configured with the following intents and partials:
const client = new Client({
intents: [GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers, GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Message, Partials.Channel, Partials.Reaction]
});

const client = new Client({
intents: [GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers, GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Message, Partials.Channel, Partials.Reaction]
});

3. Process Reactions Function: - The processReactions function, which is supposed to fetch reactions and their user details, is defined as:
const processReactions = async (message: Message<boolean>[]) => {
try {
let reactions = await fetchReactionsFromMessage(message);
for (const reaction of reactions) {
await fetchReactionUsers(reaction);
}
} catch (error) {
// Error handling
}
}

const processReactions = async (message: Message<boolean>[]) => {
try {
let reactions = await fetchReactionsFromMessage(message);
for (const reaction of reactions) {
await fetchReactionUsers(reaction);
}
} catch (error) {
// Error handling
}
}

4. Fetch Reaction Users Function: - The fetchReactionUsers function is intended to retrieve users from a given reaction:
export async function fetchReactionUsers(reaction: MessageReaction): Promise<User[]> {
let result: User[] = [];
try {
let users = await reaction.users.fetch();
for (const user of users.values()) {
result.push(user);
}
return result;
} catch (error) {
logger.error(error, `Error fetching reaction users`);
return result;
}
}

export async function fetchReactionUsers(reaction: MessageReaction): Promise<User[]> {
let result: User[] = [];
try {
let users = await reaction.users.fetch();
for (const user of users.values()) {
result.push(user);
}
return result;
} catch (error) {
logger.error(error, `Error fetching reaction users`);
return result;
}
}

5. Issue Description: - Despite this setup, I'm unable to access the user details of the reactions. I successfully get the reactions, but the user information is not being retrieved as expected. 6. Request for Assistance: - I would greatly appreciate any insights or suggestions on resolving this issue. Thank you for your help!
16 replies
DIAdiscord.js - Imagine an app
Created by LunarRage | DNA on 10/30/2023 in #djs-questions
🐛 Issue: Permission Error (50013) When Deleting a Role with Discord Bot
Description: I am encountering a 50013 Permission error when my Discord bot tries to delete a role from a server. The role is confirmed to be below the bot's role in the hierarchy. The bot does have Admin permissions in the server. Expected Behavior: The role should be deleted without any permission errors. Actual Behavior: The bot throws a 50013 Permission error. Code Snippets: Role Removal Function:
export const removeAllMembersFromStartupRole = async (guild: Guild) => {
try {
const role: Role | null = guild.roles.cache.find(role => role.name === ROLE_NAME) ?? null;
if (role) {
await role.delete(`Removing bot from guild ${guild.id}`);
}
} catch (error) {
logger.error(error, `Failed to remove role ${ROLE_NAME} from members in guild ${guild.id}`);
}
}
export const removeAllMembersFromStartupRole = async (guild: Guild) => {
try {
const role: Role | null = guild.roles.cache.find(role => role.name === ROLE_NAME) ?? null;
if (role) {
await role.delete(`Removing bot from guild ${guild.id}`);
}
} catch (error) {
logger.error(error, `Failed to remove role ${ROLE_NAME} from members in guild ${guild.id}`);
}
}
Bot Initialization (Index File):
const client = new Client({
intents: [GatewayIntentBits.GuildMembers,GatewayIntentBits.DirectMessageTyping,GatewayIntentBits.DirectMessageReactions,GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Message, Partials.Channel]
});

for (const [event, handler] of Object.entries(eventHandlers)) {
if (handler) {
client.on(event, handler);
}
}
const client = new Client({
intents: [GatewayIntentBits.GuildMembers,GatewayIntentBits.DirectMessageTyping,GatewayIntentBits.DirectMessageReactions,GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Message, Partials.Channel]
});

for (const [event, handler] of Object.entries(eventHandlers)) {
if (handler) {
client.on(event, handler);
}
}
Any insights or solutions to this issue would be greatly appreciated.
10 replies
DIAdiscord.js - Imagine an app
Created by LunarRage | DNA on 10/26/2023 in #djs-questions
private message
I have a strange issue occurring when trying to read messages in my Discord Bot DM. If I remove my Discord bot from my server and re-add it,I am able to start viewing the messages sent to the bot via dm. If I disconnect my bot and restart it wont start receiving messages. If I remove the bot from a server and re-add it to a server it will start logging the private messages correctly. If I turn my server off and restart the bot will stop logging. If I also remove it from a server the bot will stop logging. How do I resolve this and is this the expected behaviour?
import { Client, GatewayIntentBits, Partials } from 'discord.js';
import eventHandlers from './events';
require('dotenv').config();

const client = new Client({
intents: [GatewayIntentBits.DirectMessageTyping,GatewayIntentBits.DirectMessageReactions,GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Message]
});

for (const [event, handler] of Object.entries(eventHandlers)) {
if (handler) {
client.on(event, handler);
}
}
client.login(process.env.DISCORD_KEY);
export default client;
import { Client, GatewayIntentBits, Partials } from 'discord.js';
import eventHandlers from './events';
require('dotenv').config();

const client = new Client({
intents: [GatewayIntentBits.DirectMessageTyping,GatewayIntentBits.DirectMessageReactions,GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Message]
});

for (const [event, handler] of Object.entries(eventHandlers)) {
if (handler) {
client.on(event, handler);
}
}
client.login(process.env.DISCORD_KEY);
export default client;
Handler:
import { Message,ChannelType } from 'discord.js';
import logger from '../lib/logger';

const handleMessageReceived = (message: Message) => {
if (message.channel.type === ChannelType.DM){
logger.info(`Message received in DM from user ${message.author.id}: ${message.content}`);
return;
}

if (message.author.bot) return; // Ignore messages from other bots
logger.info(`Message received in channel ${message.channel.id} from user ${message.author.id}: ${message.content}`);
};

export default handleMessageReceived;
import { Message,ChannelType } from 'discord.js';
import logger from '../lib/logger';

const handleMessageReceived = (message: Message) => {
if (message.channel.type === ChannelType.DM){
logger.info(`Message received in DM from user ${message.author.id}: ${message.content}`);
return;
}

if (message.author.bot) return; // Ignore messages from other bots
logger.info(`Message received in channel ${message.channel.id} from user ${message.author.id}: ${message.content}`);
};

export default handleMessageReceived;
6 replies