Discord.py - 在 DM 上发送文本文件
Discord.py - Sending a text file on DM
我制作了一个 python 程序,它使用 2 个不同的参数连接到 API,它要求输入,然后将结果导出到文本文件。
我想把它变成一个不和谐的机器人,它会做完全相同的事情,但现在会在使用该命令的用户的 DM 上发送所述文件。
原文联系API的部分是:
# Setting the search type input
api.set_type(method)
# Getting the search query input
query = input("Please enter your search query ")
# Setting the search query input
api.set_query(query)
# Getting the results
results = api.lookup()
关于导出,我使用的是:
with open("results.txt", "w") as f:
for dictionary in results:
f.write(dictionary['line'] + "\n")
在第一段代码中,您可以看到我在请求输入。不需要它,因为机器人将从命令中获取它,我通过让机器人发送带有参数的消息来测试它是否正确并且它正确发送所有内容。
这是该消息的代码:
@bot.command(name='search', help='Search for an e-mail, password or keyword')
async def args(ctx, arg1, arg2):
await ctx.send('You sent {} and {}'.format(arg1, arg2))
如果需要机器人的完整代码,这是我目前拥有的代码:
TOKEN = ""
bot = commands.Bot(command_prefix='!')
@bot.command(name='search', help='Search for an e-mail, password or keyword')
async def args(ctx, arg1, arg2):
await ctx.send('You sent {} and {}'.format(arg1, arg2))
arg1 = "login"
arg2 = "teste"
# Now onto implementing and using the API
api = LeakCheckAPI()
api.set_key("")
ip = api.getIP()
limits = api.getLimits()
api.set_type(arg1) # setting the search method
api.set_query(arg2) # setting what we should search
results = api.lookup() # getting the results
bot.run(TOKEN)
行 arg1 = "login" 和 arg2 = "teste" 仅用于测试,因为我收到错误消息“NameError : name 'arg1' is not defined”,出于某种奇怪的原因,它也没有从消息中正确提取参数。
如果你想在文件中写入一些文本并将这个文件发送给用户,你可以使用这个例子:
@client.command(pass_context=True)
async def sendtext(ctx, arg1, arg2):
# write to file
with open("result.txt", "w") as file:
file.write('arg1 = {0}, arg2 = {1}'.format(arg1, arg2))
# send file to Discord in message
with open("result.txt", "rb") as file:
await ctx.send("Your file is:", file=discord.File(file, "result.txt"))
结果:
P.S。文档中关于 discord.File 的信息
我制作了一个 python 程序,它使用 2 个不同的参数连接到 API,它要求输入,然后将结果导出到文本文件。
我想把它变成一个不和谐的机器人,它会做完全相同的事情,但现在会在使用该命令的用户的 DM 上发送所述文件。
原文联系API的部分是:
# Setting the search type input
api.set_type(method)
# Getting the search query input
query = input("Please enter your search query ")
# Setting the search query input
api.set_query(query)
# Getting the results
results = api.lookup()
关于导出,我使用的是:
with open("results.txt", "w") as f:
for dictionary in results:
f.write(dictionary['line'] + "\n")
在第一段代码中,您可以看到我在请求输入。不需要它,因为机器人将从命令中获取它,我通过让机器人发送带有参数的消息来测试它是否正确并且它正确发送所有内容。 这是该消息的代码:
@bot.command(name='search', help='Search for an e-mail, password or keyword')
async def args(ctx, arg1, arg2):
await ctx.send('You sent {} and {}'.format(arg1, arg2))
如果需要机器人的完整代码,这是我目前拥有的代码:
TOKEN = ""
bot = commands.Bot(command_prefix='!')
@bot.command(name='search', help='Search for an e-mail, password or keyword')
async def args(ctx, arg1, arg2):
await ctx.send('You sent {} and {}'.format(arg1, arg2))
arg1 = "login"
arg2 = "teste"
# Now onto implementing and using the API
api = LeakCheckAPI()
api.set_key("")
ip = api.getIP()
limits = api.getLimits()
api.set_type(arg1) # setting the search method
api.set_query(arg2) # setting what we should search
results = api.lookup() # getting the results
bot.run(TOKEN)
行 arg1 = "login" 和 arg2 = "teste" 仅用于测试,因为我收到错误消息“NameError : name 'arg1' is not defined”,出于某种奇怪的原因,它也没有从消息中正确提取参数。
如果你想在文件中写入一些文本并将这个文件发送给用户,你可以使用这个例子:
@client.command(pass_context=True)
async def sendtext(ctx, arg1, arg2):
# write to file
with open("result.txt", "w") as file:
file.write('arg1 = {0}, arg2 = {1}'.format(arg1, arg2))
# send file to Discord in message
with open("result.txt", "rb") as file:
await ctx.send("Your file is:", file=discord.File(file, "result.txt"))
结果:
P.S。文档中关于 discord.File 的信息