I am very new to the scene of development, and even through research have yet to understand the correct use of the OnPlayerChat hook.
What I plan on doing is as follows:
When a player says a specific trigger word in chat, a bot prefixed [FriendBot] will reply with a message.
I've tried doing things like
function PLUGIN:OnPlayerChat(arg)
-- here you will get the name of the player, and on second the steamid of the player
print(arg.connection.player.displayName .. " - " .. rust.UserIDFromPlayer(arg.connection.player) .. ": Tried to talk but was muted")
-- he you will get the full line of what he was trying to say
print("He tried to say: " .. arg:GetString(0, "text"))
-- return false = cancel the message
return false
end
however, my Visual Studio Code underlined nearly the entire thing in red. I've tried taking pieces out, and starting with smaller bits, but no matter what, it always ends in tons of errors.
Thanks.
Solved Listening for wording OnPlayerChat?
Discussion in 'Rust Development' started by Ballsack_, Jul 23, 2018.
-
Wulf Community Admin
That is Lua code you are trying to use, which will not work in a C# plugin.
-
-
Wulf Community Admin
A C# example: Solved - OnPlayerChat and return (C#)
-
Problem is, I get a error saying I may be missing a reference assembly from the ConsoleSystem part. Below are the references I use.
Attached Files:
-
-
Wulf Community Admin
-
-
Ended up doing this. Thread Solved. (I can't figure out how to put solved in the title)
Code:public class MrLonely : RustPlugin { [ChatCommand("lonely")] //Lonely Command void LonelyCommand(BasePlayer player) { SendMessage(player, "[FriendBot]: Hey, " + player.displayName + ", there is no reason to feel alone. I will always be here for you."); } void SendMessage(BasePlayer player, string msg, params object[] args) { PrintToChat(player, msg, args); } private Dictionary<string, string> _phrases = new Dictionary<string, string> { ["I'm lonely"] = "/lonely" }; private void OnUserChat(IPlayer player, string message) { var basePlayer = player.Object as BasePlayer; basePlayer.SendConsoleCommand("chat.say \"/lonely\""); }
-
Wulf Community Admin
Code:void OnPlayerChat(ConsoleSystem.Arg arg) { BasePlayer player = arg.Connection.player as BasePlayer; if (arg.Args.Contains("lonely")) { PrintToChat(player, "[FriendBot]: Hey, " + player.displayName + ", there is no reason to feel alone. I will always be here for you."); } }
-
This code resulted in this error when attempting to load into the game.
Code:Error while compiling: MrLonely.cs(69,13): error CS1514: Unexpected symbol `}', expecting `.' or `{'
EDIT: I had it outside of public class...