lewando54
lewando54

Reputation: 37

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

I'm still learning python and programming and i've got myself into a problem that i can't solve. I want to make a command that would make a bot send an image that its name corresponds to number what user wrote (e.g. user wrote "!image_nr 22" and bot sends 22.jpg). I've only made code that sends random image from folder but I cant get into chosen. Here's my latest code for this problem:

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

Upvotes: 0

Views: 4424

Answers (2)

You could try inside this elif statement:

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

now it should send the user requested photo.

e.g. !obrazeknr image_nr 22

Hope this helps. Sorry for the long wait; I just saw this today.

Might be a better idea, for next time, posting on https://programming.stackoverflow.com could give you more help.

Upvotes: 1

lewando54
lewando54

Reputation: 37

It works. I've slightly modified it and it works. Thx for idea :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")

Upvotes: 1

Related Questions