2017-06-08 8 views
0

私はこのコードを持っています。基本的には、「ADMIN」または「semi-mod」というロールを持つ人物が、 '!log user'と言ってください。このコードは、メッセージを.txtファイルに入れます。このファイルの名前は、ログに記録されるユーザーのIDです。ユーザー固有のIDを持つtxtファイルが見つからない場合は、新しいものを作成します。しかし、私の問題は、私は一人以上を記録した場合、メッセージは他のユーザーのテキストファイルに行くことであるなど文字列が右のtxtファイルに入っていない[discord.py]

メッセージがあるため、あなたがループに追加し、全体 users2log配列を介して他のユーザのファイルに追加されます
@commands.command(pass_context=True) 
    async def log(self, ctx, user: discord.Member): 
     if 'Administrator' or 'semi-mod' in [x.name for x in ctx.message.author.roles]: 
      users2log.append(user.id) 
      msg = """``` 
Member's Message Being Logged 
{} messages are now being logged at the request of {}. 
```""".format(ctx.message.mentions[0], ctx.message.author) 
      await self.bot.send_message(discord.Object(id='320289509281628165'), msg) 
      await self.bot.add_reaction(ctx.message, '\U00002611') 


----------------------- 
Part not working: 
----------------------- 


    async def on_message(self, message): 
     if message.author.id in users2log: 
      for user in users2log: 
       try: 
        f = open(user, 'a') 
        msg = """ 
User: {} 
Time: {} 
Message: {} 
\n""".format(message.author, time.localtime(), message.content) 
        f.write(msg) 
        f.close() 
       except FileNotFoundError: 
        os.system("touch " + user) 

答えて

1

1つのユーザーのファイルにのみメッセージを記録したい場合でも、各ファイルに適用されます。あなたはon_messageにforループを削除する場合それはとてもあなただけのユーザーのIDと一致するファイルに追加動作するはずです:

async def on_message(self, message): 
    if message.author.id in users2log: 
     user = message.author.id 
     try: 
      f = open(user, 'a') 
      msg = """ 
User: {} 
Time: {} 
Message: {} 
\n""".format(message.author, time.localtime(), message.content) 
      f.write(msg) 
      f.close() 
     except FileNotFoundError: 
      os.system("touch " + user) 
+0

ありがとうございました。できます。 – hkyq

関連する問題