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

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

问题描述

我最近问了很多关于 discord.py 的问题,这就是其中之一.

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

有时有些人会向您的不和谐服务器发送垃圾邮件,但踢或禁止他们似乎过于严厉.我想到了一个 silence 命令,它会在给定的时间内删除频道上的每条新消息.

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 部分中的代码是:

The code in my on_message section is:

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 messages 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天全站免登陆