如何记录来自频道的消息?

时间:2021-01-30 14:41:22

标签: python discord discord.py discord.py-rewrite

我正在尝试创建一个日志命令,当您键入“y!log”时,它会记录 100 条消息,然后当您输入一个数量时,它会记录该数量,然后将所有这些消息的文件发送给您,但问题是,它不起作用哈哈,有人可以帮助我吗?我不知道我是否尽我所能解释了它,但希望这足以帮助! :D

顺便说一句,它是齿轮,所以是的。

    @commands.command()
    async def log(self, ctx, amount=0):
        with open("data.json") as f:
            data = json.load(f)
        
        valid_user = False

        for role_id in data["verified-roles"]:
            try:
                if ctx.guild.get_role(role_id) in ctx.author.roles:
                    valid_user = True
            except:
                pass

        if ctx.message.author.guild_permissions.administrator or valid_user or ctx.message.author.guild_permissions.manage_messages:
            try:
                limit = int(amount)
            except:
                return

            if not amount:
                messages = await ctx.channel.history(limit=100)
                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 ctx.message.delete()
                await ctx.author.send(f"All messages you logged will be here:", file=file)
                await ctx.send(f"{ctx.author.mention} logged previous **{limit}** messages in channel.", delete_after=5)

            elif amount:
                messages = await ctx.channel.history(limit=limit)
                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 ctx.message.delete()
                await ctx.author.send(f"All messages you logged will be here:", file=file)
                await ctx.send(f"{ctx.author.mention} logged previous **100** messages in channel.", delete_after=5)

0 个答案:

没有答案