使Discord BOT从ftp服务器获取随机图像(Filezilla)

2024-10-02 22:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我要说的是,我在python方面没有任何经验。我正在尝试为私有Discord服务器创建一个bot,该服务器在键入“$gimme”后从ftp服务器(根目录)发布一个随机图像(.jpg)#文件名是随机的

我找了几个小时想找到一个解决办法,但我总是被一些事情卡住。我无法理解ftp和discord的语法,因为我对python的了解几乎不存在,无论我如何搜索答案,我都无法理解

这真的是我最后的选择,我没有别的地方可找了。我希望比我多一点知识的人能帮助我

多谢各位

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

Tags: import服务器clienteventmessageasyncosdef
3条回答

@axelinux

非常感谢你花这么多时间来帮助我

在服务器上运行“$gimme”时出现以下错误

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 31, in on_message
    file_downloaded = download_random_file()
  File "main.py", line 17, in download_random_file
    ftp.retrbinary('RETR {0}'.format(random_file_name)) #download the file
TypeError: retrbinary() missing 1 required positional argument: 'callback'

@axelinux

再次谢谢你

在discord中键入“$gimme”后,您发送给我的修订代码返回了以下内容:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 34, in on_message
    file_downloaded = download_random_file()
  File "main.py", line 20, in download_random_file
    ftp.retrbinary('RETR {0}'.format(random_file_name), fp.write)
  File "/usr/lib/python3.8/ftplib.py", line 425, in retrbinary
    with self.transfercmd(cmd, rest) as conn:
  File "/usr/lib/python3.8/ftplib.py", line 382, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/usr/lib/python3.8/ftplib.py", line 348, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "/usr/lib/python3.8/ftplib.py", line 275, in sendcmd
    return self.getresp()
  File "/usr/lib/python3.8/ftplib.py", line 248, in getresp
    raise error_perm(resp)
ftplib.error_perm: 550 File not found

我已经做了一些搜索,我想我现在已经成功了:

def download_random_repo():
 ftp.cwd('/repo')
 file_list = ftp.nlst()
 random_file_name = random.choice(file_list)
 ftp.retrbinary('RETR {0}'.format(random_file_name), open(random_file_name, 'wb').write) #download the file
 return random_file_name

我所要做的就是将行更改为:ftp.retrbinary('RETR{0}'。format(random_file_name),open(random_file_name,'wb')。write)

非常感谢你的帮助。如果没有你,它就无法工作:)

使用回调方法编辑

我知道python和FTP不是discord,但通过查看文档,我发现了如何在discord中发送文件。我编写了以下代码(未经测试)。我添加了一个从FTP服务器随机获取文件的函数,并在on_message函数中调用该函数

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

要优化:

  • 确保文件已下载(使用try-catch块)
  • 确保下载的文件是图像(带有MIMETYPE)

相关问题 更多 >