如何使用网络服务器在 discord.js 中“捕获”基于不和谐的网络钩子并通过我的机器人发送

时间:2021-02-01 21:48:19

标签: lua discord discord.js webserver bots

我拥有一个 FiveM 服务器,很多脚本都可以选择使用 webhooks 并与 discord 集成。我的服务器中有一个机器人,我想让那个机器人发布它们,因为我认为它更干净。所以它应该做的是接收然后基本上发送 webhook 会发送的原始消息。机器人是用 discord.js 编写的,这就是发送的代码的样子。我还会添加一个安全令牌以保护 webhook,因为它现在不是:(它是用 Lua 编写的)

 function dclog(xPlayer, text)
    local playerName = Sanitize(xPlayer.getName())
    
    local discord_webhook = GetConvar('discord_webhook', Config.DiscordWebhook)
    if discord_webhook == '' then
      return
    end
    local headers = {
      ['Content-Type'] = 'application/json'
    }
    local data = {
      ["username"] = Config.WebhookName,
      ["avatar_url"] = Config.WebhookAvatarUrl,
      ["embeds"] = {{
        ["author"] = {
          ["name"] = playerName .. ' - ' .. xPlayer.identifier
        },
        ["color"] = 1942002,
        ["timestamp"] = os.date("!%Y-%m-%dT%H:%M:%SZ")
      }}
    }
    data['embeds'][1]['description'] = text
    PerformHttpRequest(discord_webhook, function(err, text, headers) end, 'POST', json.encode(data), headers)
end

function Sanitize(str)
    local replacements = {
        ['&' ] = '&',
        ['<' ] = '&lt;',
        ['>' ] = '&gt;',
        ['\n'] = '<br/>'
    }

    return str
        :gsub('[&<>\n]', replacements)
        :gsub(' +', function(s)
            return ' '..('&nbsp;'):rep(#s-1)
        end)
end

0 个答案:

没有答案