How to display discord messages in roblox chat

So I have a webhook in my discord channel and i want the messages in the channel to be displayed in roblox chat, but how would i do that

If you want to simply read messages from a Discord channel, then a webhook isn’t going to help you. Webhooks can only receive data.

You’d have to set up a bot that reads the most recent message and then stores it somewhere for Roblox to see. I rarely use Discord and I’m not very familiar with the API, so you’ll have to figure out some of it by yourself.

For example, in Javascript, a bot that monitors and stores the most recent message could look like this:

const Discord = require('discord.js');

const client = new Discord.Client();

const channelID = '123456789012345678';

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

client.login('token');

client.on('message', message => {
  if (message.channel.id === channelID) {
    console.log(`${message.content}`);
  }
});

but instead of logging it to the console, you should store it temporarily on a webserver so Roblox is able to receive it. You could simply host a JSON file that looks like this:

{
  "message": "hi",
  "username": "abc123",
  "time": "1618953630"
}

that updates everytime a message is sent.

Then, inside your game, make a script that periodically checks that file for updates using httpService. When it detects a new message, you’ll just have to filter it and then create an in-game chat message with the parsed content.

For example (I haven’t tested this):

local HttpService = game:GetService("HttpService")
local ChatService = game:GetService("Chat")
local TextService = game:GetService("TextService")

local URL = "https://192.158.1.38/message.json"  -- File url
local lastFetchedData = ""

local function filterMessage(message)
    local filteredMessage = TextService:FilterStringForBroadcast(message, game.Players.LocalPlayer)
    return filteredMessage
end

local function checkJSONChanges()
    local success, result = pcall(function()
        return HttpService:GetAsync(URL)
    end)

    if success then
        local newData = result

        if newData ~= lastFetchedData then
            lastFetchedData = newData
            local parsedData = HttpService:JSONDecode(newData)

            if parsedData.username and parsedData.message then
                local filteredMessage = filterMessage(parsedData.message)

                if filteredMessage then
                    local finalMessage = "Username: " .. parsedData.username .. "\nMessage: " .. filteredMessage
                    ChatService:Chat(game.Players.LocalPlayer, finalMessage, Enum.ChatColor.Blue)
                end
            end
        end
    else
        warn("Failed to fetch JSON data:", result)
    end
end

while true do
    checkJSONChanges()
    wait(30)  -- Checks every 30 seconds
end

You probably have to be careful with these types of scripts. I don’t know Roblox’s policy on grabbing messages from Discord, but games have been moderated in the past. I assume as long as you properly filter everything, you should be fine.

5 Likes