Last active
October 25, 2017 23:22
-
-
Save ExtraConcentratedJuice/f2b74a29f0d3677c2334fc7802fbc049 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import asyncio | |
import random | |
import logging | |
import yaml | |
import time | |
import discord | |
import sqlite3 | |
from discord.ext import commands | |
logging.basicConfig(level=logging.INFO) | |
prefix = 'g>' | |
bot = commands.Bot(command_prefix=prefix, description='Giveaway-bot', max_messages=15000) | |
with open("config.yaml", 'r') as f: | |
config = yaml.load(f) | |
async def giveaway(): | |
"""Giveaway loop""" | |
await bot.wait_until_ready() | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute("SELECT * FROM giveaways WHERE id = (SELECT MAX(id) FROM giveaways)") | |
result = c.fetchone() | |
if result != None: | |
if result[2] < time.time(): | |
print('Giveway {} has ended.'.format(result[0])) | |
await giveaway_end() | |
else: | |
resume_time = result[2] - time.time() | |
print('Giveway {} resuming, {}s remaining.'.format(result[0], resume_time)) | |
await asyncio.sleep(resume_time) | |
print('Giveaway {} has ended.'.format(result[0])) | |
await giveaway_end() | |
while not bot.is_closed: | |
print('Starting a new giveaway...') | |
await giveaway_start() | |
await asyncio.sleep(86400) | |
await giveaway_end() | |
print('Giveaway has ended.') | |
async def giveaway_end(): | |
"""Choose winner""" | |
giveaway_channel = bot.get_channel(config['channel_id']) | |
entries = get_all_entries() | |
if not entries: | |
await bot.send_message(giveaway_channel, 'Nobody entered...') | |
return | |
winner = random.choice(entries) | |
await bot.send_message(giveaway_channel, '<@{}> has won the giveaway!'.format(winner)) | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute("UPDATE giveaways SET winner = ? WHERE id = (SELECT MAX(id) FROM giveaways)", (winner, )) | |
c.execute("DELETE FROM entries") | |
db.commit() | |
db.close() | |
# Steam shit goes here | |
user = get_trade_url(winner) | |
print('Sending trade to ' + user[0]) | |
async def giveaway_start(): | |
"""Create a new giveaway""" | |
giveaway_channel = bot.get_channel(config['channel_id']) | |
desc = 'A giveaway has started. Please enter by reacting with :white_check_mark:.' | |
embed = discord.Embed(title='GIVEAWAY', description=desc, color=10038562).set_footer(text='Extra\'s Giveaway Mockup') | |
g_message = await bot.send_message(giveaway_channel, embed=embed) | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
start_time = time.time() | |
values = (start_time, start_time + 86400, g_message.id, None) | |
c.execute("INSERT INTO giveaways (start, end, msgid, winner) VALUES (?, ?, ?, ?)", values) | |
db.commit() | |
db.close() | |
def get_trade_url(user_id): | |
"""Grabs trade url from DB""" | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute("SELECT url FROM users WHERE id = ?", (user_id, )) | |
result = c.fetchone() | |
db.close() | |
return result | |
def add_entry(user_id): | |
"""Adds a giveaway entry.""" | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute("INSERT INTO entries VALUES (?)", (user_id, )) | |
db.commit() | |
db.close() | |
def get_entry(user_id): | |
"""Gets a giveaway entry.""" | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute("SELECT id FROM entries WHERE id = ?", (user_id, )) | |
result = c.fetchone() | |
db.close() | |
return result | |
def get_all_entries(): | |
"""Gets all entrants""" | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute("SELECT id FROM entries") | |
results = c.fetchall() | |
db.close() | |
return [x[0] for x in results] | |
def get_current_giveaway(): | |
"""Grabs the current giveaway from DB""" | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute("SELECT * FROM giveaways WHERE id = (SELECT MAX(id) FROM giveaways)") | |
result = c.fetchone() | |
db.close() | |
return result | |
@bot.command(pass_context=True) | |
async def giveawayinfo(ctx): | |
"""Returns information about the current giveaway.""" | |
giveaway = get_current_giveaway() | |
time_left = (giveaway[2] - time.time()) / 60 / 60 | |
desc = 'Giveaway #{}'.format(giveaway[0]) | |
embed = discord.Embed(title='Giveaway Information', description=desc, color=10038562) | |
embed.set_footer(text='Extra\'s Giveaway Mockup') | |
embed.add_field(name='Time Remaining', value='{} hours'.format(time_left), inline=False) | |
await bot.send_message(ctx.message.author, embed=embed) | |
await bot.add_reaction(ctx.message, '👌') | |
@bot.command(pass_context=True) | |
async def settradeurl(ctx, tradeurl): | |
"""Lets a user set their tradeurl""" | |
steamurl = 'https://steamcommunity.com/tradeoffer/new/?partner=' | |
if steamurl not in tradeurl: | |
await bot.say('This is an invalid trade url.') | |
return | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute("SELECT id FROM users WHERE id = ?", (ctx.message.author.id, )) | |
if c.fetchone() != None: | |
c.execute("UPDATE users SET url = ? WHERE id = ?", (tradeurl, ctx.message.author.id)) | |
db.commit() | |
db.close() | |
await bot.say('Your tradeurl was updated to ``{}``'.format(tradeurl)) | |
await bot.add_reaction(ctx.message, '👌') | |
else: | |
c.execute("INSERT INTO users VALUES (?, ?)", (ctx.message.author.id, tradeurl)) | |
db.commit() | |
db.close() | |
await bot.say('Your tradeurl was set to ``{}``'.format(tradeurl)) | |
await bot.add_reaction(ctx.message, '👌') | |
@bot.event | |
async def on_reaction_add(reaction, user): | |
if reaction.message.channel.id != config['channel_id']: | |
return | |
if user == bot.user: | |
return | |
if user.bot: | |
return | |
giveaway = get_current_giveaway() | |
if int(reaction.message.id) != giveaway[3]: | |
return | |
if not get_trade_url(user.id): | |
msg = ('You do not have a tradeurl set. Please set set one through ``{}settradeurl``' | |
'and re-enter by removing and readding your reaction.') | |
await bot.send_message(user, msg.format(prefix)) | |
else: | |
if not get_entry(user.id): | |
add_entry(user.id) | |
else: | |
return | |
await bot.send_message(user, 'You have successfully entered the giveaway. Thanks.') | |
@bot.event | |
async def on_ready(): | |
# Creates databases | |
db = sqlite3.connect('data.db') | |
c = db.cursor() | |
c.execute('CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, url TEXT)') | |
c.execute('CREATE TABLE IF NOT EXISTS entries(id INTEGER PRIMARY KEY)') | |
c.execute('CREATE TABLE IF NOT EXISTS giveaways(id INTEGER PRIMARY KEY, start INTEGER, end INTEGER, msgid INTEGER, winner INTEGER)') | |
db.commit() | |
db.close() | |
cgiveaway = get_current_giveaway() | |
# Makes sure that our giveaway message is cached on bot resume. | |
# at least i dont have to use rewrite lol | |
if cgiveaway: | |
giveaway_channel = bot.get_channel(config['channel_id']) | |
latest_giveaway = await bot.get_message(giveaway_channel, cgiveaway[3]) | |
bot.messages.append(latest_giveaway) | |
bot.loop.create_task(giveaway()) | |
print('Ready!') | |
print(bot.user.name) | |
print(bot.user.id) | |
bot.run(config['token']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment