Skip to content

Commit

Permalink
Add Command Support
Browse files Browse the repository at this point in the history
  • Loading branch information
HotaruBlaze committed Mar 31, 2021
1 parent a16a4fb commit e69728a
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/discordCommands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"bytes"
"encoding/json"
"log"

"github.com/bwmarrin/discordgo"
"github.com/spf13/viper"
)

func discordCommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
Data := make(map[string]string)
var commandStruct baseResponce
commandStruct.ServerID = viper.GetViper().GetString("tes3mp.serverid")
commandStruct.Method = "Command"
commandStruct.Source = "DiscordCommand"
Data["Command"] = m.Content[1:]
Data["replyChannel"] = m.ChannelID
commandStruct.Data = Data

log.Println("Staff Member '"+m.Author.Username+"' has executed the following command:", m.Content[1:])

jsonResponce, err := json.Marshal(commandStruct)
checkError("discordCommandHandler", err)
sendResponce := bytes.NewBuffer(jsonResponce).String()
IRCSendMessage(viper.GetString("irc.systemchannel"), sendResponce)
}
4 changes: 4 additions & 0 deletions src/relayDiscord.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if m.Content[:1] == viper.GetString("discord.commandprefix") && isStaffMember(m.Author.ID, m.GuildID) {
discordCommandHandler(s, m)
return
}
if m.ChannelID != viper.GetString("discord.serverchat") {
return
}
Expand Down
14 changes: 7 additions & 7 deletions tes3mp/custom/IrcBridge/IrcBridge.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local cjson = require("cjson")
local goTES3MP = require("custom.goTES3MP.main")
local goTES3MPSync = require("custom.goTES3MP.sync")
local goTES3MPUtils = require("custom.goTES3MP.utils")
local goTES3MPCommands = require("custom.goTES3MP.commands")

local IrcBridge = {}

Expand Down Expand Up @@ -90,13 +91,12 @@ IrcBridge.RecvMessage = function()
if responce.Status == "Pong" and WaitingForSync then
goTES3MPSync.GotSync(responce.ServerID, responce.SyncID)
end
-- if responce.method == "Discord" or responce.method == "IRC" then
-- for pid, player in pairs(Players) do
-- if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then
-- IrcBridge.ChatMessage(pid, responce)
-- end
-- end
-- end
if
responce.method == "Command" and responce.data["replyChannel"] ~= nil and
responce.data["Command"]
then
goTES3MPCommands.main(responce.data["Command"], responce.data["replyChannel"])
end
if responce.method == "DiscordChat" or responce.method == "IRC" then
for pid, player in pairs(Players) do
if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then
Expand Down
121 changes: 121 additions & 0 deletions tes3mp/custom/goTES3MP/commands.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
local commands = {}
local goTES3MPUtils = require("custom.goTES3MP.utils")

commands.kickPlayer = function(player,discordReplyChannel)
targetPid = commands.getPlayerPID(player)
if targetPid ~= nil then
tes3mp.SendMessage(
targetPid,
color.Red .. "[SYSTEM]" .. " " .. "You have been kicked by an Administrator.",
false
)
tes3mp.Kick(targetPid)
commands.SendResponce(discordReplyChannel)
end
end

commands.runConsole = function(player, consoleCommand, discordReplyChannel)
targetPid = commands.getPlayerPID(player)
if targetPid ~= nil then
logicHandler.RunConsoleCommandOnPlayer(targetPid, consoleCommand)
commands.SendResponce(discordReplyChannel)
end
end

commands.resetKills = function(discordReplyChannel)
for refId, killCount in pairs(WorldInstance.data.kills) do
WorldInstance.data.kills[refId] = 0
end
WorldInstance:QuicksaveToDrive()
if tableHelper.getCount(Players) > 0 then
for pid, player in pairs(Players) do
WorldInstance:LoadKills(pid, true)
tes3mp.SendMessage(pid, "All the kill counts for creatures and NPCs have been reset.\n", false)
end
end
tes3mp.LogMessage(enumerations.log.INFO, "All the kill counts for creatures and NPCs have been reset.")
commands.SendResponce(discordReplyChannel)
end

commands.main = function(str, discordReplyChannel)
tes3mp.LogMessage(enumerations.log.INFO, "Recieved "..'"'..str..'"'.." from discord.")
cmdChunks = {}
for substring in str:gmatch("%S+") do
table.insert(cmdChunks, substring)
end

command = string.lower(cmdChunks[1])

if command == "kickplayer" then
print(tableHelper.concatenateFromIndex(cmdChunks, 2))
commands.kickPlayer(tableHelper.concatenateFromIndex(cmdChunks, 2), discordReplyChannel)
end
if command == "runconsole" then
print(tableHelper.concatenateFromIndex(cmdChunks, 3))
commands.runConsole(cmdChunks[2], tableHelper.concatenateFromIndex(cmdChunks, 3), discordReplyChannel)
end
if command == "resetkills" then
commands.resetKills(discordReplyChannel)
end
if command == "help" then
local commandList = ""
commandList = commandList .. "```" .. "\n"
commandList = commandList .. "!kickplayer (PlayerName): Kicks the specified player from the tes3mp server." .. "\n"
commandList = commandList .. "!runconsole (PlayerName) (Console Command): Run a console command on a specific Player." .. "\n"
commandList = commandList .. "!resetkills: Reset player kills." .. "\n"
commandList = commandList .. "```" .. "\n"

local messageJson = {
method = "rawDiscord",
source = "TES3MP",
serverid = GOTES3MPServerID,
syncid = GoTES3MPSyncID,
data = {
channel = discordReplyChannel,
server = GoTES3MP_DiscordServer,
message = commandList
}
}

local responce = goTES3MPUtils.isJsonValidEncode(messageJson)
if responce ~= nil then
IrcBridge.SendSystemMessage(responce)
end
end

end

commands.SendResponce = function(discordReplyChannel)
local messageJson = {
method = "rawDiscord",
source = "TES3MP",
serverid = GOTES3MPServerID,
syncid = GoTES3MPSyncID,
data = {
channel = discordReplyChannel,
server = GoTES3MP_DiscordServer,
message = "**Command Executed**"
}
}

local responce = goTES3MPUtils.isJsonValidEncode(messageJson)
if responce ~= nil then
IrcBridge.SendSystemMessage(responce)
end
end

commands.getPlayerPID = function(str)
local lastPid = tes3mp.GetLastPlayerId()
if str ~= nil then
for playerIndex = 0, lastPid do
if Players[playerIndex] ~= nil and Players[playerIndex]:IsLoggedIn() then
if string.lower(Players[playerIndex].name) == string.lower(str) then
return playerIndex
end
end
end
end
return nil
end

return commands

0 comments on commit e69728a

Please sign in to comment.