Skip to content

Instantly share code, notes, and snippets.

@itsJlot
Last active June 18, 2017 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsJlot/78864c4fc27fe98551d918b1660c5353 to your computer and use it in GitHub Desktop.
Save itsJlot/78864c4fc27fe98551d918b1660c5353 to your computer and use it in GitHub Desktop.
Minimal version of discord running in Commandline. Potentially useful for weak pcs or machines with operating systems without GUI support. requires discord.py and python3
# -*- coding: utf-8 -*-
import discord
import getpass, sys,re, datetime, time as t
cmds = {"help" : "help()","servers" : "list_servers()", "logout" : "break",
"send" : "send_message()", "chat" : "choose_chat()"}
cmds_no_param = {"help" : "help()","servers" : "listServers()", "logout" : "break" }
client = discord.Client()
def millisecs():
return int((datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() * 1000)
def help():
print("All commands: \n\t" + "\n\t".join([i for i in cmds]))
def list_servers():
x = -1
for i in client.servers:
x += 1
print(str(x) + ":\t " + i.name)
def list_channels(server):
x = -1
for i in server.channels:
x+=1
print(str(x) + ":\t" + i.name)
def list_pms():
x = -1
for i in client.private_channels:
x+=1
print(str(x) + ":\t" + ", ".join([j.name for j in list(i.recipients)]))
def choose_pm(pm = -1):
if pm < 0:
list_pms()
pm = input("pm number: ")
pm = max(min(int(pm), len(list(client.private_channels))), 0)
return list(client.private_channels)[pm]
def choose_chat(server = -1,channel = -1):
if server < 0:
list_servers()
server = input("Server number: ")
server = max(min(int(server), len(list(client.servers))), 0)
list_channels(list(client.servers)[server])
if channel < 0:
channel = input("channel number: ")
chkeys = [i for i in list(client.servers)[server].channels]
channel = max(min(int(channel), len(chkeys)), 0)
return list(list(client.servers)[server].channels)[channel]
async def send_message(channel):
await client.send_message(channel,input("insert your message: "))
async def show_messages(channel,count = 20):
async for x in client.logs_from(channel,count,reverse=True):
print(x.timestamp.isoformat(' ', 'minutes'))
x.content = re.sub("[^a-zA-Z0-9,./;:'\"-_+=\(\)&^%$#@!<> ]","?",x.content)
print(" by " + x.author.name + ": " + x.content)
async def save_messages(channel,count = 20):
b = ""
a = client.logs_from(channel, limit=count)
async for message in a:
b += re.sub("[^/a-zA-Z .?!_+-=0-9#]", "?", message.author.display_name) + ": " + re.sub("[^/a-zA-Z .?!0-9#]", "", message.content) + "\n"
open(channel.server.name + "X" + channel.name + ".txt", "w").write(b)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
channel = choose_chat(0,0)
strrepr = channel.server.name + ".#" + channel.name
scheduled = {}
scheduledc = {}
while True:
for i in scheduled:
if int(i) < int(t.time()*1000):
print(str(i) + " > " + str(int(t.time()*1000)))
await client.send_message(scheduledc[i],scheduled[i])
cmd = input(strrepr + ": ").split(" ")
if cmd[0] == "logout":
break
elif cmd[0] == "send":
await send_message(channel)
elif cmd[0] == "chat":
s = -1
c = -1
if len(cmd) == 3:
s = cmd[1]
c = cmd[2]
elif len(cmd) == 2:
s = cmd[1]
channel = choose_chat(s,c)
strrepr = channel.server.name + ".#" + channel.name
elif cmd[0] == "read":
amount = 20
if len(cmd) > 1:
amount = int(cmd[1])
await show_messages(channel,amount)
elif cmd[0] == "pm":
channel = choose_pm()
strrepr = ", ".join([j.name for j in list(channel.recipients)])
elif cmd[0] == "schedule":
time = input("How long should the message be delayed?: ")
millis = 0
ptrn = {"h":3600*1000,"m":60*1000,"d":24*3600*1000,"s":1000}
for i in ptrn:
if i in time:
units = re.findall("["+i.upper() + i.lower() + "]\d*[" + i.upper() + i.lower() + "]", time)
for u in units:
u = re.sub("[" + i.lower() + i.upper() + "]","",u)
millis += ptrn[i]*int(u)
scheduled[int(t.time()*1000+millis)] = input("What do you want to send?: ")
scheduledc[int(t.time() * 1000 + millis)] = channel
elif cmd[0] == "save":
await save_messages(channel,100000)
else:
if cmd[0] not in cmds_no_param:
cmd[0] = "help"
else:
eval(cmds_no_param[cmd[0]])
else:
try:
await client.logout()
except RuntimeError:
sys.exit(0)
client.run(input("e-Mail: "),getpass.getpass("Password: "))
Copy link

ghost commented May 31, 2017

👍

@vladfrangu
Copy link

wewlad 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment