在特定频道中重定向Discord DM

时间:2020-06-28 12:02:39

标签: javascript bots discord discord.js

我开始使用discord.js对不和谐的bot进行编程,但我想不出该bot如何以嵌入方式重定向服务器上特定通道中收到的消息的方法。

到目前为止,这是我的代码:

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:您可能会说,我是JS的新手。

编辑:我设法使它现在开始工作,它发送聊天中收到的所有消息,但也发送每个答案(msg.author.send("bruh dming me has literally no point");)。是否可以跳过具有特定内容的消息,并且可以将重定向的消息放入带有日期,作者和消息的嵌入中?

现在我的代码:

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]');
        }
    }
});

可能是最后一次编辑: 我设法通过使用另一个嵌入模板使其工作。代码:

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 个答案:

答案 0 :(得分:2)

在您的client.on('message')回调中,您获得了return,该结束了函数的执行。这意味着它后面的任何代码(在同一功能中)均无法访问。如果将其删除,它应该可以工作。

尽管是可选的,但建议您确保每个语句都以分号结尾。

答案 1 :(得分:1)

您可能要替换以下两行:

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

使用:

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

答案 2 :(得分:1)

如果您不希望机器人也发送其答案,则可以添加以下内容替换第一个if语句:

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

希望这会有所帮助:)

相关问题