Skip to content

Instantly share code, notes, and snippets.

@sgqy
Created March 27, 2019 14:20
Show Gist options
  • Save sgqy/41c61a28c50981a8fede3be572419c7d to your computer and use it in GitHub Desktop.
Save sgqy/41c61a28c50981a8fede3be572419c7d to your computer and use it in GitHub Desktop.
delete all messages by you from all discord servers
#!/usr/bin/env python3
# Erase all messages by you from all servers.
# Consider use this script before deleting account.
# modified from https://github.com/elevenchars/discorddelete/blob/master/main.py
# This is a self-bot script. Use at your own risk.
# https://support.discordapp.com/hc/en-us/articles/115002192352
import json
import requests
import urllib
import time
import colorama
import sys
## prepare global vars
class ApiSession(requests.Session):
def __init__(self, url_base=None, *args, **kwargs):
super(ApiSession, self).__init__(*args, **kwargs)
self.url_base = url_base
def request(self, method, url, **kwargs):
api_url = urllib.parse.urljoin(self.url_base, url)
print(colorama.Style.BRIGHT + colorama.Fore.GREEN + method + ' ' + api_url + colorama.Style.RESET_ALL)
return super(ApiSession, self).request(method, api_url, **kwargs)
s = ApiSession(url_base='https://discordapp.com/api/v6/')
s.headers.update({'authorization': 'mfa.H_0000000000000000011111111111111111112222222222222222222222333333333333333333333333'})
#s.proxies.update({'http': 'http://127.0.0.1:8118', 'https': 'http://127.0.0.1:8118' })
uid = ''
## remove messages
def get_msg(ch, count):
msg = json.loads(s.get('channels/' + ch + '/messages', params = {'limit': 100}).content)
ret = [m for m in msg if m['author']['id'] == uid]
while True:
last = sorted(msg, key=lambda x: x['timestamp'], reverse=True)[-1]['id']
msg = json.loads(s.get('channels/' + ch + '/messages', params = {'limit': 100, 'before': last}).content)
ret += [m for m in msg if m['author']['id'] == uid]
print('... {}/{}'.format(len(ret), count))
if len(msg) < 100:
print('reach top: {}'.format(len(msg)))
return ret
if len(ret) >= count:
print('... cut')
return ret
def delete_msg(ch, msg, total):
cnt = 0
for m in msg:
cnt += 1
print(str(cnt) + '/' + str(total) + '\t' + m['content'])
## export all messages
#with open(m['channel_id'] + '-' + str(cnt), 'w') as of:
# of.write(json.dumps(m, ensure_ascii=False, indent=2, sort_keys=True))
# do delete
resp = 429
while not resp == 204:
resp = s.delete('channels/' + ch + '/messages/' + m['id']).status_code
if cnt >= total:
break
def get_guild_channel():
ret = []
guilds = json.loads(s.get('users/@me/guilds').content)
for g in guilds:
channels = json.loads(s.get('guilds/' + g['id'] + '/channels').content)
for ch in channels:
search = json.loads(s.get('guilds/' + g['id'] + '/messages/search?author_id=' + uid + '&channel_id=' + ch['id'] + '&include_nsfw=true').content)
if search.get('total_results'):
ret.append( (ch, search['total_results']) )
return ret
def get_dm_channel():
ret = []
channels = json.loads(s.get('users/@me/channels').content)
for ch in channels:
search = json.loads(s.get('channels/' + ch['id'] + '/messages/search?author_id=' + uid).content)
if search.get('total_results'):
ret.append( (ch, search['total_results']) )
return ret
## Do not use or you may be blocked!
##
## def unhidden_dm_from_guild_member():
## ret = []
## guilds = json.loads(s.get('users/@me/guilds').content)
## for g in guilds:
## members = json.loads(s.get('guilds/' + g['id'] + '/members', params={'limit': 1000}).content)
## for m in members:
## # create DM
## ch = json.loads(s.post('users/@me/channels', json={'recipient_id': m['user']['id']}).content)
## time.sleep(1)
## # test if msg exists
## search = json.loads(s.get('channels/' + ch['id'] + '/messages/search?author_id=' + uid).content)
## time.sleep(1)
## # confirm or remove
## if search.get('total_results'):
## ret.append( (ch, search['total_results']) )
## else:
## s.delete('channels/' + ch['id'])
## time.sleep(1)
## return ret
user = json.loads(s.get('users/@me').content)
uid = user['id']
channels = get_guild_channel()
channels += get_dm_channel()
for ch,cnt in channels:
print('{}\t{}\t{}'.format(ch['id'], ch.get('name'), cnt))
msg = get_msg(ch['id'], cnt)
delete_msg(ch['id'], msg, cnt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment