Hi!
I'm currently using Discord.JS and Nodered to create a Discord bot. I have set up an function node which establishes the connection and then stores the session(the client) in gobal context for later use. The function node also listens for new messages. If i were to send this message through the flow for further processing, then NodeRed would crash. I have to extract the properties i want into a new object(contains user id's, message content and so on) .
If i want to process this message in another function node, i would have to import the client from global context, and use the newly created object to determine which message the bot would reply to etc. This creates extra overhead due to me then having to create new objects all the time, instead of being able to just using the built in methods in the message. Is there a way to forward the object and its content and functions created by the event-listener?
I have set up examples with codesnippets to further help me explain my issues due to me not being fluent in neither English or Javascript
In a regular nodejs-app you would handle 'everything' in a single file
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// This is the event listener and message object im talking about. It's pretty easy to reply to the message by using the .reply-method.
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
}
});
client.login('token');
If i want to handle the bots logic using nodered and its flow-based approach, i would have to do something like this:
The node which establishes the connection:
const client = new Discord.Client();
// Makes the client session available for later use
global.set('DCClient', client);
// This is the event listener and message object im talking about.
client.on('message', message => {
// Do not react to own messages
if (message.author.bot) return;
const ChannelId = message.channel.id
const MessageId = message.id
const GuildId = message.guild.id
const SenderMemberId = message.member.id
const Content = message.content
const Attachments = message.attachments
// I have to create a new object due to not being able to send the 'message'-object
let msg = {
ChannelId: ChannelId,
MessageId: MessageId,
GuildId: GuildId,
SenderMemberId: SenderMemberId,
Content: Content,
Attachments: Attachments
}
// node.send(message) does not work
node.send(msg)
})
The node connected to the listeneer-node which replies to the message:
const client = global.get('DCClient')
if(msg.Content === 'ping'){
client.channels.cache.get(msg.ChannelId).send(msg.payload)
}
May not seem much from the examples provided, but it's bothering me.