Page 1 of 1

Adding a prefix to chat

Posted: Sat Apr 14, 2018 10:55 pm
by Zeus
I've seen a number of things in SP that might allow this; but im a little lost on how they work

I was able to get it to work with this:

Syntax: Select all

@SayFilter
def _say_filter(command, index, team_only):
if index is 0:
pass
else:
msg = ' '.join(command)
sender = Player(index)
SayText2(f"PREFIX {sender.name}: {msg}").send()
return False
return True


But makes it so commands don't work. So thats obviously not ideal.

I suppose what I want, is to prehook the player send message event, and add a string on front of the thing it renders to the clients. Is this easy to do?

Re: Adding a prefix to chat

Posted: Sun Apr 15, 2018 1:46 pm
by satoon101
I would think the best way would be to use a HookUserMessage. If you want to add colors to it, you'll likely have to build the message yourself from the values in the hook.

Re: Adding a prefix to chat

Posted: Sun Apr 15, 2018 3:17 pm
by Zeus
Nice! That does work much better:

Syntax: Select all

@HookUserMessage("SayText2")
def on_say(recpient, message_data):
if message_data['index'] == 0:
return True

sender = Player(message_data['index'])
rank = f"{WHITE}[{GOLD}Admin{WHITE}]"

message_data['message'] = f"{rank} {TEAM_COLORS[sender.team]}{sender.name}{WHITE}: {message_data['param2']}"

Re: Adding a prefix to chat

Posted: Sun Apr 15, 2018 3:42 pm
by satoon101
Bare in mind that if someone sends team or dead chat, you will change that message, too. It will still only be sent to the appropriate users, but the message will not look the same.

Re: Adding a prefix to chat

Posted: Sun Apr 15, 2018 6:39 pm
by Zeus
Ah yes, you are right.

Syntax: Select all

@HookUserMessage("SayText2")
def on_say(recpient, message_data):
if message_data['index'] == 0:
return True

rank = f"{GOLD}Admin{WHITE} |"
sender = Player(message_data['index'])

if message_data['message'] == "TF_Chat_All" or message_data['message'] == "TF_Chat_AllDead":
message_data['message'] = f"{TEAM_COLORS[sender.team]}{sender.name}{WHITE}: {message_data['param2']}"

elif message_data['message'] == "TF_Chat_Team" or message_data['message'] == "TF_Chat_Team_Dead":
message_data['message'] = f"{TEAM_COLORS[sender.team]}(TEAM) {sender.name}{WHITE}: {message_data['param2']}"

Re: Adding a prefix to chat

Posted: Wed Apr 18, 2018 3:39 am
by Zeus
So hooking that function; this keeps happening to me maybe every few hours:


Syntax: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
File "../addons/source-python/packages/source-python/messages/hooks.py", line 204, in _pre_user_message_begin
tmp_recipients = make_object(BaseRecipientFilter, args[1])

ValueError: Pointer is NULL.


This causes a fatal error that crashes srcds

Re: Adding a prefix to chat

Posted: Wed Apr 18, 2018 5:05 am
by Ayuto
What's the output of "sp info"?

Re: Adding a prefix to chat

Posted: Wed Apr 18, 2018 9:40 pm
by Zeus
Ayuto wrote:What's the output of "sp info"?


Code: Select all

IMPORTANT: Please copy the full output.
--------------------------------------------------------
Checksum      : 32dec6fa49b2fd255c92735c8b764986
Date          : 2018-04-18 21:39:54.491542
OS            : Linux-4.13.0-1012-gcp-x86_64-with-debian-stretch-sid
Game          : tf2
SP version    : 646
Github commit : 3aee743f47727579a0bf2b013d54b4d4ba0aff4c
Server plugins:
   00: Metamod:Source 1.10.7-dev
   01: Source.Python, (C) 2012-2018, Source.Python Team.
SP plugins:
   00: patron
--------------------------------------------------------

Re: Adding a prefix to chat

Posted: Thu Apr 19, 2018 5:28 pm
by Ayuto
Does this also happen without Metamod/Sourcemod?

Re: Adding a prefix to chat

Posted: Thu Apr 19, 2018 8:02 pm
by Zeus
Not sure; I can test that tonight. I do have a sorucemod plugin that is controlling bots and having them occasionally drop lines in chat. Maybe one of those plugins are causing an issue?

Though it seems to happen randomly and i don't know how to replicate the issue.