【问题标题】:Discord bot log不和谐机器人日志
【发布时间】:2021-01-09 04:25:47
【问题描述】:

所以,我创建了一个 discord.js 机器人并将以下内容添加到 index.js 中:

client.on("guildCreate", guild = {
    const logsServerJoin = client.channels.get('757945781352136794');
    console.log(`The bot just joined to ${guild.name}, Owned by ${guild.owner.user.tag}`);
    client.channels.cache.get('channel id paste here').send(`The bot just joined to ${guild.name}, Owned by ${guild.owner.user.tag}`);

    var guildMSG = guild.channels.find('name', 'general');

    if (guildMSG) {
        guildMSG.send(` Hello there! My original name is \`Bryant\`!\n\ This bot created by **R 1 J 4 N#7686**\n\ For more info
       type \`/help\`!\n\ \`Bryant - Official Server:\`
       https://discord.gg/UsQFpzy`);
    } else {
        return;
    }
});

// Logs of the bot leaves a server and changed the game of the bot
client.on("guildDelete", guild = {
    client.channels.cache.get('757945781352136794').send(`The bot just
       left ${guild.name}, Owned by ${guild.owner.user.tag}`);
    console.log(`The bot has been left ${guild.name}, Owned by ${guild.owner.user.tag}`);
    logsServerLeave.send(`The bot has been left ${guild.name}, Owned by ${guild.owner.user.tag}`);
});

它在终端中没有显示任何错误。它应该记录我在提到的频道中机器人加入和离开的位置,但没有 ????‍♂️。谁能帮我解决这个问题?

【问题讨论】:

    标签: javascript logging discord.js bots


    【解决方案1】:

    如果您在频道中没有收到控制台日志并且没有消息/错误告诉您无法找到频道,则问题很可能在于您如何注册事件,请确保 client 是一个实例discord.js 客户端的,下面是一个最小的工作示例

    const { Client } = require("discord.js")
    
    const client = new Client()
    
    client.on("guildCreate", guild => {
      console.log(`The bot just joined to ${guild.name}, Owned by ${guild.owner.user.tag}`)
    })
    
    client.login("yourtoken")
    

    【讨论】:

    • 不,伙计,我实际上希望日志位于我的不和谐服务器的特定频道中。 :(
    • 如果您没有收到任何错误,则很难提供帮助,请确保您的代码运行,因为如果它不起作用,它应该会出错。例如,在您的代码 sn-p 中,logsServerLeave 没有在任何地方定义
    【解决方案2】:

    如果您尝试从您的 bot 获取日志,在您的家庭 discord 服务器中提醒您,您可以通过多种方式执行此操作:从缓存中获取通道、构建通道或使用 webhook。目前您正在尝试从缓存中获取频道。虽然是一个不错的解决方案,但在以后使用分片时它可能会失败。我个人更喜欢 webhook,因为它们是最简单和最孤立的。

    来自缓存的频道

    这项技术与您的做法非常相似。

    const channel = client.channels.cache.get('757945781352136794')
    channel.send('An Event occurred')
    

    只需将此代码放在您想要记录某些内容的任何位置,就可以了。

    构建通道

    const guild = new Discord.Guild(client, { id: '757945781352136794' });
    const channel = new Discord.TextChannel(guild, { id: '757945781352136794' });
    channel.send('An Event occurred')
    

    此方法类似于从缓存中获取频道,但它会更快,因为您正在构建您的家庭公会和频道,然后发送到它。请注意,您需要一个可以从 message.client 获得的客户端

    网络钩子

    我最喜欢的方法使用 webhook。我建议您阅读有关不和谐 webhook 在 DiscordDiscord.js 中的工作原理

    您还需要创建一个 webhook。这很容易。进入您希望将 webhook 发送到的频道,然后转到集成并创建一个新的 webhook。您可以根据需要更改名称和配置文件,但复制 url,它应该看起来像这样:

    https://discord.com/api/webhooks/757945781352136794/OkMsuUHwdStR90k7hrfEi5*********

    路径的最后一部分是 webhook 令牌,前面的位是通道 ID

    我建议您创建一个可以像这样调用的辅助函数:

    sendWebhook('An event occurred');
    

    然后编写函数创建然后发送到webhook

    function sendWebhook(text) {
        const webhook = new Discord.WebhookClient('757945781352136794', 'OkMsuUHwdStR90k7hrfEi5*********');
        webhook.send(text);
        webhook.destroy();
    }
    

    这不会很动态,并且更改频道会很痛苦,但是对于持续记录(例如公会加入和离开),我认为这是最好的解决方案

    【讨论】:

      【解决方案3】:

      问题可能是您没有启用“特权网关意图”。要打开它们,请转到https://discord.com/developers,单击您的应用程序,然后单击“Bot”,然后向下滚动并启用“PRESENCE INTENT”和“SERVER MEMBERS INTENT”并保存。它现在应该对你有用。

      【讨论】:

        猜你喜欢
        • 2018-03-20
        • 2020-07-03
        • 2020-09-23
        • 2021-10-30
        • 2021-08-05
        • 2021-05-27
        • 2021-07-15
        • 2022-01-07
        • 2019-09-13
        相关资源
        最近更新 更多