discord.py - 发送文件而不在我的计算机上创建文件?
discord.py - Sending file without creating one on my computer?
所以,我有一个 JSON 文件,我们称它为 my_file.json
,其中包含一些信息:
[
{
"user": 0,
"infos": []
},
{
"user": 1,
"infos": []
}
]
在我的 BOT 上,我想创建一个函数来访问用户 ID 的 "infos"
:
import json
@bot.command()
async def getinfo(ctx, *, user_id):
user_id = int(user_id)
with open("my_file.json") as f:
for _ in json.load(f):
if _["user"] == user_id:
# HERE I WANT TO SEND THE CONTENTS OF "infos" OF THE
# CORRESPONDING USER ID IN A FILE, EACH LIST ITEM SEPERATED BY
# A NEWLINE
所以我希望BOT发送包含相应用户id的"infos"
列表中所有项目的文件,每一项都用换行符分隔,但我不希望文件是保存在我的电脑上。这可能吗?
我相信 StringIO 就是您要找的。
示例:
有可能!
密钥正在使用 StringIO 而不是常规文件。
解决方案
from os import linesep
import io
import json
from discord import File
@bot.command()
async def getinfo(ctx, *, user_id):
user_id = int(user_id)
with open("my_file.json") as fdata:
for _ in json.load(fdata):
if _["user"] == user_id:
f = io.StringIO(linesep.join(_["info"]))
await ctx.channel.send(content="info", file=File(fp=f, filename="user_info.txt"))
旁注
您的数据结构可能不正确,如果您可以在 json 文件中以这种方式组织数据:
{
"0": {"info": []},
"1": {"info": []},
...
}
那么您的问题的解决方案既更容易编写代码又更快 运行,因为通过将您的用户放入由他们的 ID 索引的字典中,您将不必遍历列表来查找您想要的用户:
from os import linesep
import io
import json
from discord import File
@bot.command()
async def getinfo(ctx, *, user_id):
with open("my_file.json") as fdata:
users = json.load(fdata)
if user_id in users:
f = io.StringIO(linesep.join(users[user_id]["info"]))
await ctx.channel.send(content="info", file=File(fp=f, filename="user_info.txt"))
EDIT1: 在旁注解决方案中使用字符串作为键而不是 int
EDIT2:添加 \r\n 以在 Windows
上添加功能性换行符
EDIT3:现在使用 os.linesep 在每个 OS
上获取功能换行符
所以,我有一个 JSON 文件,我们称它为 my_file.json
,其中包含一些信息:
[
{
"user": 0,
"infos": []
},
{
"user": 1,
"infos": []
}
]
在我的 BOT 上,我想创建一个函数来访问用户 ID 的 "infos"
:
import json
@bot.command()
async def getinfo(ctx, *, user_id):
user_id = int(user_id)
with open("my_file.json") as f:
for _ in json.load(f):
if _["user"] == user_id:
# HERE I WANT TO SEND THE CONTENTS OF "infos" OF THE
# CORRESPONDING USER ID IN A FILE, EACH LIST ITEM SEPERATED BY
# A NEWLINE
所以我希望BOT发送包含相应用户id的"infos"
列表中所有项目的文件,每一项都用换行符分隔,但我不希望文件是保存在我的电脑上。这可能吗?
我相信 StringIO 就是您要找的。
示例:
有可能!
密钥正在使用 StringIO 而不是常规文件。
解决方案
from os import linesep
import io
import json
from discord import File
@bot.command()
async def getinfo(ctx, *, user_id):
user_id = int(user_id)
with open("my_file.json") as fdata:
for _ in json.load(fdata):
if _["user"] == user_id:
f = io.StringIO(linesep.join(_["info"]))
await ctx.channel.send(content="info", file=File(fp=f, filename="user_info.txt"))
旁注
您的数据结构可能不正确,如果您可以在 json 文件中以这种方式组织数据:
{
"0": {"info": []},
"1": {"info": []},
...
}
那么您的问题的解决方案既更容易编写代码又更快 运行,因为通过将您的用户放入由他们的 ID 索引的字典中,您将不必遍历列表来查找您想要的用户:
from os import linesep
import io
import json
from discord import File
@bot.command()
async def getinfo(ctx, *, user_id):
with open("my_file.json") as fdata:
users = json.load(fdata)
if user_id in users:
f = io.StringIO(linesep.join(users[user_id]["info"]))
await ctx.channel.send(content="info", file=File(fp=f, filename="user_info.txt"))
EDIT1: 在旁注解决方案中使用字符串作为键而不是 int
EDIT2:添加 \r\n 以在 Windows
EDIT3:现在使用 os.linesep 在每个 OS
上获取功能换行符