2017-02-05 22 views
1

の中に配置されたコードをcommand.processCommandオブジェクトに当てたときに実行したいとき(私がdefined[]の中に配置されたすべてのコマンドをループしているとき) ?この前述のループはmyproject.py変数が呼び出されるときにプロセスを実行

command.py

class Command: 
    global defined 

    defined = [] 

    def __init__(self, name): 
     self.name = name 
     self.description = "This command lacks a description" 
     self.args = "" 
     self.process = None 

     defined.append(self) 

eightball.py

def processCommand(): 
     print('hello') 

    thing = commands.Command('8ball') 
    thing.description = "Gives you a response from the mighty 8ball." 
    thing.process = processCommand 

myproject.py

# Cogs 
import cogs.commands as commands 
import cogs.eightball 
import cogs.helloworld 

def processCommands(message): 
    if(message.content[:2] == "b#"): 
     args = message.content.split(' ') 

     args[0] = args[0][2:] 

     for command in defined: 
      if args[0] == command.name: 
       command.args = args 
       command.processCommand 
+0

アドオン '()'、それを実行するために名前を機能させる必要がある - すなわち。 'thing.process()' – furas

+0

私は明確にする必要があります、私はメインクラスからインポートするときにコマンドを実行したくないです。 – Jake

+0

あなたは起動時にそれをする必要はありませんが、必要なときには、実行方法や場所を表示しませんでした。 – furas

答えて

1
for x in defined: 
    if x.process: # to skip `self.process = None` 
     x.process() 

にようになっているように実行されます

EDIT:あなたがprocess()代わりのprocessCommand

for command in defined: 
     if args[0] == command.name: 
      command.args = args 
      command.process() 
関連する問題