1
0
Fork 0
mirror of synced 2024-05-19 20:12:30 +12:00

Modified file to match PEP-8

This commit is contained in:
phxntxm 2016-07-07 17:10:24 -05:00
parent 2feaf487e2
commit 2a9c64f7aa

66
bot.py
View file

@ -22,32 +22,48 @@ with open("/home/phxntx5/public_html/bot/config.yml","r") as f:
botDescription = global_config.get("description")
commandPrefix = global_config.get("command_prefix")
# Custom predicates
def isOwner():
def predicate(ctx):
return ctx.message.author.id == ownerID
return commands.check(predicate)
def isMod():
def predicate(ctx):
return ctx.message.author.top_role.permissions.kick_members
return commands.check(predicate)
def isAdmin():
def predicate(ctx):
return ctx.message.author.top_role.permissions.manage_server
return commands.check(predicate)
def isPM():
def predicate(ctx):
return ctx.message.channel.is_private
return commands.check(predicate)
def battled():
def predicate(ctx):
return ctx.message.author == battleP2
return commands.check(predicate)
bot = commands.Bot(command_prefix=commandPrefix, description=botDescription)
music = playlist.Music(bot)
bot.add_cog(music)
# Turn battling off, reset users
def battlingOff():
global battleP1
@ -57,6 +73,7 @@ def battlingOff():
battleP1 = ""
battleP2 = ""
# Bot event overrides
@bot.event
async def on_ready():
@ -67,8 +84,8 @@ async def on_ready():
'''success = checkSetup(cursor)
if success=="Error: default_db":
await bot-send_message(determineId(ownerID),("The bot ran into an error while checking the database information."
"Please double check the config.yml file, make sure the database is created, and the user has access to the database"))'''
fmt = "The bot ran into an error while checking the database information."
await bot.send_message(determineId(ownerID),fmt)'''
cursor.execute('use {0}'.format(db_default))
cursor.execute('select channel_id from restart_server where id=1')
@ -78,14 +95,17 @@ async def on_ready():
cursor.execute('update restart_server set channel_id=0 where id=1')
connection.commit()
@bot.event
async def on_member_join(member):
await bot.say("Welcome to the '{0.server.name}' server {0.mention}!".format(member))
@bot.event
async def on_member_remove(member):
await bot.say("{0} has left the server, I hope it wasn't because of something I said :c".format(member))
# Bot commands
@bot.command()
async def joke():
@ -97,6 +117,7 @@ async def joke():
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True)
@isOwner()
async def restart(ctx):
@ -113,6 +134,7 @@ async def restart(ctx):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True)
@isOwner()
async def py(ctx):
@ -128,11 +150,13 @@ async def py(ctx):
else:
def r(v):
loop.create_task(bot.say("```{0}```".format(v)))
exec(match_multi[0])
except Exception as e:
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True)
@isOwner()
async def shutdown(ctx):
@ -145,6 +169,7 @@ async def shutdown(ctx):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command()
@isOwner()
async def avatar(content):
@ -156,6 +181,7 @@ async def avatar(content):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command()
@isOwner()
async def name(newNick):
@ -169,6 +195,7 @@ async def name(newNick):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True, no_pm=True)
@isOwner()
async def leave(ctx):
@ -179,6 +206,7 @@ async def leave(ctx):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command()
@isMod()
async def status(*stat: str):
@ -191,6 +219,7 @@ async def status(*stat : str):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True)
@isMod()
async def say(ctx, *msg: str):
@ -202,6 +231,7 @@ async def say(ctx,*msg : str):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command()
async def urban(*msg: str):
try:
@ -219,6 +249,7 @@ async def urban(*msg : str):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True)
async def derpi(ctx, *search: str):
try:
@ -253,6 +284,7 @@ async def derpi(ctx,*search : str):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True)
async def roll(ctx):
try:
@ -263,6 +295,7 @@ async def roll(ctx):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(no_pm=True)
@battled()
async def accept():
@ -282,6 +315,7 @@ async def accept():
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(no_pm=True)
@battled()
async def decline():
@ -295,6 +329,7 @@ async def decline():
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True)
async def commands(ctx):
try:
@ -303,7 +338,7 @@ async def commands(ctx):
fmt += '(please note that this only includes the commands that you have access to):'
await bot.whisper(fmt.format(ctx.message, bot))
fmt2 = 'I have just sent you a private message, containing all the commands you have access to {0.author.mention}!'
fmt2 = 'I have just sent you a PM, containing all the commands you have access to {0.author.mention}!'
ocmds = 'Commands for everyone: ```'
for com, act in openCommands.items():
ocmds += commandPrefix + com + ": " + act + "\n\n"
@ -340,6 +375,7 @@ async def commands(ctx):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True, no_pm=True)
async def battle(ctx):
try:
@ -372,6 +408,7 @@ async def battle(ctx):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True, no_pm=True)
async def boop(ctx):
try:
@ -413,8 +450,9 @@ async def boop(ctx):
cursor.execute(sql)
# 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"
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)
@ -425,6 +463,7 @@ async def boop(ctx):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True, no_pm=True)
async def mostboops(ctx):
try:
@ -434,12 +473,14 @@ async def mostboops(ctx):
cursor.execute(sql)
result = cursor.fetchone()
member = determineId(result.get('id'))
await bot.say("{0} you have booped {1} the most amount of times, coming in at {2} times".format(ctx.message.author.mention,member.mention,result.get('amount')))
await bot.say("{0} you have booped {1} the most amount of times, coming in at {2} times".format(
ctx.message.author.mention, member.mention, result.get('amount')))
connection.commit()
except Exception as e:
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
@bot.command(pass_context=True, no_pm=True)
async def mostwins(ctx):
try:
@ -488,6 +529,7 @@ async def mostwins(ctx):
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await bot.say(fmt.format(type(e).__name__, e))
def determineId(ID):
if type(ID) is int:
ID = str(ID)
@ -504,6 +546,7 @@ def determineId(ID):
if channel is not None:
return channel
def updateBattleRecords(winner, loser):
cursor = connection.cursor()
cursor.execute('use {0}'.format(db_default))
@ -538,6 +581,7 @@ def updateBattleRecords(winner,loser):
connection.commit()
def checkSetup(cursor):
try:
cursor.execute('use {}'.format(db_default))
@ -548,14 +592,16 @@ def checkSetup(cursor):
cursor.execute('describe battle_records')
except pymysql.ProgrammingError:
# battle_records does not exist, create it
sql = "create table `battle_records` (`id` varchar(32) not null,`record` varchar(32) not null,primary key (`id`)) engine=InnoDB default charset=utf8 collate=utf8_bin"
sql = "create table `battle_records` (`id` varchar(32) not null,`record` varchar(32) not null," + \
"primary key (`id`)) engine=InnoDB default charset=utf8 collate=utf8_bin"
cursor.execute(sql)
connection.commit()
try:
cursor.execute('describe restart_server')
except pymysql.ProgrammingError:
# restart_server does not exist, create it
sql = "create table `restart_server` (`id` int(11) not null auto_increment,`channel_id` varchar(32) not null,primary key (`id`)) engine=InnoDB default charset=utf8 collate=utf8_bin;"
sql = "create table `restart_server` (`id` int(11) not null auto_increment,`channel_id` varchar(32)" + \
"not null,primary key (`id`)) engine=InnoDB default charset=utf8 collate=utf8_bin;"
cursor.execute(sql)
connection.commit()
sql = "insert into restart_server (id,channel_id) values (1,'0')"
@ -566,11 +612,13 @@ def checkSetup(cursor):
except pymysql.OperationalError:
return "Error: boop_db"
db_default = global_config.get("db_default")
db_boops = global_config.get("db_boops")
nsfwChannels = global_config.get("nsfw_channel")
connection = pymysql.connect(host=global_config.get("db_host"), user=global_config.get("db_user"),
password=global_config.get("db_user_pass"),charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
password=global_config.get("db_user_pass"), charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
battling = False
battleP1 = None