当前位置: 首页 > 知识库问答 >
问题:

在特定信道中重定向不协调DM

丁正阳
2023-03-14

我开始使用discord.js编程一个discord 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个答案

白越
2023-03-14

如果您不希望bot也发送它的答案,可以添加replace第一个If语句,使用以下语句:

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

希望这有帮助:)

傅茂实
2023-03-14

您可能希望替换以下两行:

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

与:

client.channels.cache.get('726515463017988176').send(messageEmbed);
黄成荫
2023-03-14

client.on('message')回调中,有return,它结束函数的执行。这意味着后面的任何代码(在同一函数中)都是不可访问的。如果你把它移开,它应该会起作用的。

  • 返回MDN
  • 回调函数MDN

尽管是可选的,但也应该确保以分号结束每个语句。

 类似资料:
  • 我使用java为discord制作了一个bot,使用net.dv8tion.jda来管理这个bot。我已经做了,当一个新用户进入机器人将发送一个私人消息给他,问题是一些用户已经禁用了显示来自机器人或不在朋友列表用户的消息的选项。所以我的想法是用一种新的方式来创建它,当一个新用户加入服务器时,bot将创建一个新的textchannel,例如“welcome”,在这个通道中,everyone角色没有权

  • 在我的应用程序我有导航抽屉,所以我有一个主要活动和Rest是碎片。我的应用程序运行良好。当我按back按钮时,它会重定向到以前的片段。它工作得很好。但我想要的是在成功支付后,我会显示成功支付页面,在此页面上,当用户按back按钮时,我想重定向到HomeFragment,但现在它会重定向到Placeorder片段。

  • 在死信endpoint的情况下,我想添加自定义生根(将消息/交换重定向到其他endpoint,将Wire Tap添加到bean等)。不幸的是,我没有看到死信通道类是(以及如何)提供这样的重定向。我需要编写自己的错误处理程序还是可以以某种方式使用死信通道?(我知道在错误的情况下,消息会重定向到死信通道——结束。)

  • 问题内容: 我正在用Go编写程序,该程序执行类似程序的服务器(也就是Go)。现在,我想在启动父程序的终端窗口中获​​得子程序的标准输出。一种方法是使用函数,但这仅在进程退出后才输出标准输出。(这是一个问题,因为这个类似于服务器的程序运行了很长时间,并且我想读取日志输出) 变量是,我不知道该如何使用它来完成任务,因此我在网络上找不到有关此主题的任何有用信息。 对代码的解释:取消注释该函数以获取要编译

  • 我一直在四处寻找,似乎找不到我用Typescript制作的不和谐机器人的这个问题的答案。我的所有命令都放在它们自己的文件夹中,每个命令都有一个单独的文件。有助于保持井井有条。 我见过有人说 但这给了我并且 实际上,每当有人运行重启命令时,我会尝试在每个文本频道(从列表中给出)发送一条机器人消息,因为不管出于什么原因,人们总是重启机器人。我把它实现为一件有趣的事情,如果有人需要使用它,我会时不时地以

  • 问题内容: 为什么我无法在控制器中使用此功能 我得到: 我以前有这个工作。为什么不是现在? 问题答案: 试试这个,如果你正确配置了视图解析器,它应该可以工作