2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
|
|
|
from .utils import checks
|
|
|
|
from .utils import config
|
|
|
|
from threading import Timer
|
2016-07-09 13:42:33 +12:00
|
|
|
import discord
|
2016-07-09 13:27:19 +12:00
|
|
|
import random
|
|
|
|
|
|
|
|
battling = False
|
|
|
|
battleP1 = None
|
|
|
|
battleP2 = None
|
|
|
|
|
|
|
|
|
|
|
|
def battlingOff():
|
|
|
|
global battleP1
|
|
|
|
global battleP2
|
|
|
|
global battling
|
|
|
|
battling = False
|
|
|
|
battleP1 = ""
|
|
|
|
battleP2 = ""
|
|
|
|
|
|
|
|
|
|
|
|
def updateBattleRecords(winner, loser):
|
2016-07-10 04:02:33 +12:00
|
|
|
cursor = config.getCursor()
|
2016-07-09 13:27:19 +12:00
|
|
|
cursor.execute('use {0}'.format(config.db_default))
|
|
|
|
|
|
|
|
# Update winners records
|
|
|
|
sql = "select record from battle_records where id={0}".format(winner.id)
|
|
|
|
cursor.execute(sql)
|
|
|
|
result = cursor.fetchone()
|
|
|
|
if result is not None:
|
|
|
|
result = result['record'].split('-')
|
|
|
|
result[0] = str(int(result[0]) + 1)
|
|
|
|
sql = "update battle_records set record ='{0}' where id='{1}'".format("-".join(result), winner.id)
|
|
|
|
cursor.execute(sql)
|
|
|
|
else:
|
|
|
|
sql = "insert into battle_records (id,record) values ('{0}','1-0')".format(winner.id)
|
|
|
|
cursor.execute(sql)
|
|
|
|
|
|
|
|
config.connection.commit()
|
2016-07-10 04:02:33 +12:00
|
|
|
config.connection.close()
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
# Update losers records
|
|
|
|
sql = "select record from battle_records where id={0}".format(loser.id)
|
|
|
|
cursor.execute(sql)
|
|
|
|
result = cursor.fetchone()
|
|
|
|
if result is not None:
|
|
|
|
result = result['record'].split('-')
|
|
|
|
result[1] = str(int(result[1]) + 1)
|
|
|
|
sql = "update battle_records set record ='{0}' where id='{1}'".format('-'.join(result), loser.id)
|
|
|
|
cursor.execute(sql)
|
|
|
|
else:
|
|
|
|
sql = "insert into battle_records (id,record) values ('{0}','0-1')".format(loser.id)
|
|
|
|
cursor.execute(sql)
|
|
|
|
|
2016-07-10 04:33:48 +12:00
|
|
|
config.closeConnection()
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
|
|
|
|
class Interaction:
|
2016-07-09 13:59:10 +12:00
|
|
|
"""Commands that interact with another user"""
|
2016-07-09 13:27:19 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2016-07-09 13:42:33 +12:00
|
|
|
async def battle(self, ctx, player2: discord.Member):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Challenges the mentioned user to a battle"""
|
2016-07-10 00:57:25 +12:00
|
|
|
global battleP1
|
|
|
|
global battleP2
|
|
|
|
global battling
|
|
|
|
if battling:
|
|
|
|
return
|
|
|
|
if len(ctx.message.mentions) == 0:
|
|
|
|
await self.bot.say("You must mention someone in the room " + ctx.message.author.mention + "!")
|
|
|
|
return
|
|
|
|
if len(ctx.message.mentions) > 1:
|
|
|
|
await self.bot.say("You cannot battle more than one person at once!")
|
|
|
|
return
|
|
|
|
if ctx.message.author.id == player2.id:
|
|
|
|
await self.bot.say("Why would you want to battle yourself? Suicide is not the answer")
|
|
|
|
return
|
|
|
|
if self.bot.user.id == player2.id:
|
|
|
|
await self.bot.say("I always win, don't even try it.")
|
|
|
|
return
|
|
|
|
fmt = "{0.mention} has challenged you to a battle {1.mention}\n!accept or !decline"
|
|
|
|
battleP1 = ctx.message.author
|
|
|
|
battleP2 = player2
|
|
|
|
await self.bot.say(fmt.format(ctx.message.author, player2))
|
|
|
|
t = Timer(180, battlingOff)
|
|
|
|
t.start()
|
|
|
|
battling = True
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
@commands.command(no_pm=True)
|
|
|
|
@checks.battled(battleP2)
|
|
|
|
async def accept(self):
|
|
|
|
"""Accepts the battle challenge"""
|
2016-07-10 00:57:25 +12:00
|
|
|
if not battling:
|
|
|
|
return
|
|
|
|
num = random.randint(1, 100)
|
|
|
|
fmt = config.battleWins[random.randint(0, len(config.battleWins) - 1)]
|
|
|
|
if num <= 50:
|
|
|
|
await self.bot.say(fmt.format(battleP1.mention, battleP2.mention))
|
|
|
|
updateBattleRecords(battleP1, battleP2)
|
|
|
|
elif num > 50:
|
|
|
|
await self.bot.say(fmt.format(battleP2.mention, battleP1.mention))
|
|
|
|
updateBattleRecords(battleP2, battleP1)
|
|
|
|
battlingOff()
|
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
@commands.command(no_pm=True)
|
|
|
|
@checks.battled(battleP2)
|
|
|
|
async def decline(self):
|
|
|
|
"""Declines the battle challenge"""
|
2016-07-10 00:57:25 +12:00
|
|
|
if not battling:
|
|
|
|
return
|
|
|
|
await self.bot.say("{0} has chickened out! {1} wins by default!".format(battleP2.mention, battleP1.mention))
|
|
|
|
updateBattleRecords(battleP1, battleP2)
|
|
|
|
battlingOff()
|
2016-07-10 06:27:14 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2016-07-09 13:42:33 +12:00
|
|
|
async def boop(self, ctx, boopee: discord.Member):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Boops the mentioned person"""
|
2016-07-10 00:57:25 +12:00
|
|
|
booper = ctx.message.author
|
|
|
|
if len(ctx.message.mentions) == 0:
|
|
|
|
await self.bot.say("You must mention someone in the room " + ctx.message.author.mention + "!")
|
|
|
|
return
|
|
|
|
if len(ctx.message.mentions) > 1:
|
|
|
|
await self.bot.say("You cannot boop more than one person at once!")
|
|
|
|
return
|
|
|
|
if boopee.id == booper.id:
|
|
|
|
await self.bot.say("You can't boop yourself! Silly...")
|
|
|
|
return
|
|
|
|
if boopee.id == self.bot.user.id:
|
|
|
|
await self.bot.say("Why the heck are you booping me? Get away from me >:c")
|
|
|
|
return
|
|
|
|
|
2016-07-10 04:02:33 +12:00
|
|
|
cursor = config.getCursor()
|
2016-07-10 00:57:25 +12:00
|
|
|
cursor.execute('use {0}'.format(config.db_boops))
|
|
|
|
sql = "show tables like '" + str(booper.id) + "'"
|
|
|
|
cursor.execute(sql)
|
|
|
|
result = cursor.fetchone()
|
|
|
|
amount = 1
|
|
|
|
# Booper's table exists, continue
|
|
|
|
if result is not None:
|
|
|
|
sql = "select `amount` from `" + booper.id + "` where id='" + str(boopee.id) + "'"
|
2016-07-09 13:27:19 +12:00
|
|
|
cursor.execute(sql)
|
|
|
|
result = cursor.fetchone()
|
2016-07-10 00:57:25 +12:00
|
|
|
# Boopee's entry exists, continue
|
2016-07-09 13:27:19 +12:00
|
|
|
if result is not None:
|
2016-07-10 00:57:25 +12:00
|
|
|
amount = result.get('amount') + 1
|
|
|
|
sql = "update `" + str(booper.id) + "` set amount = " + str(amount) + " where id=" + str(
|
|
|
|
boopee.id)
|
2016-07-09 13:27:19 +12:00
|
|
|
cursor.execute(sql)
|
2016-07-10 00:57:25 +12:00
|
|
|
# Boopee does not exist, need to create the field for it
|
2016-07-09 13:27:19 +12:00
|
|
|
else:
|
|
|
|
sql = "insert into `" + str(booper.id) + "` (id,amount) values ('" + str(boopee.id) + "',1)"
|
|
|
|
cursor.execute(sql)
|
2016-07-10 00:57:25 +12:00
|
|
|
# Booper's table does not exist, need to create the table
|
|
|
|
else:
|
|
|
|
sql = "create table `" + str(booper.id) + \
|
|
|
|
"` (`id` varchar(255) not null,`amount` int(11) not null" + \
|
|
|
|
",primary key (`id`)) engine=InnoDB default charset=utf8 collate=utf8_bin"
|
|
|
|
cursor.execute(sql)
|
|
|
|
sql = "insert into `" + str(booper.id) + "` (id,amount) values ('" + str(boopee.id) + "',1)"
|
|
|
|
cursor.execute(sql)
|
|
|
|
fmt = "{0.mention} has just booped you {1.mention}! That's {2} times now!"
|
|
|
|
await self.bot.say(fmt.format(booper, boopee, amount))
|
2016-07-10 04:33:48 +12:00
|
|
|
config.closeConnection()
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Interaction(bot))
|