Skip to content

Instantly share code, notes, and snippets.

@Aerodos12
Created September 27, 2017 20:30
Show Gist options
  • Save Aerodos12/50833da53e83f3a1069563ee1a325a20 to your computer and use it in GitHub Desktop.
Save Aerodos12/50833da53e83f3a1069563ee1a325a20 to your computer and use it in GitHub Desktop.
DiscordService (Game Chat)
┏━┓︱┏┓ ┏━━┓ ┏━━━━┓ ┏━━━┓ ┏━━━┓ ┏━━━┓ ┏━━━┓ ┏━━━━┓ ┏━━━┓ ┏━━━┓
┃┃┗┓┃┃ ┗┫┣┛ ┃┏┓┏┓┃ ┃┏━┓┃ ┃┏━┓┃ ┃┏━┓┃ ┃┏━━┛ ┃┏┓┏┓┃ ┃┏━┓┃ ┃┏━┓┃
┃┏┓┗┛┃ ︱┃┃︱ ┗┛┃┃┗┛ ┃┗━┛┃ ┃┃︱┃┃ ┃┗━┛┃ ┃┗━━┓ ┗┛┃┃┗┛ ┃┗━┛┃ ┃┃︱┃┃
┃┃┗┓┃┃ ︱┃┃︱ ︱︱┃┃︱︱ ┃┏┓┏┛ ┃┃︱┃┃ ┃┏┓┏┛ ┃┏━━┛ ︱︱┃┃︱︱ ┃┏┓┏┛ ┃┃︱┃┃
┃┃︱┃┃┃ ┏┫┣┓ ︱︱┃┃︱︱ ┃┃┃┗┓ ┃┗━┛┃ ┃┃┃┗┓ ┃┗━━┓ ︱︱┃┃︱︱ ┃┃┃┗┓ ┃┗━┛┃
┗┛︱┗━┛ ┗━━┛ ︱︱┗┛︱︱ ┗┛┗━┛ ┗━━━┛ ┗┛┗━┛ ┗━━━┛ ︱︱┗┛︱︱ ┗┛┗━┛ ┗━━━┛
-----------------------------------------------------------------------
# Current Version: v1.0.0
# Versioning System
a.b.c
a - major version, usually NOT backwards-compatible
b - minor version, backwards-compatible
c - patch, bug fixes
If an update that is not backwards-compatible rolls out, there will
be a section here for updating your code.
=======================================================================
█▀▀▄ █▀▀█ █▀▀ █░░█ █▀▄▀█ █▀▀ █▀▀▄ ▀▀█▀▀ █▀▀█ ▀▀█▀▀ ▀█▀ █▀▀█ █▀▀▄
█░░█ █░░█ █░░ █░░█ █░▀░█ █▀▀ █░░█ ░░█░░ █▄▄█ ░░█░░ ░█░ █░░█ █░░█
▀▀▀░ ▀▀▀▀ ▀▀▀ ░▀▀▀ ▀░░░▀ ▀▀▀ ▀░░▀ ░░▀░░ ▀░░▀ ░░▀░░ ▀▀▀ ▀▀▀▀ ▀░░▀
-----------------------------------------------------------------------
# Welcome!
========
Welcome to the NitroDiscordAPI v1.0.0 documentation.
About
-----
NitroDiscordAPI is a powerful Discord webhook module for ROBLOX that aims for 100% API coverage.
Features:
- Object-oriented
- Performant
- Allows the use of Embeds
- 100% coverage of the webhook API
# Definitions
===========
StringResolvable
----------------
This can mean either a string, or a <RichEmbed>.
WebhookOptions
--------------
A table with webhook options.
# Webhook options list:
Type Name Description
============================================================================================
string | username | override the default username of the webhook
string | avatar_url | override the default avatar of the webhook
bool | tts | if set to true, it will enable Text-To-Speech on the message
<RichEmbed> | embed | RichEmbed to send
Example: { username = "Foobar", tts = true }
# Classes
=======
When you see a word that has <> characters around it, it signifies
a class.
Discord
-------
The main entry-point for all classes.
Automatically created when you use the require() function on the module.
From there you can create other classes, such as a <Webhook>.
Webhook
-------
Webhooks are a low-effort way to post messages to channels in Discord.
They do not require a bot user or authentication to use.
Methods:
<Webhook>.new(string webhookURL) -- Creates a new <Webhook> class.
-- Arguments:
webhookURL - Webhook URL (REQUIRED)
<Webhook>:send(StringResolvable content, WebhookOptions options) -- Sends a message.
-- Arguments:
content - Message to send or <RichEmbed> (REQUIRED)
options - Webhook options
RichEmbed
---------
Represents an Embed Object.
All RichEmbed methods return itself.
This allows for some pretty nice nesting capabilities. For example:
# local myRichEmbed = <Discord>.RichEmbed.new():setTitle("Hello world!"):setDescription("This was sent with NitroDiscordAPI")
Look up "Discord Embed Cheat Sheet" for more information on what represents what.
Methods:
<RichEmbed>:addField(string name, string value, bool inline)
-- Arguments:
name - The name/title of a field (REQUIRED)
value - The value/description of a field (REQUIRED)
inline - If true, this field will be allowed to be displayed inline with other fields
<RichEmbed>:setAuthor(string name, string icon, string url)
-- Arguments:
name - The name of the author (REQUIRED)
icon - An image URL that is to be displayed next to the author's name
url - The URL to redirect to if you click on the author's name
<RichEmbed>:setColor(int color)
-- Arguments:
color - A color represented in decimal to be shown (REQUIRED) (Example: 3066993, which is green)
<RichEmbed>:setDescription(string description)
-- Arguments:
description - The description of the embed (REQUIRED)
<RichEmbed>:setFooter(string text, string icon)
-- Arguments:
text - The text to be displayed on the footer (REQUIRED)
icon - An image URL to be displayed next to the footer
<RichEmbed>:setImage(string url)
-- Arguments:
url - An image URL to be displayed as an image in the embed (REQUIRED)
<RichEmbed>:setThumbnail(string url)
-- Arguments:
url - A thumbnail URL to be displayed as a thumbnail in the embed (REQUIRED)
<RichEmbed>:setTimestamp(string timestamp)
-- Arguments:
timestamp - An ISO8601 timestamp. Look it up on Google for more information. (REQUIRED)
<RichEmbed>:setTitle(string title)
-- Arguments:
title - The title of the embed (REQUIRED)
<RichEmbed>:setURL(string url)
-- Arguments:
url - The URL of the embed (REQUIRED)
=======================================================================
█▀▀ █░░█ █▀▀█ █▀▀▄ █▀▀▀ █▀▀ █░░ █▀▀█ █▀▀▀
█░░ █▀▀█ █▄▄█ █░░█ █░▀█ █▀▀ █░░ █░░█ █░▀█
▀▀▀ ▀░░▀ ▀░░▀ ▀░░▀ ▀▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀▀ ▀▀▀▀
-----------------------------------------------------------------------
# Version v1.0.0 - 14/09/2017
+ Added classes: <Discord>, <Webhook>, <RichEmbed>
+ Added demos and use cases
=======================================================================
--[[
Remember to provide webhook link
--]]
local discord = require(script.Parent.Parent:WaitForChild("NitroDiscordAPI"))
local webhook = discord.Webhook.new("")
local feedbackEmbed = discord.RichEmbed.new()
:setAuthor("Game Feedback", "https://static-cdn.jtvnw.net/jtv_user_pictures/roblox-profile_image-b364d141205e2f1b-300x300.png")
:setTitle("Feedback Information")
:setDescription("**Feedback sender:** NitroretroOfficial\n**Type of feedback:** Suggestion")
:addField("Feedback", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc fringilla eget diam sit amet auctor. Etiam iaculis est non orci commodo, et commodo metus hendrerit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Sed eu sapien dolor. Integer eget rhoncus nibh. Pellentesque id sodales lectus.")
:setThumbnail("https://t2.rbxcdn.com/4979bf220d8550011c17c5bb83d38b92")
:setFooter("Powered by NitroDiscordAPI")
:setColor(3066993)
webhook:send(feedbackEmbed, { username = "ROBLOX Game Feedback", avatar_url = "https://static-cdn.jtvnw.net/jtv_user_pictures/roblox-profile_image-b364d141205e2f1b-300x300.png" })
--[[
Remember to provide webhook link
--]]
local discord = require(script.Parent.Parent:WaitForChild("NitroDiscordAPI"))
local webhook = discord.Webhook.new("")
game:GetService("Players").PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message, recipient)
local avatarImage = "https://www.roblox.com/headshot-thumbnail/image?userId="..player.UserId.."&width=420&height=420&format=png"
webhook:send(message, { username = player.Name, avatar_url = avatarImage })
end)
end)
--[[
NitroDiscordAPI
===============
Documentation in "README" file located under this script.
# Made by Nitroretro
https://www.roblox.com/user.aspx?id=105118309
Version v1.0.0
--]]
--[[
LICENSE
=======
MIT License
Copyright (c) 2017
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
--[[
█░░░█ █▀▀█ █▀▀█ █▀▀▄ ▀█▀ █▀▀▄ █▀▀▀
█▄█▄█ █▄▄█ █▄▄▀ █░░█ ░█░ █░░█ █░▀█
░▀░▀░ ▀░░▀ ▀░▀▀ ▀░░▀ ▀▀▀ ▀░░▀ ▀▀▀▀
DO NOT EDIT THE CODE BELOW UNLESS
YOU KNOW WHAT YOU'RE DOING.
--]]
local http = game:GetService("HttpService")
local logsEnabled = true
function isHttpEnabled()
local isEnabled = pcall(function()
http:GetAsync('http://www.google.com/')
end)
return isEnabled
end
function log(message)
if logsEnabled then
print("[NitroDiscordAPI] "..message)
end
end
function wrn(message)
warn("[NitroDiscordAPI] "..message)
end
if not isHttpEnabled() then
wrn("Critial error: HTTP Requests are not enabled in this place. To enable, do the following:")
wrn("Locate HttpService under the Explorer window, select it, and set its HttpEnabled property to true.")
end
local Discord = {}
function Discord.new(properties)
return setmetatable({properties = properties}, Discord)
end
Discord.__index = Discord
local RichEmbed = {}
function RichEmbed.new()
return setmetatable({ fields = {} }, RichEmbed)
end
RichEmbed.__index = RichEmbed
function RichEmbed:addField(name, value, inline)
if name == nil or value == nil then error("You can't add a field with an empty name or value.") end
if inline == nil then inline = false end
self.fields[#self.fields + 1] = {
["name"] = name,
["value"] = value,
["inline"] = inline
}
return self
end
function RichEmbed:setAuthor(name, icon, url)
if name == nil then error("You must specify an author name.") end
if icon == nil then icon = "" end
if url == nil then url = "" end
self.author = {
["name"] = name,
["icon_url"] = icon,
["url"] = url
}
return self
end
function RichEmbed:setColor(color)
if color == nil then error("You must specify a color.") end
self.color = color
return self
end
function RichEmbed:setDescription(description)
if description == nil then error("You must specify a description.") end
self.description = description
return self
end
function RichEmbed:setFooter(text, icon)
if text == nil then error("You must specify the footer's text.") end
if icon == nil then icon = "" end
self.footer = {
["text"] = text,
["icon_url"] = icon
}
return self
end
function RichEmbed:setImage(url)
if url == nil then error("You must specify the image's URL.") end
self.image = {
["url"] = url
}
return self
end
function RichEmbed:setThumbnail(url)
if url == nil then error("You must specify the thumbnail's URL.") end
self.thumbnail = {
["url"] = url
}
return self
end
function RichEmbed:setTimestamp(timestamp)
if timestamp == nil then error("You must specify the ISO8601 timestamp.") end
self.timestamp = timestamp
return self
end
function RichEmbed:setTitle(title)
if title == nil then error("You must specify the title.") end
self.title = title
return self
end
function RichEmbed:setURL(url)
if url == nil then error("You must specify the URL.") end
self.url = url
return self
end
Discord.RichEmbed = RichEmbed
local Webhook = {}
function Webhook.new(url, options)
if url == nil then error("You must provide an URL for this webhook.") end
if options == nil then options = {} end
return setmetatable({ url = url, options = options }, Webhook)
end
Webhook.__index = Webhook
function Webhook:send(content, options)
if content == nil then error("You must provide content for the webhook to send.") end
local requestData = {}
if type(content) == "string" then
requestData["content"] = content
end
local richEmbed
if type(content) == "table" then richEmbed = content
elseif options["embed"] ~= nil then richEmbed = options["embed"]
end
if richEmbed ~= nil then
local embedData = {}
for i, v in pairs(richEmbed) do
embedData[i] = v
end
requestData["embeds"] = {}
requestData["embeds"][1] = embedData
end
if options ~= nil then
for i, v in pairs(options) do
if type(v) ~= "table" then
requestData[i] = v
end
end
end
pcall(function()
http:PostAsync(self.url, http:JSONEncode(requestData))
end)
end
Discord.Webhook = Webhook
return Discord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment