Skip to content

Commit

Permalink
Add sample bot script
Browse files Browse the repository at this point in the history
  • Loading branch information
rasikhq committed Aug 11, 2021
1 parent a873bdf commit d5ce706
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
62 changes: 62 additions & 0 deletions [resources]/[dev bot]/SampleBot/bot.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
BOT_TOKEN = "YOUR_TOKEN"
BOT_PREFIX = "."
CHANNEL_ID = "CHANNELID"

addEventHandler("onResourceStart", resourceRoot, function()
print("Discord module script started")
g_Bot = DiscordBot:new()

g_Bot:setPrefix(BOT_PREFIX)
g_Bot:connect(BOT_TOKEN)
end)

addEventHandler("onResourceStop", resourceRoot, function()
print("Discord module script stopping")
if g_Bot then
g_Bot:disconnect()
g_Bot = nil
end
end)

addEvent("onDiscordReady", true)
addEventHandler("onDiscordReady", resourceRoot, function(jdata)
local data = fromJSON(jdata)
iprint("onDiscordReady", data)

g_Bot:setActivity("Some Activity") -- Sets the Playing [ACTIVITY] for the bot
end)

addEvent("onDiscordResume", true)
addEventHandler("onDiscordResume", resourceRoot, function(jdata)
local data = fromJSON(jdata)
iprint("onDiscordResume", data)
end)

addEvent("onDiscordServer", true)
addEventHandler("onDiscordServer", resourceRoot, function(jdata)
local data = fromJSON(jdata)
iprint("onDiscordServer", data)
end)

addEvent("onDiscordMessage", true)
addEventHandler("onDiscordMessage", resourceRoot, function(jdata)
local data = fromJSON(jdata)
iprint("onDiscordMessage", data)
end)

addEventHandler("onPlayerJoin", root, function()
g_Bot:sendMessage(CHANNEL_ID, getPlayerName(source).." joined the server")
end)

addEventHandler("onPlayerQuit", root, function(quitType, reason)
g_Bot:sendMessage(CHANNEL_ID, getPlayerName(source).." left the server ("..quitType..") "..(reason and "["..reason.."]" or ""))
end)

addEventHandler("onPlayerChat", root, function(msg, msgType)
g_Bot:sendMessage(CHANNEL_ID, getPlayerName(source) .. ": " .. msg)
end)

_getPlayerName = getPlayerName
function getPlayerName(player)
return _getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):gsub("_|*`", "")
end
3 changes: 3 additions & 0 deletions [resources]/[dev bot]/SampleBot/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<meta>
<script src="bot.lua"/>
</meta>

0 comments on commit d5ce706

Please sign in to comment.