Last active
May 15, 2020 16:29
-
-
Save Luckshya/84a4884ff69ce0934bd72b46210eb30b to your computer and use it in GitHub Desktop.
Discord message handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Table to hold our Discord CSessions | |
sessions <- {}; | |
// Wrapper class to manage our session | |
class CDiscord | |
{ | |
session = null; | |
connID = null; | |
channels = null; | |
isConnected = null; | |
function constructor() | |
{ | |
session = ::SqDiscord.CSession(); | |
connID = session.ConnID; | |
channels = { "echo" : "channel ID" }; | |
::sessions.rawset(connID, this); | |
isConnected = false; | |
} | |
function Connect(token) | |
{ | |
session.Connect(token); | |
} | |
function sendMessage(channelID, message) | |
{ | |
session.Message(channelID, message); | |
} | |
function sendEmbed(channelID, embed) | |
{ | |
session.MessageEmbed(channelID, "", embed); | |
} | |
function onReady() | |
{ | |
isConnected = true; | |
} | |
function onMessage(channelID, author, authorNick, authorID, roles, message) | |
{ | |
print("A message is received by Discord connection at ID: " + connID); | |
} | |
} | |
function onDiscord_Ready(session) | |
{ | |
if(sessions.rawin(session.ConnID)) | |
{ | |
local session_s = sessions.rawget(session.ConnID); | |
session_s.onReady(); | |
} | |
} | |
function onDiscord_Message(session, channelID, author, authorNick, authorID, roles, message) | |
{ | |
if(sessions.rawin(session.ConnID)) | |
{ | |
local session_s = sessions.rawget(session.ConnID); | |
session_s.onMessage(channelID, author, authorNick, authorID, roles, message); | |
} | |
} | |
g_DiscordHandler <- CDiscord(); | |
g_DiscordHandler.Connect("token"); | |
// Messages handler to queue the messages to avoid rate limit | |
class Message | |
{ | |
discordHandler = null; | |
timer = null; | |
msgString = null; | |
function constructor(discordHandler) | |
{ | |
this.discordHandler = discordHandler; | |
this.msgString = ""; | |
this.timer = NewTimer("onMessageHandler_Timer", 1050, 0); | |
} | |
function add(msg) | |
{ | |
if( msg.len() > 1920 ) return; // Message too big, skip it! | |
msgString += (msgString.len() == 0 ? msg : "\n" + msg); | |
} | |
function process() | |
{ | |
local tempString = ""; | |
if(msgString.len() > 1950) | |
{ | |
local index = 0; | |
while((index = msgString.find("\n")) != null) | |
{ | |
local msgPart = msgString.slice(0, index + 1); | |
if(msgPart.len() + tempString.len() > 1950) break; | |
tempString += msgPart; | |
} | |
} | |
else | |
{ | |
tempString = msgString; | |
} | |
msgString = msgString.slice(tempString.len()); | |
if(tempString.len() > 0 && discordHandler.isConnected) | |
{ | |
g_DiscordHandler.sendMessage(g_DiscordHandler.channels["echo"], tempString); | |
::print("Sent " + tempString); | |
} | |
} | |
} | |
function onMessageHandler_Timer() | |
{ | |
g_MsgHandler.process(); //Forward the call! | |
} | |
g_MsgHandler <- Message(g_DiscordHandler); | |
// Example usage | |
function onPlayerChat(player, msg) | |
{ | |
g_MsgHandler.add(msg); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment