Server, Client, and Say Commands

Post Python examples to help other users.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Server, Client, and Say Commands

Postby satoon101 » Tue Jan 15, 2013 6:05 am

I just added a new update to the command structure. Previously, the Python-side API only allowed the used of decorators to register/unregister the commands. With this update, you can now use a registration class (which the decorators now tie into) to register your commands:

  • ServerCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [description]
      4. [flags]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.server import ServerCommandRegistry

      def server_command_callback(CCommand):
      print('Test command just called')

      ServerCommandRegistry.register_command('test_command', server_command_callback, 'Test command')

      def unload():
      ServerCommandRegistry.unregister_command('test_command', server_command)
  • ClientCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [level]
      4. [permission]
      5. [flag]
      6. [fail_callback]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.client import ClientCommandRegistry

      def client_command_callback(edict, CCommand):
      print('Test client command just called')

      ClientCommandRegistry.register_command('client_test', client_command_callback)

      def unload():
      ClientCommandRegistry.unregister_command('client_test', client_command_callback)
  • SayCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [level]
      4. [permission]
      5. [flag]
      6. [fail_callback]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.client import SayCommandRegistry

      def say_command_callback(index, teamonly, CCommand):
      print('Test say command just called')

      SayCommandRegistry.register_command('say_test', say_command_callback)

      def unload():
      SayCommandRegistry.unregister_command('say_test', say_command_callback)

With ClientCommandRegistry and SayCommandRegistry, you can also register filters (though client command filters has not been added, as of yet):

Syntax: Select all

from commands.client import ClientCommandRegistry
from commands.say import SayCommandRegistry

def load():
ClientCommandRegistry.register_filter(client_command_filter)
SayCommandRegistry.register_filter(say_filter)

def client_command_filter(edict, CCommand):
print('Client Command Filter')

def say_filter(index, teamonly, CCommand):
print('Say Filter')

def unload():
ClientCommandRegistry.unregister_filter(client_command_filter)
SayCommandRegistry.unregister_filter(say_filter)


You can also, of course, use the decorators to register your commands/filters, and they will automatically be unregistered when you unload your script:

Syntax: Select all

from commands.client import ClientCommand
from commands.client import ClientCommandFilter
from commands.say import SayCommand
from commands.say import SayFilter
from commands.server import ServerCommand

@ClientCommand('client_test')
def client_command_test(edict, CCommand):
print('Client Command Test')

@ClientCommandFilter
def client_command_filter_test(edict, CCommand):
print('Client Command Filter Test')

@SayCommand('say_test')
def say_command_test(index, teamonly, CCommand):
print('Say Command Test')

@SayFilter
def say_filter_test(index, teamonly, CCommand):
print('Say Filter Test')

@ServerCommand('server_test')
def server_command_test(CCommand):
print('Server Command Test')
Again, client command filters are not implemented, so that portion of the code above will probably raise an error. All of the rest should work perfectly fine.

If you notice any bugs, have any questions/comments, please feel free to post them.

Satoon
battleweaver
Member
Posts: 43
Joined: Mon Oct 02, 2017 7:57 am
Location: Moscow
Contact:

Re: Server, Client, and Say Commands

Postby battleweaver » Tue Nov 21, 2017 10:01 am

Say, I have an endless warmup period, then i want to execute "mp_warmup_end" at server side. How can I execute this command directly from plugin?
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: Server, Client, and Say Commands

Postby BackRaw » Tue Nov 21, 2017 10:35 am

battleweaver wrote:Say, I have an endless warmup period, then i want to execute "mp_warmup_end" at server side. How can I execute this command directly from plugin?

Syntax: Select all

from engines.server import engine_server

# where you need it:
engine_server.execute_server_command('mp_warmup_end')
Should do it.
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Server, Client, and Say Commands

Postby Ayuto » Tue Nov 21, 2017 12:51 pm

Not quite.

Syntax: Select all

from engines.server import execute_server_command

# where you need it:
execute_server_command('mp_warmup_end')
battleweaver
Member
Posts: 43
Joined: Mon Oct 02, 2017 7:57 am
Location: Moscow
Contact:

Re: Server, Client, and Say Commands

Postby battleweaver » Tue Nov 21, 2017 4:05 pm

Guys, thank you very much! I was so inattentive while reading wiki.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: Server, Client, and Say Commands

Postby BackRaw » Tue Nov 21, 2017 4:06 pm

Ayuto wrote:Not quite.

Syntax: Select all

from engines.server import execute_server_command

# where you need it:
execute_server_command('mp_warmup_end')

Whoops :D
battleweaver
Member
Posts: 43
Joined: Mon Oct 02, 2017 7:57 am
Location: Moscow
Contact:

Re: Server, Client, and Say Commands

Postby battleweaver » Wed Nov 22, 2017 10:44 am

A problem though.
It worked correctly, but server did not recognize the other command: execute_server_command('bot_kick all')
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Server, Client, and Say Commands

Postby Ayuto » Wed Nov 22, 2017 3:58 pm

IIRC, it should raise an exception, because "bot_kick all" is not a server command, but a server command plus an argument. Try this instead:

Syntax: Select all

execute_server_command('bot_kick', 'all')
battleweaver
Member
Posts: 43
Joined: Mon Oct 02, 2017 7:57 am
Location: Moscow
Contact:

Re: Server, Client, and Say Commands

Postby battleweaver » Thu Nov 23, 2017 10:28 am

Ayuto wrote:IIRC, it should raise an exception, because "bot_kick all" is not a server command, but a server command plus an argument. Try this instead:

Syntax: Select all

execute_server_command('bot_kick', 'all')

Test case:
"sp_version" = "607", deathmatch gamemode

Syntax: Select all

from commands.typed import TypedSayCommand
from messages import SayText2
from engines.server import execute_server_command

@TypedSayCommand('!test1')
def cmd_on_test1(command_info):
execute_server_command('bot_kick') #enough to kick all bots
SayText2(f"Bots kicked").send()

@TypedSayCommand('!test2')
def cmd_on_test2(command_info):
execute_server_command('exec', 'my.cfg') #config is located in ../csgo/cfg/
SayText2(f"Config executed").send()

@TypedSayCommand('!test3')
def cmd_on_test3(command_info):
execute_server_command('mp_warmup_end')
SayText2(f"Warmup ended").send()


Output:
All test cases returned SayText to user, but only test2 executed server command.

This is most confusing, because i used

Syntax: Select all

execute_server_command('mp_warmup_end')
in other places of my code, and it works fine.
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Server, Client, and Say Commands

Postby Ayuto » Thu Nov 23, 2017 6:47 pm

Hmm, the immediate execution of the server command doesn't seem to work in certain cases. In this case use queue_server_command.
battleweaver
Member
Posts: 43
Joined: Mon Oct 02, 2017 7:57 am
Location: Moscow
Contact:

Re: Server, Client, and Say Commands

Postby battleweaver » Fri Nov 24, 2017 12:41 pm

Ayuto wrote:Hmm, the immediate execution of the server command doesn't seem to work in certain cases. In this case use queue_server_command.

Works as you predicted. Thanks a lot!

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 16 guests