Discord.py文件日志记录,更改消息数
我有这段代码,我用它来记录discord频道。它基本上获取一个通道中的最后10000条消息,并生成一个包含这些消息的.txt文件,使用!日志命令。然而,我如何使它成为用户可以选择键入一个数字后!日志,它将记录以前的消息数?例如:“!log”在它自己的日志中记录最后10000条消息,但“!log 100”将记录最后100条消息。但我不知道该怎么做 这是我的密码:Discord.py文件日志记录,更改消息数,discord.py,Discord.py,我有这段代码,我用它来记录discord频道。它基本上获取一个通道中的最后10000条消息,并生成一个包含这些消息的.txt文件,使用!日志命令。然而,我如何使它成为用户可以选择键入一个数字后!日志,它将记录以前的消息数?例如:“!log”在它自己的日志中记录最后10000条消息,但“!log 100”将记录最后100条消息。但我不知道该怎么做 这是我的密码: @commands.has_any_role('Logger', 'logger') @bot.command() async def
@commands.has_any_role('Logger', 'logger')
@bot.command()
async def log(ctx):
try:
channel = ctx.message.channel
await ctx.message.delete()
await channel.send(ctx.message.author.mention+" Creating a log now. This may take up to 5 minutes, please be patient.")
messages = await ctx.channel.history(limit=10000).flatten()
numbers = "\n".join([f"{message.author}: {message.clean_content}" for message in messages])
f = BytesIO(bytes(numbers, encoding="utf-8"))
file = discord.File(fp=f, filename='Log.txt')
await channel.send(ctx.message.author.mention+" Message logging has completed.")
await channel.send(file=file)
except:
embed = discord.Embed(title="Error creating normal log:", description="The bot doesn't have necessary permissions to make this log type. Please confirm the bot has these permissions for this channel: View Channel, Read and Send Messages, Attatch Files (used for sending the log file), Manage Messages (used for deleting the command the user sends when making a log).", color=0xf44336)
await channel.send(embed=embed)
任何关于如何让他们可以选择提供一个数字的帮助,然后记录这个数字,都会有很大帮助。谢谢 将要记录的消息数作为参数
。。。
@bot.command()
异步def日志(ctx,限制:int=1000):
...
然后使用参数的值
。。。
messages=wait ctx.channel.history(limit=limit).flatten()
...
docs:这行不通,
limit
参数是一个字符串,TextChannel.history
只接受一个整数作为限制值。此外,您还应将默认值设置为itlimit,现在它的类型为int
,并且limit的默认值为1000,现在它应该可以工作了。