Discord.py静音命令 [英] Discord.py silence command

查看:145
本文介绍了Discord.py静音命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been asking loads of questions lately about discord.py and this is one of them.

有时候,有些人向您的不和谐服务器发送垃圾邮件,但是踢或禁止他们似乎太苛刻了。我想到了沉默命令的想法,该命令将在给定的时间内删除频道上的每条新消息。

Sometimes there are those times when some people spam your discord server but kicking or banning them seems too harsh. I had the idea for a silence command which would delete every new message on a channel for a given amount of time.

到目前为止我的代码是:

My code so far is:

@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)

所有使用的变量都在机器人的顶部。

All the variables used are pre-defined at the top of the bot.

我的问题是,该漫游器会删除其有权访问的所有频道中的所有新消息。我尝试将 if silentChannel == ctx.message.channel 放在 on_message 部分中,但这使命令完全停止工作

My problem is that the bot deletes all new message in all channels which it has access to. I tried putting if silentChannel == ctx.message.channel in the on_message section but this made the command stop working completely.

任何有关为什么发生这种情况的建议都将受到赞赏。

Any suggestions as to why this is happening are much appreciated.

推荐答案

类似

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)

应该工作(我还没有测试过,测试机器人很痛苦。快速搜索集合,因此对每条消息进行搜索都不会对您的资源造成真正的负担。

Should work (I haven't tested it, testing bots is a pain). Searching through sets is fast, so doing it for every message shouldn't be a real burden on your resources.

这篇关于Discord.py静音命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆