Discord.py静音命令
最近,我一直在询问有关discord.py的大量问题,这就是其中之一。
有时候,有些人向您的不和谐服务器发送垃圾邮件,但踢或禁止它们似乎太苛刻了。我想到了一个silence
命令,该命令将在给定的时间内删除通道上的所有新消息。
到目前为止,我的代码是:
@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)
所有使用的变量都在bot的顶部预先定义。
我的问题是,该漫游器会删除其有权访问的所有频道中的所有新消息。我尝试放入if silentChannel ==
ctx.message.channel
该on_message
部分,但这使命令完全停止工作。
关于为什么发生这种情况的任何建议都将受到赞赏。
-
就像是
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)
应该可以工作(我还没有测试过,测试机器人很痛苦)。在集合中快速搜索,因此对每条消息进行搜索都不会真正给您的资源带来负担。