首页 文章

Discord.py沉默命令

提问于
浏览
3

我最近一直在询问有关discord.py的大量问题,这是其中之一 .

有时有些人会对你的不和谐服务器发送垃圾邮件,但踢它们或者禁止它们似乎过于苛刻 . 我有一个 silence 命令的想法,它将在一段时间内删除某个 Channels 上的每条新消息 .

到目前为止我的代码是:

@BSL.command(pass_context = True)
async def silence(ctx, lenghth = None):
        if ctx.message.author.server_permissions.administrator or ctx.message.author.id == ownerID:
             global silentMode
             global silentChannel
             silentChannel = ctx.message.channel
             silentMode = True
             lenghth = int(lenghth)
             if lenghth != '':
                  await asyncio.sleep(lenghth)
                  silentMode = False
             else:
                  await asyncio.sleep(10)
                  silentMode = False
        else:
             await BSL.send_message(ctx.message.channel, 'Sorry, you do not have the permissions to do that @{}!'.format(ctx.message.author))

我的 on_message 部分中的代码是:

if silentMode == True:
        await BSL.delete_message(message)
        if message.content.startswith('bsl;'):
                await BSL.process_commands(message)

使用的所有变量都是在机器人顶部预先定义的 .

我的问题是机器人删除了它有权访问的所有通道中的所有新消息 . 我尝试将 if silentChannel == ctx.message.channel 放在 on_message 部分,但这使命令完全停止工作 .

关于为什么会发生这种情况的任何建议都非常感谢 .

1 回答

  • 1

    就像是

    silent_channels = set()
    
    @BSL.event
    async def on_message(message):
        if message.channel in silent_channels:
            if not message.author.server_permissions.administrator and  message.author.id != ownerID:
                await BSL.delete_message(message)
                return
        await BSL.process_commands(message)
    
    @BSL.command(pass_context=True)
    async def silent(ctx, length=0): # Corrected spelling of length
        if ctx.message.author.server_permissions.administrator or ctx.message.author.id == ownerID:
            silent_channels.add(ctx.message.channel)
            await BSL.say('Going silent.')
            if length:
                length = int(length)
                await asyncio.sleep(length)
                if ctx.message.channel not in silent_channels: # Woken manually
                    return
                silent_channels.discard(ctx.message.channel)
                await BSL.say('Waking up.')
    
    @BSL.command(pass_context=True)
    async def wake(ctx):
        silent_channels.discard(ctx.message.channel)
    

    应该工作(我没有测试过,测试机器人是一种痛苦) . 通过集搜索很快,因此对每条消息执行此操作不应该是您资源的真正负担 .

相关问题