让 Discord BOT 从 ftp 服务器 (Filezilla) 获取随机图像
Make Discord BOT get random image from a ftp server (Filezilla)
我只想说我在 python 方面没有任何经验。我正在尝试为私有 Discord 服务器制作一个机器人,该机器人在输入“$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'))
使用回调方法编辑
我知道 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)
@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' ).写)
非常感谢您的帮助。没有你就无法工作:)
我只想说我在 python 方面没有任何经验。我正在尝试为私有 Discord 服务器制作一个机器人,该机器人在输入“$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'))
使用回调方法编辑
我知道 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)
@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' ).写)
非常感谢您的帮助。没有你就无法工作:)