Skip to content

Instantly share code, notes, and snippets.

@mikeshardmind
Last active February 10, 2022 18:21
Show Gist options
  • Select an option

  • Save mikeshardmind/6445102a697e60fbd5473d6f4d1aaab6 to your computer and use it in GitHub Desktop.

Select an option

Save mikeshardmind/6445102a697e60fbd5473d6f4d1aaab6 to your computer and use it in GitHub Desktop.

Revisions

  1. mikeshardmind revised this gist Aug 8, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions retrocensor.py
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    except ImportError:
    raise RuntimeError(
    "You need the rewrite of discord.py for this script"
    "python3 -m pip install https://github.com/Rapptz/discord.py/archive/8ccb98d395537b1c9acc187e1647dfdd07bb831b.zip#egg=discord.py-1.0.0a"
    "python3 -m pip install discord.py -U
    )

    log = logging.getLogger()
    @@ -109,9 +109,9 @@ async def retroactiveregexcensor(ctx: commands.Context, *, pattern: str):
    if not discord.__version__.startswith("1.0."):
    print("You need the rewrite version of discord.py for this script.")
    sys.exit(1)
    try:
    TOKEN = os.environ.get("CENSORSHIPTOKEN")
    except:

    TOKEN = os.environ.get("CENSORSHIPTOKEN")
    if not TOKEN:
    print(
    "You need to run this with your token "
    'stored in an environment var named "CENSORSHIPTOKEN"'
  2. mikeshardmind created this gist Apr 20, 2019.
    123 changes: 123 additions & 0 deletions retrocensor.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    #!/usr/bin/env python3
    """
    This is free and unencumbered software released into the public domain.
    Anyone is free to copy, modify, publish, use, compile, sell, or
    distribute this software, either in source code form or as a compiled
    binary, for any purpose, commercial or non-commercial, and by any
    means.
    In jurisdictions that recognize copyright laws, the author or authors
    of this software dedicate any and all copyright interest in the
    software to the public domain. We make this dedication for the benefit
    of the public at large and to the detriment of our heirs and
    successors. We intend this dedication to be an overt act of
    relinquishment in perpetuity of all present and future rights to this
    software under copyright law.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.
    For more information, please refer to <http://unlicense.org/>
    """
    import asyncio
    import logging
    import os
    import re

    try:
    import discord
    from discord.ext import commands
    except ImportError:
    raise RuntimeError(
    "You need the rewrite of discord.py for this script"
    "python3 -m pip install https://github.com/Rapptz/discord.py/archive/8ccb98d395537b1c9acc187e1647dfdd07bb831b.zip#egg=discord.py-1.0.0a"
    )

    log = logging.getLogger()
    log.setLevel(logging.INFO)

    bot = commands.AutoShardedBot(
    command_prefix=commands.when_mentioned,
    description="Censorship is dumb. Rewriting & erasing history is dumber. Here we are.",
    )


    async def channel_cleaner(
    ctx: commands.Context, channel: discord.TextChannel, pattern: re.Pattern
    ):

    count = 0
    async for message in channel.history(limit=None):
    if message.content and pattern.match(message.content):
    try:
    _id = message.id
    url = message.jump_url
    await message.delete()
    except discord.HTTPException:
    log.info(f"Could not delete message {url}")
    else:
    count += 1
    log.info(f"Deleted message {_id}")

    await ctx.send(f"No more books to burn in {channel.mention} (burned {count})")


    @commands.bot_has_permissions(manage_messages=True)
    @commands.guild_only()
    @commands.is_owner()
    @bot.command()
    async def retroactivecensor(ctx: commands.Context, *, phrase: str):
    """
    Will delete tons of messages. Case insensitive.
    """

    pattern = re.compile(re.escape(phrase), re.I)
    tasks = [
    channel_cleaner(ctx, c, pattern)
    for c in ctx.guild.text_channels
    if c.permissions_for(ctx.guild.me).manage_messages
    ]

    await asyncio.gather(*tasks)
    await ctx.send("I'm done burning books.")


    @commands.bot_has_permissions(manage_messages=True)
    @commands.guild_only()
    @commands.is_owner()
    @bot.command()
    async def retroactiveregexcensor(ctx: commands.Context, *, pattern: str):
    """
    Takes a regex pattern. Filters all the messages which match it.
    Not ReDoS safe, make your regex carefully to avoid pathalogical expansions.
    """

    pattern = re.compile(pattern)
    tasks = [
    channel_cleaner(ctx, c, pattern)
    for c in ctx.guild.text_channels
    if c.permissions_for(ctx.guild.me).manage_messages
    ]

    await asyncio.gather(*tasks)
    await ctx.send("I'm done burning books.")


    if __name__ == "__main__":
    if not discord.__version__.startswith("1.0."):
    print("You need the rewrite version of discord.py for this script.")
    sys.exit(1)
    try:
    TOKEN = os.environ.get("CENSORSHIPTOKEN")
    except:
    print(
    "You need to run this with your token "
    'stored in an environment var named "CENSORSHIPTOKEN"'
    )
    sys.exit(1)
    print(
    "I hate that this is a neccessary tool, but here we are. Use with caution and discretion."
    )
    bot.run(TOKEN)