Admin Chat

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Kill
Member
Posts: 88
Joined: Wed Aug 31, 2016 10:05 pm

Admin Chat

Postby Kill » Fri Nov 04, 2016 3:26 pm

I started working on this, but since I'm quite new to Python, and Source.Python, I'd love to hear from You guys, and make this better.
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
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Admin Chat

Postby iPlayer » Fri Nov 04, 2016 3:31 pm

I would recommend using TypedSayCommand instead of regular one. It supports permissions out-of-the-box, handles the case with invalid permissions, and has a really convenient way of providing you the arguments.

http://wiki.sourcepython.com/developing/module_tutorials/commands.html#using-permissions
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Kill
Member
Posts: 88
Joined: Wed Aug 31, 2016 10:05 pm

Re: Admin Chat

Postby Kill » Fri Nov 04, 2016 3:44 pm

iPlayer wrote:I would recommend using TypedSayCommand instead of regular one. It supports permissions out-of-the-box, handles the case with invalid permissions, and has a really convenient way of providing you the arguments.

http://wiki.sourcepython.com/developing/module_tutorials/commands.html#using-permissions


Thanks! I know about it, but I don't like the way it shows the error.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Admin Chat

Postby iPlayer » Fri Nov 04, 2016 3:58 pm

SP wiki wrote:You can also add a callback that gets called when an unauthorized player tries to execute the command.


Syntax: Select all

from commands.typed import TypedClientCommand

...

def on_test_failed(command_info, args):
NO_PERMISSION_ERROR.send(command_info.index)

@TypedSayCommand(TSAY_COMMANDS, ADMIN_CHAT_FLAG, on_test_failed)
def say_tsay(command_info, msg:str):
...
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Kill
Member
Posts: 88
Joined: Wed Aug 31, 2016 10:05 pm

Re: Admin Chat

Postby Kill » Fri Nov 04, 2016 4:07 pm

iPlayer wrote:
SP wiki wrote:You can also add a callback that gets called when an unauthorized player tries to execute the command.


Syntax: Select all

from commands.typed import TypedClientCommand

...

def on_test_failed(command_info, args):
NO_PERMISSION_ERROR.send(command_info.index)

@TypedSayCommand(TSAY_COMMANDS, ADMIN_CHAT_FLAG, on_test_failed)
def say_tsay(command_info, msg:str):
...


Thanks, Noted!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 6 guests