2017-10-06 65 views
0

用戶在這裏是我的代碼如何檢查是否不和諧機器人是輸入命令

import discord 
import asyncio 
import sys 
import commands 
import pickle 
import os 
import random 
import array 
#if len(sys.argv) != 2: 
# print('Usage: python3 main.py [token]') 

commandz=("yes","no","hi","hi") 
try: 
    with open(os.path.join(os.path.dirname(__file__), "token.pickle"), 'rb') as file: 
     token = pickle.load(file) 
    if len(token) == 59: 
     key = int(random.random() * 10000000000000000) 
     print('Found saved token in stored.py, use phrase tokenreset'+str(key), 'to undo this.')#youll need to add the command 
    else: 
     raise 
except: 
    #code tokenreset with admin permissions 
    token = input('What is your Discord bot token? (found on Discord developer page): ') 
    with open(os.path.join(os.path.dirname(__file__), "token.pickle"), 'wb') as file: 
     pickle.dump(token, file) 
client = discord.Client() 

@client.event 
async def on_ready(): 
    print('Logged in as' + '[' + client.user.id + ']' + client.user.name) 
    print('--------') 


@client.event 
async def on_message(message): 
     await handle_command(message) 


async def handle_command(message): 
    print('Noticed: ' + message.content) 
    if message.content == 'tokenreset'+str(key): 
     await client.send_message(message.channel, 'code accepted') 
    i = 0 
    if str(message.author) == "NOTAKOALAINVENEZUELA#6895": 
     i = 1 
    x = 0 
    await client.send_message(message.channel, message.author) 
    while i == 0: 
     if commandz[x] in message.content: 
      x = x + 1 
      await client.send_message(message.channel, commandz[x]) 
      i = i + 1 
     else: 
      if x == len(commandz) - 2: 
       i = i + 1 
      else: 
       x = x + 2 
client.run(token) 

結果是我的機器人一遍又一遍,我已經試過各種觸發其自己的命令得到它不承認它的自己的消息,我在我的智慧結束。

我認爲由於某種原因message.author是一些不能用於比較的奇怪類型的變量,我對編程和python超級新,所以我不確定。

+0

您發佈的代碼不會運行。請仔細檢查所有需要它自己的行的東西都有它,並且所有內容都正確縮進。 –

+0

是的當發佈的格式搞砸了編輯 – trisimix

回答

0

你可以嘗試檢查如果作者是用這個機器人:

if message.author.id === message.server.me.id 

這樣可以確保機器人不會觸發本身。

+0

這工作100%。你是怎麼弄出來的? – trisimix

+0

@trisimix你看過github頁面的'examples'文件夾嗎? https://github.com/Rapptz/discord.py/blob/async/examples/reply.py –

+0

@trisimix,我用它作爲我的機器人,但我認爲你可以在例子或一些隨機教程中看到它。另外,如果它工作,隨時給我正確的答案thingy :) –