Adding a prefix to chat

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Adding a prefix to chat

Postby Zeus » Sat Apr 14, 2018 10:55 pm

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?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Adding a prefix to chat

Postby satoon101 » Sun Apr 15, 2018 1:46 pm

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.
Image
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: Adding a prefix to chat

Postby Zeus » Sun Apr 15, 2018 3:17 pm

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']}"
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Adding a prefix to chat

Postby satoon101 » Sun Apr 15, 2018 3:42 pm

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.
Image
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: Adding a prefix to chat

Postby Zeus » Sun Apr 15, 2018 6:39 pm

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']}"
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: Adding a prefix to chat

Postby Zeus » Wed Apr 18, 2018 3:39 am

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
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Adding a prefix to chat

Postby Ayuto » Wed Apr 18, 2018 5:05 am

What's the output of "sp info"?
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: Adding a prefix to chat

Postby Zeus » Wed Apr 18, 2018 9:40 pm

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
--------------------------------------------------------
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Adding a prefix to chat

Postby Ayuto » Thu Apr 19, 2018 5:28 pm

Does this also happen without Metamod/Sourcemod?
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: Adding a prefix to chat

Postby Zeus » Thu Apr 19, 2018 8:02 pm

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.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 21 guests