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

时间:2020-06-13 14:20:14

标签: python bots discord discord.py-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))

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

1 个答案:

答案 0 :(得分:0)

您可以指定参数类型:

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

这是将参数类型设置为其各自的不和谐对象,使您可以访问其属性。

在示例中,我还添加了以下条件:如果未指定用户,则默认为命令发送者;如果未指定通道,则使用命令的通道。


参考: