Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

python - Discord.py silence command

I have been asking lots 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))

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...