简体   繁体   中英

discord.py block advertisment isnt working

I have recently started learning discord.py, and I am trying to remove certain links when they get posted in a channel. Please see my below code.

@Bot.event
async def on_message(message):
  if "discord.gg" in message.content.lower():
    await message.delete()
    await message.channel.send("Luften Reklam Yapmaurn!")
  await Bot.process_commands(message)

Here I have made a very basic link or word filter, it will look through the whole message for anything containing the specific key words specified below;

Depending if you also want to allow bots to send links, just remove the if message.author.bot: if not, keep it otherwise people may find they are limited to commands by other bots as they can't vote, etc. And depending on what links you want filtered can easily be customized in the "link_filter" array just below.

link_filter = [".com", "http", "https:", ".gg", "discord."]


@client.listen('on_message')
async def message(message, member: discord.Member = None):

    if message.author.bot:
        return
        
    for word in link_filter:
        if message.content.count(word) > 0:
            await message.channel.purge(limit=1)

            embed = discord.Embed(
                    
            colour = discord.Colour.red())
                
            embed.set_footer(text=f"No links please!")
            embed.add_field(name=f"NO links!", value=(message.author.mention) + ', Your message was deleted because it contained a link.', inline=False)
            await message.channel.send(embed=embed, delete_after=10)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM