2
votes

I started programming a discord bot using discord.js, and I cant figure out a way for the bot to redirect received messages in a specific channel on my server as an embed.

Here's my code so far:

const Discord = require("discord.js");
const client = new Discord.Client();


client.on('ready', () => {
    console.log('Logged in!');
});

client.on('message', msg => {
    if (msg.channel.type == "dm") {
        msg.author.send("bruh dming me has literally no point");
        return;
        const messageEmbed = {
            color: 0x00ff00,
            title: 'Received DM',
            author: {
                name: 'Me'
            },
            description: 'I received the following DM:',
            thumbnail: {
            },
            fields: [
                {
                    name: 'Author:',
                    value: message.author,
                },
                {
                    name: 'Message:',
                    value: messageEmbed,
                    inline: false,
                },
            ],
        }
        client.channels.cache.get('726515463017988176').send(messageEmbed)
    }
});

client.login('this is where my token was but i had to replace it lol');

client.on('message', message => {
    console.log(message.content);
    if (message.channel.type === 'text') {
        if (message.content === '!ip') {
            message.channel.send('[insert server ip here]');
        }
    }
});

PS: as you can probably tell, I'm new to JS.

Edit: I managed to semi get it to work now, it sends every message received in the chat but every answer (msg.author.send("bruh dming me has literally no point");) as well. Is there a way to skip Messages with specific content and is there a way to put the redirected message in an embed with date, author and message?

My code now:

const Discord = require("discord.js");
const client = new Discord.Client();


client.on('ready', () => {
    console.log('Logged in!');
});

client.on('message', msg => {
    if (msg.channel.type == "dm") {
        msg.author.send("bruh dming me has literally no point");
//      return;
        const messageEmbed = {
            color: 0x00ff00,
            title: 'Recieved DM',
            author: {
                name: 'Me'
            },
            description: 'I recieved the following DM:',
            thumbnail: {
            },
            fields: [
                {
                    name: 'Author:',
                    value: msg.author,
                },
                {
                    name: 'Message:',
                    value: msg.content,
                    inline: false,
                },
            ],
        }
//      if (msg.content) == "bruh dming me has literally no point")
        client.channels.cache.get('726515463017988176').send(msg.content)
        client.channels.cache.get('726515463017988176').send(msg.author)
    }
});

client.login('tokentokentokentokentokentokentoken(secret)');

client.on('message', message => {
    console.log(message.content);
    if (message.channel.type === 'text') {
        if (message.content === '!ip') {
            message.channel.send('[insert server ip here]');
        }
    }
});

Probably the last edit: I managed to get it to work by using another embed template. Code:

const Discord = require("discord.js");
const client = new Discord.Client();

client.login('you probably already know what belongs here xd')

client.on('ready', () => {
    console.log('Logged in!')
});

client.on('message', message => {
    if (message.channel.type === "dm" && message.author.id !== client.user.id) {
        console.log("-----DM-----")
        console.log(message.content)
        console.log(message.author.tag)
        console.log("-----DM-----")
        message.author.send("bruh dming me has literally no point");
        client.channels.cache.get('726919268142415973').send({
                embed: {
                color: 0x8b0000,
                    author: {
                        name: "I recieved the following DM:",
                        icon_url: message.author.avatarURL
                    },
                    title: message.author.tag,
                    description: message.content,
                    timestamp: new Date(),
                    footer: {
                        icon_url: client.user.avatarURL,
                        text: "Staff"
                    }
                }
        });
    }
});
3
Welcome to Stack Overflow! Please spend a minute reading How do I ask a good question?. Could you please tell us a bit more about what you've tried already? :)Pedro Fracassi

3 Answers

2
votes

In your client.on('message') callback, you've got return, which ends the function's execution. This means any code after it (in the same function) is not reachable. It should work if you remove it.

Though optional, it's also advisable to make sure you end every statement with a semicolon.

1
votes

You might want to replace the following two lines :

client.channels.cache.get('726515463017988176').send(msg.content);
client.channels.cache.get('726515463017988176').send(msg.author);

With :

client.channels.cache.get('726515463017988176').send(messageEmbed);
1
votes

If you do not want the bot to send it's answer as well, you can add replace the very first if statement with the following :

if (msg.channel.type === "dm" && message.author.id !== client.user.id) {

Hope this helps :)