Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Custom ROBLOX command --> Discord webhook?

Asked by 4 years ago

There is a way to do it, just wondering how. I'm wondering how I can make a command transfer over to a discord webhook. An example of what I mean is: if someone with a certain rank in a group (or a higher rank than the certain rank) said something like ":Announce (message)" then it would ping everyone in the server and making an announcement message. This could be used for announcing trainings quickly or something like that. If someone could reply to me on this, It'd be highly appreciated.

2 answers

Log in to vote
-1
Answered by
NotedAPI 810 Moderation Voter
4 years ago

Hello, Starlighxt!

I've made this script to hopefully match what you need. I put little notes here and there so you can see what everything does.

local http = game:GetService("HttpService")
local webhook = "" -- Your webhook here

local GroupID = 0 -- Change this to your group ID
local RanksAllowed = 0 -- Change this to what ranks are allowed. EX. Everything above 244 is allowed)
local prefix = ":" -- Change this to the prefix you want

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        local m = msg:lower()

        if m:sub(1,10) == prefix.."announce " then -- This will basically see what the command is
            if player:GetRankInGroup(GroupID) >= RanksAllowed then
                local Announcement = m:sub(11) -- Gets everything after the command

                local Data = {
                    ["username"] = "Announce Bot", -- Whatever name you want for the bot
                    ["content"] = "@everyone "..Announcement
                }
                Data = http:JSONEncode(Data)
                http:PostAsync(webhook, Data)
            end
        end
    end)
end)
Ad
Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Hello, Starlighxt!

Scripting Helpers is not an requesting website, please aways make a try before posting a question here

You will need to know this before creating this script:

Player Added Event

Player Chatted Event

Dictionaries

For - do Loop

Manipulating strings

Using functions

HTTPService - Post Async

HTTPService - Json Enconde

First of all, lets define some variables: prefix, groupId, url

local prefix = ":"
local groupId = 3605321 -- Put group Id here
local url = "WebHook URL" -- Put webhook URL here

After that, we will define a variable for the HTTPService

local httpService = game:GetService("HttpService")

Then, we create an dictionary to hold our commands and functions that need to run for each command

local commands = {
    [1] = {
        ["command"] = "announce",
        ["function"] = function(player, args, fullArgs)
            if player:GetRankInGroup(groupId) > 50 then --If you want, you can change the needed rank here
            local request = {
                ["content"] = player.Name .. "'s announcement: \n" .. fullArgs .. "\n@everyone" -- \n is a new line in DiscordJs
            }   
            httpService:PostAsync(url, httpService:JSONEncode(request))
            end
        end
    }
}

Now, lets start with the events, first waiting for player join and then listening for him chatting:

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)

    end
end

All the rest of the code will be insite the player.Chatted function

Now, we need to check for the prefix...

local prefixCheck = string.sub ( msg,1, string.len(prefix)) -- Cuts the string to check for the prefix
if prefixCheck ~= prefix then return end -- Stops the code if the prefix isn't there

For now, your code should be something like

local prefix = ":"
local groupId = 3605321 -- Put group Id here
local url = "https://discordapp.com/api/webhooks/657656270156726318/vzLbZ6vHgVk32lbL6O4b2z9CgH7G_cwYRWzKgyDVlHUJEiucBS-KvNgsPlCtdnqTr-X5" -- Put webhook URL here

local httpService = game:GetService("HttpService")



local commands = {
    [1] = {
        ["command"] = "announce",
        ["function"] = function(player, args, fullArgs)
            if player:GetRankInGroup(groupId) > 50 then --If you want, you can change the needed rank here
            local request = {
                ["content"] = player.Name .. "'s announcement: \n" .. fullArgs .. "\n@everyone"
            }   
            httpService:PostAsync(url, httpService:JSONEncode(request))
            end
        end
    }
}

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        local prefixCheck = string.sub ( msg,1, string.len(prefix)) -- Cuts the string to check for the prefix
        if prefixCheck ~= prefix then return end -- Stops the code if the prefix isn't there

    end)
end)

After checking the prefix, we need to try extracting the command from the message, so we use string.sub and a for do loop

for _,v in pairs(commands) do
    local cmd = string.sub ( msg, string.len(prefix)+1, string.len(v["command"])+string.len(prefix)) -- Cuts the string to try getting only the command
    if cmd:lower() == v["command"]:lower() then --Checks if this is the command we want

    end
end

This part of the code will go inside the if cmd:lower() ... end

Basically we are extracting the arguments from the message on a full string, then transforming it on a table and then sending thease variables to the function that we created earlyear inside the dictionary

local FullArgs = string.sub ( msg, string.len(v["command"])+2) -- Try grabbing only the string arguments
local args = string.split(FullArgs, " ")

v["function"](player, args, FullArgs)

Yes, there're other ways on doing this, but I think this is one of the best because is easy to add new commands

Answer this question