如何使用不和谐重写的特定频道

How to use a specific channel with discord rewrite

我有这个机器人可以告诉您某个用户在给定时间内在当前频道中发送的消息数量,但我希望能够指定检查来自哪个频道的消息以及不仅仅是现在的。你会怎么做?

到目前为止我的脚本:

import discord
from discord.ext import commands
from datetime import datetime, timedelta


class AdminCommands(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command(aliases=["stats", "activity", "messages"])
    @commands.has_permissions(manage_messages=True)
    async def check(self, ctx, duration=7, specified_channel=None, *, user):
        async with ctx.channel.typing():
            msg = await ctx.channel.send('Calculating...')
            await msg.add_reaction('')

            counter = 0
            # I want to change this line:  
            async for message in ctx.channel.history(limit=5000, after=datetime.today() - timedelta(days=duration)):
                if str(message.author) == str(user):
                    counter += 1

            await msg.remove_reaction('', member=message.author)
            if counter == 5000:
                await msg.edit(content=f'{user} has sent over 5000 messages in this channel in {duration} days!')
            else:
                await msg.edit(content=f'{user} has sent {str(counter)} message(s) in this channel in {duration} days.')


def setup(client):
    client.add_cog(AdminCommands(client))

我该如何做到这一点,以便当用户进入频道时它会检查该频道而不是消息发送到的频道?

您可以指定参数类型:

async def check(self, ctx, duration=7, channel: discord.TextChannel=None, *, user: discord.Member=None):
    if not channel:
        channel = ctx.channel
    if not user:
        user = ctx.author
    # rest of your code

这是将参数类型设置为其各自的 discord 对象,这样您就可以访问它们的属性。

在示例中我还添加了条件,如果没有指定用户,则默认为命令发送者,如果没有指定频道,则使用命令的频道。


参考文献: