2020-04-15 19:54:29 -04:00
import discord
import io
import sys
client = discord . Client ( )
2020-10-05 15:14:49 -04:00
def mentions_me ( message ) :
return client . user in message . mentions or [ client . user ] in [ role . members for role in message . role_mentions ]
2020-04-15 19:54:29 -04:00
@client.event
async def on_ready ( ) :
print ( ' logged in as {0.user} ' . format ( client ) , file = sys . stderr )
@client.event
async def on_message ( message ) :
if message . author == client . user :
return
2020-10-05 15:14:49 -04:00
if ( ( ' spoil ' in message . content . lower ( ) or mentions_me ( message ) )
2020-04-15 19:54:29 -04:00
and message . attachments
and not any ( a . is_spoiler ( ) for a in message . attachments ) ) :
attachments = [ ]
try :
for a in message . attachments :
f = await a . to_file ( )
f . filename = ' SPOILER_ ' + f . filename
attachments . append ( f )
2020-11-16 18:27:25 -05:00
except discord . DiscordException as e :
2020-04-15 19:54:29 -04:00
print ( ' error reading attachments: {0} ' . format ( e ) , file = sys . stderr )
return
try :
await message . channel . send ( " {0} says: {1} " . format ( message . author . mention , message . content ) , files = attachments )
2020-11-16 18:27:25 -05:00
except discord . DiscordException as e :
2020-04-15 19:54:29 -04:00
print ( ' error sending message: {0} ' . format ( e ) , file = sys . stderr )
return
try :
await message . delete ( )
2020-11-16 18:27:25 -05:00
except discord . Forbidden as e :
print ( ' error deleting message: {0} ' . format ( e ) )
await message . channel . send ( " I can ' t delete the original unspoiled message because I don ' t have the permissions I need. Admins, please make sure I have the Manage Messages permission in all channels where people want to use me. " )
return
except discord . DiscordException as e :
2020-04-15 19:54:29 -04:00
print ( ' error deleting message: {0} ' . format ( e ) )
return
2020-10-05 15:14:49 -04:00
elif mentions_me ( message ) :
2021-03-23 22:45:28 -04:00
await message . channel . send ( " upload an image and put ' spoil ' anywhere in the description, and I ' ll spoiler it for you. to delete your post, add a :x: react to it. " )
@client.event
async def on_reaction_add ( reaction , user ) :
if ( reaction . message . author == client . user
and str ( reaction . emoji ) == ' ❌ '
and reaction . message . content . startswith ( ' {0} says: ' . format ( user . mention ) ) ) :
try :
await reaction . message . delete ( )
except discord . DiscordException as e :
print ( ' error deleting my own message: {0} ' . format ( e ) )
2020-04-15 19:54:29 -04:00
with open ( ' token ' ) as f :
token = f . read ( ) . strip ( )
client . run ( token )