2016-07-24 7 views
-1

Discord Botのために書いたこのコードは動作しません。DiscordボットPythonミニゲームで書かれたループwhile not working

def russian_roulette(author, message): 
    game_active = True 
    client.send_message(message.channel, "Russian Roulette game started.6 chambers. 1 loaded.\nType $spin to spin the chamber.\nType $pull to pull the trigger.") 
    while game_active == True: 
     if message.content.startswith('$spin'): 
      chamber = randint(1,6) 
      client.send_message(message.channel, "%s spins the chambers." % author) 
     if message.content.startswith('$pull'): 
      if chamber == 1: 
       client.send_message(message.channel, "%s pulled the trigger and was not lucky. R.I.P." % author) 
       game_active = False 
      else: 
       client.send_message(message.channel, "%s pulled the trigger and nothing happened." % author) 
+1

何が問題なのですか? [Minimal、Complete、Verifiable](http://stackoverflow.com/help/mcve)の例を投稿して、問題が正確に何かを説明してください – James

+0

@Jamesこの関数を実行すると、3行目のコードを次のように出力します。私はそれを期待していましたが、whileループに到達してコマンドに入力すると何も起こりません。 MCVEを入れないと申し訳ありません – Ziepo

+0

私はdiscord APIの仕組みや実装が分かりませんが、メッセージ変数や作成者変数を無限にループして更新することはないように見えます。無限に車輪。無限にループするのではなく、何らかの種類のリスナー関数を作成またはメッセージに添付する必要があります。 – James

答えて

0

私はあなたがthis API wrapper for discordを使用していると仮定しています。

この場合、デコレータclient.eventで関数を作成して、メッセージに正しく応答する必要があります。これと同じように:

@client.event 
async def on_message(message): 
    if message.content.startswith('$spin'): 
      chamber = randint(1,6) 
      client.send_message(message.channel, "%s spins the chambers." % author) 
    if message.content.startswith('$pull'): 
     if chamber == 1: 
      client.send_message(message.channel, "%s pulled the trigger and was not lucky. R.I.P." % message.author) 
      game_active = False 
     else: 
      client.send_message(message.channel, "%s pulled the trigger and nothing happened." % message.author) 

この機能は、あなたのボットは、むしろ機能は一度だけ呼び出されますここで、あなたのコードに比べて、新しいメッセージを受信するたびに呼び出されます。

私はこれまでこのAPIを使用したことがなく、単にドキュメントと例を読むだけです。これは間違いかもしれません。

+0

コードの実装を読んで何かをクリックして動作させました。ありがとうございました。 – Ziepo