Skip to content

Instantly share code, notes, and snippets.

@zeratax
Created September 24, 2017 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeratax/46bf59252459bd8745c7906411563ce3 to your computer and use it in GitHub Desktop.
Save zeratax/46bf59252459bd8745c7906411563ce3 to your computer and use it in GitHub Desktop.
resends deleted discord messages cos some people really over do it...
import discord
import asyncio
from datetime import datetime
import time
client = discord.Client()
@client.event
async def on_ready():
if not hasattr(client, 'uptime'):
client.uptime = datetime.utcnow()
await set_profile()
oauth = await get_oauth_url()
users = len(set(client.get_all_members()))
servers = len(client.servers)
# outputs general info about the bot
print('------')
print('Logged in as')
print(client.user.name)
print(client.user.id)
print(oauth)
print('serving {0} users on {1} servers'.format(users, servers))
print('------')
@client.event
async def on_message_delete(message):
channel = message.channel
user = message.author.mention
time = pretty_date(time=datetime_from_utc_to_local(message.timestamp))
org_message = message.content
if message.attachments:
for attachment in message.attachments:
org_message += attachment["url"] + ', '
org_message = org_message[:-2]
text = "{} said {}: {}".format(user, time, org_message)
await client.send_message(channel, text)
async def set_profile():
await client.edit_profile(username="Tsukimoto")
async def get_oauth_url():
try:
data = await client.application_info()
except Exception as e:
return "Couldn't get invite link"
return discord.utils.oauth_url(data.id)
def datetime_from_utc_to_local(utc_datetime):
now_timestamp = time.time()
offset = datetime.fromtimestamp(now_timestamp) - datetime.utcfromtimestamp(now_timestamp)
return utc_datetime + offset
def pretty_date(time=False):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
"""
from datetime import datetime
now = datetime.now()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):
diff = now - time
elif not time:
diff = now - now
second_diff = diff.seconds
day_diff = diff.days
if day_diff < 0:
return "just now"
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str(int(second_diff / 60)) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str(int(second_diff / 3600)) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days ago"
if day_diff < 31:
return str(int(day_diff / 7)) + " weeks ago"
if day_diff < 365:
return str(int(day_diff / 30)) + " months ago"
return str(int(day_diff / 365)) + " years ago"
if __name__ == '__main__':
client.run("token")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment