Skip to content

Instantly share code, notes, and snippets.

@rioru
Created June 25, 2018 22:36
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 rioru/af8fe44d8ba182065e2161698f83e5d3 to your computer and use it in GitHub Desktop.
Save rioru/af8fe44d8ba182065e2161698f83e5d3 to your computer and use it in GitHub Desktop.
import discord
import asyncio
import sqlite3
import datetime
import random
conn = sqlite3.connect('selphy.db')
client = discord.Client()
c = conn.cursor()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
def getQuoteHelp():
em = discord.Embed(description='You can use !quote to get a random message, add a quote (either using an id or with a raw message) or search a quote', colour=0xdbd7b8)
em.set_author(name='!quote help', icon_url=client.user.avatar_url)
em.add_field(name='!quote', value='Get a random quote', inline=True)
em.add_field(name='!quote add <id>', value='Add a quote with an id (recommended), you\'ll need to be in developer mode to get a message id', inline=True)
em.add_field(name='!quote add <username> <message>', value='Add a quote with a username and the message', inline=True)
em.add_field(name='!quote search <search phrase>', value='Search a quote', inline=True)
em.add_field(name='!quote delete <id>', value='Delete a quote', inline=True)
em.add_field(name='!quote count', value='Count the number of quotes in the database', inline=True)
return em
@client.event
async def on_message(message):
if message.content.startswith('!test'):
counter = 0
tmp = await client.send_message(message.channel, 'Calculating messages...')
async for log in client.logs_from(message.channel, limit=100):
if log.author == message.author:
counter += 1
await client.edit_message(tmp, 'You have {} messages.'.format(counter))
elif message.content.startswith('!sleep'):
await asyncio.sleep(5)
await client.send_message(message.channel, 'Done sleeping')
elif message.content.startswith('!quote'):
cmd = message.content.split(" ")
if len(cmd) == 1:
query = 'SELECT * FROM quotes'
c.execute(query)
result = c.fetchall()
randomval = random.randint(0, len(result) - 1)
em = discord.Embed(description=result[randomval][5], colour=0xdbd7b8)
try:
author = await client.get_user_info(int(result[randomval][4]))
except:
author = client.user
try:
user = await client.get_user_info(result[randomval][2])
except:
user = client.user
qtype = int(result[randomval][3])
if qtype == 0:
em.set_author(name=author.name, icon_url=author.avatar_url)
else:
em.set_author(name=result[randomval][4], icon_url=author.avatar_url)
em.add_field(name='Added by', value=user.name+' at '+result[randomval][1]+' as id ['+str(result[randomval][0])+']', inline=True)
await client.send_message(message.channel, embed=em)
elif len(cmd) == 2:
if cmd[1] == 'count':
c.execute("SELECT count(*) FROM quotes")
result = c.fetchone()
await client.send_message(message.channel, 'There are currently [%s] quotes in the database.' % result)
else:
em = getQuoteHelp()
await client.send_message(message.channel, 'I couldn\'t understand your command, here\'s a quick help.', embed=em)
elif len(cmd) == 3:
if cmd[1] == 'add':# Add by Id
try:
message_add = await client.get_message(message.channel, int(cmd[2]))
c.execute("INSERT INTO quotes (date, user_id, type, author_id, message) VALUES(?, ?, ?, ?, ?)", (str(datetime.datetime.now()), int(message.author.id), 0, int(message_add.author.id), message_add.content))
conn.commit()
await client.send_message(message.channel, 'Quote added!')
query = 'SELECT * FROM quotes ORDER BY id DESC LIMIT 1'
c.execute(query)
result = c.fetchone()
em = discord.Embed(description=result[5], colour=0xdbd7b8)
try:
author = await client.get_user_info(int(result[4]))
except:
author = client.user
try:
user = await client.get_user_info(result[2])
except:
user = client.user
em.set_author(name=author.name, icon_url=author.avatar_url)
em.add_field(name='Added by', value=user.name+' at '+result[1]+' as id ['+str(result[0])+']', inline=True)
await client.send_message(message.channel, embed=em)
except:
await client.send_message(message.channel, 'Couldn\'t get your message, sorry!')
elif cmd[1] == 'delete':
try:
mesid = int(cmd[2])
except:
mesid = -1
c.execute("SELECT * FROM quotes WHERE id = ?", (mesid,))
result = c.fetchone()
if result == None:
await client.send_message(message.channel, 'Id doesn\'t exist or has already been deleted')
else:
em = discord.Embed(description=result[5], colour=0xdbd7b8)
try:
author = await client.get_user_info(int(result[4]))
except:
author = client.user
try:
user = await client.get_user_info(result[2])
except:
user = client.user
qtype = int(result[3])
if qtype == 0:
em.set_author(name=author.name, icon_url=author.avatar_url)
else:
em.set_author(name=result[4], icon_url=author.avatar_url)
em.add_field(name='Added by', value=user.name+' at '+result[1]+' as id ['+str(result[0])+']', inline=True)
await client.send_message(message.channel, 'Deleting...', embed=em)
c.execute("DELETE FROM quotes WHERE id = ?", (mesid,))
conn.commit()
await client.send_message(message.channel, 'Message deleted')
elif cmd[1] == 'search':
c.execute("SELECT * FROM quotes WHERE message LIKE ?", (str("%"+cmd[2]+"%"),))
result = c.fetchall()
if len(result) > 0:
randomval = random.randint(0, len(result) - 1)
em = discord.Embed(description=result[randomval][5], colour=0xdbd7b8)
try:
author = await client.get_user_info(int(result[randomval][4]))
except:
author = client.user
try:
user = await client.get_user_info(result[randomval][2])
except:
user = client.user
qtype = int(result[randomval][3])
if qtype == 0:
em.set_author(name=author.name, icon_url=author.avatar_url)
else:
em.set_author(name=result[randomval][4], icon_url=author.avatar_url)
em.add_field(name='Added by', value=user.name+' at '+result[randomval][1]+' as id ['+str(result[randomval][0])+']', inline=True)
await client.send_message(message.channel, embed=em)
else:
await client.send_message(message.channel, 'Couldn\'t find your search string')
elif len(cmd) == 4:
if cmd[1] == 'add':# Add by author+message
try:
c.execute("INSERT INTO quotes (date, user_id, type, author_id, message) VALUES(?, ?, ?, ?, ?)", (str(datetime.datetime.now()), int(message.author.id), 1, cmd[2], cmd[3]))
conn.commit()
await client.send_message(message.channel, 'Quote added!')
query = 'SELECT * FROM quotes ORDER BY id DESC LIMIT 1'
c.execute(query)
result = c.fetchone()
em = discord.Embed(description=result[5], colour=0xdbd7b8)
try:
author = await client.get_user_info(int(result[4]))
except:
author = client.user
try:
user = await client.get_user_info(result[2])
except:
user = client.user
em.set_author(name=result[4], icon_url=author.avatar_url)
em.add_field(name='Added by', value=user.name+' at '+result[1]+' as id ['+str(result[0])+']', inline=True)
await client.send_message(message.channel, embed=em)
except:
await client.send_message(message.channel, 'Couldn\'t add your message, sorry!')
else:
em = getQuoteHelp()
await client.send_message(message.channel, 'I couldn\'t understand your command, here\'s a quick help.', embed=em)
elif message.content.startswith('!roll'):
cmd = message.content.split(" ")
if len(cmd) != 2:
await client.send_message(message.channel, 'Wrong argument number!')
else:
try:
rolls, limit = map(int, cmd[1].split('d'))
result = ', '.join(str(random.randint(1, limit)) for r in range(rolls))
await client.send_message(message.channel, result)
except Exception:
await client.send_message(message.channel, 'Format has to be in NdN!')
client.run(token)
# client.user.avatar_url == Kafra
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment