2017-08-31 50 views
0

我仍在學習python和編程,我已經陷入了一個我無法解決的問題。我想製作一個命令,讓機器人發送一個名字與用戶編寫的數字相對應的圖像(例如,用戶編寫了「!image_nr 22」和bot發送22.jpg)。我只做了從文件夾發送隨機圖像的代碼,但我無法進入選擇狀態。這是我最新的這個問題代碼:如何使不和諧機器人發送圖像與用戶選擇的號碼的名稱? (Python 3.5.x)

elif message.content.startswith("!obrazeknr"): #missing an argument or something, idk what to make here 
     obrazDirectoryChc = "C:/Users/Lewando54/Downloads/Localisation/English/" + liczba + ".jpg" 
     await client.send_file(message.channel, obrazDirectoryChc, content=obrazName[1]) 

回答

0

你可以試試這個elif的語句中:

msg = message.content[12:] #skips the command word and the '!' and the " " 

msg = msg.split() #split the message into an array at the spaces. 
#msg = ["!image_nr","22"] 

if msg[0] == "!image_nr" and msg[1].isnumeric() == True: 

obrazDirectoryChc = "C:/Users/Lewando54/Downloads/Localisation/English/" + 
    liczba + ".jpg" 
await client.send_file(message.channel, obrazDirectoryChc, 
    content=obrazName[int(msg[1]]) 

現在應該向用戶發送請求的照片。

例如!obrazeknr image_nr 22

希望這會有所幫助。抱歉等了很久,我今天才看到這個。

可能是一個更好的主意,下次發佈在programming.stackoverflow.com 它可以給你更多的幫助。

+0

它的工作。我稍微修改了它,它可以工作。想法的Thx:D – lewando54

0

它的工作原理。我稍微修改了它,它可以工作。 Thx的想法:D

elif message.content.startswith('!obrazeknr'): 
    msg1 = message.content #skips the command word and the '!' and the " " 
    msg1 = msg1.split() #split the message into an array at the spaces. 
    #msg = ["!obrazeknr","22"] 
    if msg1[0] == "!obrazeknr" and msg1[1].isnumeric() == True: 
     await client.send_file(message.channel, "C:/Users/Lewando54/Downloads/Localisation/English/" + str(int(msg1[1])) + ".jpg") 
相關問題