edenbot/bot.py
2022-02-19 14:41:47 -05:00

80 lines
2.0 KiB
Python

import aiosqlite
import discord
import logging
import requests
from discord.ext import commands
import os
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
API_ENDPOINT = 'http://api-mainnet.magiceden.dev/v2/'
# Bot
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='$', intents=intents)
# Logging
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
# Database
schema = [
'''CREATE TABLE IF NOT EXISTS collections (
id integer PRIMARY KEY,
symbol text,
image text,
name text,
last_price integer
)''',
'''CREATE TABLE IF NOT EXISTS guilds (
id integer PRIMARY KEY,
guild_id integer
)''',
'''CREATE TABLE IF NOT EXISTS lists (
collection_id integer,
guild_id integer
)'''
]
async def create_db():
for query in schema:
await db.execute (query)
await db.commit()
print('Database ready')
@bot.event
async def on_ready():
global db
db = await aiosqlite.connect('db.sqlite3')
await create_db()
print('We have logged in as {0.user}'.format(bot))
@bot.event
async def on_guild_join(guild):
await db.execute('INSERT INTO guilds (guild_id) VALUES (?)', (guild.id,))
await db.commit()
print(f"Joined guild {guild.name}")
@bot.command()
async def list(ctx):
list_string = 'Here is a list of all your tracked collections:\n'
async with await db.execute("SELECT id from guilds WHERE guild_id=? LIMIT 1", (ctx.guild.id,)) as cursor:
async for row in cursor:
guild_id = row[0]
async with await db.execute("SELECT collection_id from lists WHERE guild_id=?", (guild_id,)) as cursor:
async for collection in cursor:
list_string += f'{collection.name}\n'
# await ctx.send(list_string)
bot.run(TOKEN)