The plugin will support @, @@(from a player to admins), csay, hsay...
Plugin:
Syntax: Select all
# ----- Imports ---------
# ----- Source.Python ---
from config.manager import ConfigManager
from commands.say import SayCommand, SayFilter
from commands.typed import TypedSayCommand, CommandReturn
from colors import Color
from cvars.public import PublicConVar
from messages import DialogMsg, SayText2, HintText, TextMsg, KeyHintText
from players.entity import Player
from players.helpers import index_from_name
from plugins.info import PluginInfo
# ----- Plugin info -----
info = PluginInfo()
info.author = 'Kill'
info.basename = 'adminchat'
info.name = 'Admin Chat'
info.description = 'Admin chat.'
info.version = '0.1'
PublicConVar(info.basename + '_version', info.version, info.description)
# ---- Configs ----------
# plugin_cvar = ConfigManager(info.basename)
TSAY_COLOR = Color(245, 245, 0, 255)
TSAY_COMMANDS = ['!tsay', '/tsay']
HSAY_COMMANDS = ['!hsay', '/hsay']
CSAY_COMMANDS = ['!csay', '/csay']
KSAY_COMMANDS = ['!ksay', '/ksay']
PSAY_COMMANDS = '!psay'
# booleans
CSAY_SHOW_ADMIN_NAME = True
HSAY_SHOW_ADMIN_NAME = True
TSAY_SHOW_ADMIN_NAME = True
KSAY_SHOW_ADMIN_NAME = True
# admin flag required
ADMIN_CHAT_FLAG = "admin.chat"
# Admin string
ADMIN_STRING = "ADMIN" # Translate this
# --- Messages ---
# error msg
NO_PERMISSION_ERROR = SayText2("You don't have permission to execute this command.")
# and translate this... and everything
# ---- admins only @@ ---
@SayFilter
def _say_filter(command, index, team_only):
if index is 0: # console
pass
else:
msg = ' '.join(command)
at = msg.startswith('@@')
msg_to_send = msg[2:]
if at and team_only:
SayText2("\x04(ADMINS ONLY)\x01 \x03{}\x01\n".format(
msg_to_send
)).send()
return False
return True
# --- @ admin say -------
# TODO: Should we use TypedClientCommand instead of a filter?
@SayFilter
def _say_filter(command, index, team_only):
msg = ' '.join(command)
at = msg.startswith('@')
msg_to_send = msg[1:]
if index is 0:
SayText2("\x04(ADMIN)\x01 \x03{msg_to_send}\x01\n".format(
msg_to_send=msg_to_send
)).send()
else:
if at and has_permission(index):
SayText2("\x04(ADMIN)\x01 \x03{msg_to_send}\x01\n".format(
msg_to_send=msg_to_send
)).send()
return False
return True
# ---- !tsay ------------
@SayCommand(TSAY_COMMANDS)
def _command_hsay(command, index, team_only):
msg_join = ' '.join(command)
msg_start = msg_join.startswith('/')
if not has_permission(index):
NO_PERMISSION_ERROR.send(index)
else:
if TSAY_SHOW_ADMIN_NAME:
admin = Player(index).name
else:
admin = ADMIN_STRING
msg = msg_join[5:]
DialogMsg('{admin}: {msg}'.format(
admin=admin,
msg=msg
), color=TSAY_COLOR, time=5).send()
if msg_start:
return CommandReturn.BLOCK
else:
return CommandReturn.CONTINUE
# ---- !hsay ------------
@SayCommand(HSAY_COMMANDS)
def _command_hsay(command, index, team_only):
msg_join = ' '.join(command)
msg_start = msg_join.startswith('/')
if not has_permission(index):
NO_PERMISSION_ERROR.send(index)
else:
if CSAY_SHOW_ADMIN_NAME:
admin = Player(index).name
else:
admin = ADMIN_STRING
msg = msg_join[5:]
HintText('{admin}: {msg}'.format(
admin=admin,
msg=msg
)).send()
if msg_start:
return CommandReturn.BLOCK
else:
return CommandReturn.CONTINUE
# ---- !csay ------------
@SayCommand(CSAY_COMMANDS)
def _command_csay(command, index, team_only):
msg_join = ' '.join(command)
msg_start = msg_join.startswith('/')
if has_permission(index) is False:
NO_PERMISSION_ERROR.send(index)
else:
if CSAY_SHOW_ADMIN_NAME:
admin = Player(index).name
else:
admin = ADMIN_STRING
msg = msg_join[5:]
TextMsg('{admin}: {msg}'.format(
admin=admin,
msg=msg
)).send()
# silence command
if msg_start:
return CommandReturn.BLOCK
else:
return CommandReturn.CONTINUE
# ---- !ksay ------------
@SayCommand(KSAY_COMMANDS)
def _command_csay(command, index, team_only):
msg_join = ' '.join(command)
msg_start = msg_join.startswith('/')
if has_permission(index, ADMIN_CHAT_FLAG) is False:
NO_PERMISSION_ERROR.send(index)
else:
if KSAY_SHOW_ADMIN_NAME:
admin = Player(index).name
else:
admin = ADMIN_STRING
msg = msg_join[5:]
KeyHintText('{admin}: {msg}'.format(
admin=admin,
msg=msg
)).send()
# silence command
if msg_start:
return CommandReturn.BLOCK
else:
return CommandReturn.CONTINUE
# ---- !psay ------------
@TypedSayCommand(PSAY_COMMANDS, permission=ADMIN_CHAT_FLAG)
def _command_psay(command, recipient, *msg):
admin = Player(command.index)
if recipient[0:1] is '#':
try:
recipient = Player.from_userid(int(recipient[1:]))
except ValueError: # Does it really return a ValueError?
SayText2("Can't find player with {userid}.").send(command.index, userid=recipient)
else:
try:
recipient = index_from_name(recipient)
recipient = Player(recipient)
except ValueError:
SayText2("Can't find player {name}.".format(
name=recipient
)).send()
return CommandReturn.BLOCK
msg = ' '.join(msg)
SayText2('\x04(PRIVATE: {recipient_name}) {admin}: {msg}\x01'.format(
recipient_name=recipient.name,
admin=admin.name,
msg=msg
)).send([recipient.index, command.index])
return CommandReturn.BLOCK
def has_permission(index, flag=ADMIN_CHAT_FLAG):
if index is 0:
return
else:
player = Player(index)
admin_level = flag in player.permissions
return admin_level
# return True or False