I'm just gonna say it right away that I have no experience whatsoever in python. I'm trying to make a bot for a private Discord server that posts a random image (.jpg) from a ftp server (root directory) after typing '$gimme'. #file names are random jibberish
I've searched for hours to find a solution but I always get stuck at something. I can't figure out the syntax of ftp in conjunction with discord since my knowledge of python is as close to non-existent as it gets and no matter how much I search for an answer I can't figure it out.
This is really my last option, I have nowhere else to look for. I hope someone with a little bit more knowledge than me can help me out.
Thank You
import os
import requests
import ftplib
from ftplib import FTP
import random
client = discord.Client()
ftp = FTP()
ftp.connect(os.getenv('BOP_IP'), 2021)
ftp.login(os.getenv('BOP_UN'), os.getenv('BOP_PW'))
#path='/'
@client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$gimme'):
await message.channel.send(#######)
client.run(os.getenv('TOKEN'))
EDIT with the callback method
I know python and FTP not discord but by looking at the docs I found how to send a file in discord. I made the following code (without test). I add a function which randomly get a file from FTP the server and call this function in the on_message function
import os
import requests
import ftplib
from ftplib import FTP
import random
import discord
client = discord.Client()
ftp = FTP()
ftp.connect(os.getenv('BOP_IP'), 2021)
ftp.login(os.getenv('BOP_UN'), os.getenv('BOP_PW'))
#path='/'
def download_random_file()
file_list = ftp.nlst()
random_file_name = random.choice(file_list)
#download the file
with open(random_file_name, 'wb') as fp:
ftp.retrbinary('RETR {0}'.format(random_file_name), fp.write)
return random_file_name
@client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$gimme'):
file_downloaded = download_random_file()
await message.channel.send(file=discord.File(file_downloaded))
os.unlink(file_downloaded) #Delete the downloaded file
client.run(os.getenv('TOKEN'))
To optimize :