Discord.py silence command(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 静音命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord.py 静音命令


- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01