如何让 Discord bot 发送带有用户选择的号码名称的图像? (Python 3.5.x)

How to make Discord bot send image with the name of number chosen by user? (Python 3.5.x)

我仍在学习 python 和编程,但我遇到了无法解决的问题。我想做一个命令,让机器人发送一个图像,其名称对应于用户写的数字(例如,用户写了“!image_nr 22”,机器人发送 22.jpg)。我只编写了从文件夹发送随机图像的代码,但我无法进入选择状态。这是我针对此问题的最新代码:

 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])

您可以在这个 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():

 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

希望这对您有所帮助。抱歉让您久等了;今天刚看到。

可能是个更好的主意,下次在 https://programming.whosebug.com 上发帖可以给你更多帮助。

有效。我稍微修改了它并且它有效。谢谢你的想法: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")