我要说的是,我对 Python 没有任何经验。我正在尝试为私人 Discord 服务器制作一个机器人,该服务器在键入“$gimme”后从 ftp 服务器(根目录)发布随机图像(.jpg)。 #文件名是随机的乱码
我已经搜索了几个小时来寻找解决方案,但我总是遇到一些问题。我无法弄清楚 ftp 与 discord 的语法,因为我对 Python 的了解几乎不存在,无论我搜索多少答案,我都无法弄清楚。
这真的是我最后的选择,我无处可寻。我希望有比我多一点知识的人可以帮助我。
谢谢
#include <iostream>
#include <map>
#include <variant>
#include <vector>
#include <string>
int main(){
typedef std::map<std::string, std::variant<int, double, float, std::string>> Dict;
std::vector<Dict> params = {
{
{"label", "Labs"},
{"dir", "/media/sf_Data/"},
{"num_frames", 4},
{"scale_factor", 1.0},
}, // Dict 0
{
{"label", "Airplanes"},
{"dir", "/media/m_result/"},
{"num_frames", 5},
{"scale_factor", 0.5},
} // Dict 1
};
int idx = 0;
std::string label = std::get<std::string>(params[idx]["label"]);
std::string folder = std::get<std::string>(params[idx]["dir"]);
int num_frames = std::get<int>(params[idx]["num_frames"]);
double scf = std::get<double>(params[idx]["scale_factor"]);
std::cout << label << std::endl;
std::cout << folder << std::endl;
std::cout << num_frames << std::endl;
std::cout << scf << std::endl;
return 0;
}
答案 0 :(得分:0)
使用回调方法编辑
我知道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'))
优化:
答案 1 :(得分:0)
@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'
答案 2 :(得分:0)
@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 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)
非常感谢您的帮助。没有你就不能工作:)