Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Deleting messages that start with certain prefix [discord.py]

I want my bot to delete all messages that are not starting with the prefix .. I wrote this code so far in discord.py but it keeps deleting all messages.

@Client.event
async def on_message(message):
    if message.author.has_role("Bot"):
        return
    elif message.content.startswith('.'):
        return
    else:
        await message.delete()

Thanks in advance


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

1 Answer

0 votes
by (71.8m points)

Try doing it like this:

@client.event
async def on_message(ctx):
    if ctx.content.startswith('.'):
        await client.process_commands(ctx)
        return

    for role in ctx.author.roles:
        if role.name == 'Bot':
            return
    
    await ctx.delete()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...