1
0
Fork 0
mirror of synced 2024-06-10 06:34:33 +12:00

Corrected some syntax/pep8 errors

This commit is contained in:
phxntxm 2016-08-16 10:22:32 -05:00
parent 8239d66524
commit c8730a3b6f
6 changed files with 96 additions and 72 deletions

View file

@ -8,8 +8,9 @@ import random
def battling_off(player_id):
battling = config.get_content('battling') or {}
# Create a new dictionary, exactly the way the last one was setup, but don't include any that have the player's ID provided
# Create a new dictionary, exactly the way the last one was setup
# But don't include any that have the player's ID provided
battling = {p1: p2 for p1, p2 in battling.items() if not p2 == player_id and not p1 == player_id}
config.save_content('battling', battling)
@ -17,7 +18,7 @@ def battling_off(player_id):
def user_battling(ctx, player2=None):
battling = config.get_content('battling')
# If no one is battling, obviously the user is not battling
if battling is None:
return False
@ -37,14 +38,14 @@ def update_battle_records(winner, loser):
battles = config.get_content('battle_records')
if battles is None:
battles = {winner.id: "1-0", loser.id: "0-1"}
# Start ratings at 1000 if they have no rating
winner_stats = battles.get(winner.id) or {}
winner_rating = winner_stats.get('rating') or 1000
loser_stats = battles.get(loser.id) or {}
loser_rating = loser_stats.get('rating') or 1000
# The scale is based off of increments of 25, increasing the change by 1 for each increment
# That is all this loop does, increment the "change" for every increment of 25
# The change caps off at 300 however, so break once we are over that limit
@ -56,7 +57,7 @@ def update_battle_records(winner, loser):
break
rating_change += 1
count += 25
# 16 is the base change, increased or decreased based on whoever has the higher current rating
if winner_rating > loser_rating:
winner_rating += 16 - rating_change
@ -64,7 +65,7 @@ def update_battle_records(winner, loser):
else:
winner_rating += 16 + rating_change
loser_rating -= 16 + rating_change
# Just increase wins/losses for each person, making sure it's at least 0
winner_wins = winner_stats.get('wins') or 0
winner_losses = winner_stats.get('losses') or 0
@ -72,7 +73,7 @@ def update_battle_records(winner, loser):
loser_losses = loser_stats.get('losses') or 0
winner_wins += 1
loser_losses += 1
# Now save the new wins, losses, and ratings
winner_stats = {'wins': winner_wins, 'losses': winner_losses, 'rating': winner_rating}
loser_stats = {'wins': loser_wins, 'losses': loser_losses, 'rating': loser_rating}
@ -102,12 +103,12 @@ class Interaction:
if user_battling(ctx, player2):
await self.bot.say("You or the person you are trying to battle is already in a battle!")
return
# Add the author and player provided in a new battle
battling = config.get_content('battling') or {}
battling[ctx.message.author.id] = ctx.message.mentions[0].id
config.save_content('battling', battling)
fmt = "{0.mention} has challenged you to a battle {1.mention}\n!accept or !decline"
# Add a call to turn off battling, if the battle is not accepted/declined in 3 minutes
config.loop.call_later(180, battling_off, ctx.message.author.id)
@ -122,7 +123,7 @@ class Interaction:
if not user_battling(ctx):
await self.bot.say("You are not currently in a battle!")
return
# This is an extra check to make sure that the author is the one being BATTLED
# And not the one that started the battle
battling = config.get_content('battling') or {}
@ -133,12 +134,12 @@ class Interaction:
battleP1 = discord.utils.find(lambda m: m.id == p1[0], ctx.message.server.members)
battleP2 = ctx.message.author
# Get a random win message from our list
fmt = config.battleWins[random.SystemRandom().randint(0, len(config.battleWins) - 1)]
# Due to our previous check, the ID should only be in the dictionary once, in the current battle we're checking
battling_off(ctx.message.author.id)
# Randomize the order of who is printed/sent to the update system
# All we need to do is change what order the challengers are printed/added as a paramater
if random.SystemRandom().randint(0, 1):
@ -147,7 +148,7 @@ class Interaction:
else:
await self.bot.say(fmt.format(battleP2.mention, battleP1.mention))
update_battle_records(battleP2, battleP1)
await self.bot.delete_message(ctx.message)
@commands.command(pass_context=True, no_pm=True)
@ -157,7 +158,7 @@ class Interaction:
if not user_battling(ctx):
await self.bot.say("You are not currently in a battle!")
return
# This is an extra check to make sure that the author is the one being BATTLED
# And not the one that started the battle
battling = config.get_content('battling') or {}
@ -165,11 +166,10 @@ class Interaction:
if len(p1) == 0:
await self.bot.say("You are not currently being challenged to a battle!")
return
battleP1 = discord.utils.find(lambda m: m.id == p1[0], ctx.message.server.members)
battleP2 = ctx.message.author
# There's no need to update the stats for the members if they declined the battle
battling_off(ctx.message.author.id)
await self.bot.say("{0} has chickened out! What a loser~".format(battleP2.mention, battleP1.mention))
@ -180,6 +180,7 @@ class Interaction:
@checks.custom_perms(send_messages=True)
async def boop(self, ctx, boopee: discord.Member):
"""Boops the mentioned person"""
booper = ctx.message.author
if boopee.id == booper.id:
await self.bot.say("You can't boop yourself! Silly...")
return
@ -188,8 +189,9 @@ class Interaction:
return
boops = config.get_content('boops') or {}
# This is only used to print the amount of times they've booped someone, set to 1 for the first time someone was booped
# This is only used to print the amount of times they've booped someone
# Set to 1 for the first time someone was booped
amount = 1
# Get all the booped stats for the author
booper_boops = boops.get(ctx.message.author.id)
@ -197,7 +199,8 @@ class Interaction:
# Create a new dictionary with the amount
if booper_boops is None:
boops[ctx.message.author.id] = {boopee.id: 1}
# If the booper has never booped the member provided, still add that user to the dictionary with the amount of 1 to start it off
# If the booper has never booped the member provided, still add that user
# To the dictionary with the amount of 1 to start it off
elif booper_boops.get(boopee.id) is None:
booper_boops[boopee.id] = 1
boops[ctx.message.author.id] = booper_boops

View file

@ -18,7 +18,7 @@ class Owner:
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True)
@commands.check(checks.is_owner)
async def saferestart(self, ctx):
@ -27,10 +27,12 @@ class Owner:
# I do not want to restart the bot if someone is playing music
# This gets all the exiting VoiceStates that are playing music right now
# If we are, say which server it
servers_playing_music = [server_id for server_id, state in self.bot.get_cog('Music').voice_states.items() if state.is_playing()]
await self.bot.say("Sorry, it's not safe to restart. I am currently playing a song on {} servers".format(len(servers_playing_music)))
servers_playing_music = [server_id for server_id, state in self.bot.get_cog('Music').voice_states.items() if
state.is_playing()]
await self.bot.say("Sorry, it's not safe to restart. I am currently playing a song on {} servers".format(
len(servers_playing_music)))
ctx.invoke(self.bot.commands.get('restart'))
@commands.command(pass_context=True)
@commands.check(checks.is_owner)
async def restart(self, ctx):
@ -47,7 +49,7 @@ class Owner:
"""Saves a URL as an image to add for the doggo command"""
# Save the local path based on how many images there currently are
local_path = 'images/doggo{}.jpg'.format(len(glob.glob('images/doggo*')))
# "read" the image and save as bytes
with aiohttp.ClientSession() as s:
async with s.get(url) as r:
@ -56,14 +58,14 @@ class Owner:
f.write(val)
await self.bot.say(
"Just saved a new doggo image! I now have {} doggo images!".format(len(glob.glob('images/doggo*'))))
@commands.command()
@commands.check(checks.is_owner)
async def addsnek(self, url: str):
"""Saves a URL as an image to add for the snek command"""
# Save the local path based on how many images there currently are
local_path = 'images/snek{}.jpg'.format(len(glob.glob('images/snek*')))
# "read" the image and save as bytes
with aiohttp.ClientSession() as s:
async with s.get(url) as r:
@ -79,16 +81,15 @@ class Owner:
"""Executes code"""
# Eval and exec have different useful purposes, so use both
try:
# `Get all content in this format`
match_single = getter.findall(ctx.message.content)
# ```\nGet all content in this format```
match_multi = multi.findall(ctx.message.content)
if match_single:
result = eval(match_single[0])
# In case the result needs to be awaited, handle that
if inspect.isawaitable(result):
result = await result
@ -97,6 +98,7 @@ class Owner:
# Internal method to send the message to the channel, of whatever is passed
def r(v):
self.bot.loop.create_task(self.bot.say("```\n{}```".format(v)))
exec(match_multi[0])
except Exception as error:
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
@ -131,18 +133,18 @@ class Owner:
async def status(self, *, status: str):
"""Changes the bot's 'playing' status"""
await self.bot.change_status(discord.Game(name=status, type=0))
await self.bot.say("Just changed my status to '{0}'!".format(newStatus))
await self.bot.say("Just changed my status to '{0}'!".format(status))
@commands.command()
@commands.check(checks.is_owner)
async def load(self, *, module: str):
"""Loads a module"""
# Do this because I'm too lazy to type cogs.module
module = module.lower()
if not module.startswith("cogs"):
module = "cogs.{}".format(module)
# This try catch will catch errors such as syntax errors in the module we are loading
try:
self.bot.load_extension(module)
@ -150,17 +152,17 @@ class Owner:
except Exception as error:
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await self.bot.say(fmt.format(type(error).__name__, error))
@commands.command()
@commands.check(checks.is_owner)
async def unload(self, *, module: str):
"""Unloads a module"""
# Do this because I'm too lazy to type cogs.module
module = module.lower()
if not module.startswith("cogs"):
module = "cogs.{}".format(module)
self.bot.unload_extension(module)
await self.bot.say("I have just unloaded the {} module".format(module))
@ -168,13 +170,13 @@ class Owner:
@commands.check(checks.is_owner)
async def reload(self, *, module: str):
"""Reloads a module"""
# Do this because I'm too lazy to type cogs.module
module = module.lower()
if not module.startswith("cogs"):
module = "cogs.{}".format(module)
self.bot.unload_extension(module)
# This try block will catch errors such as syntax errors in the module we are loading
try:
self.bot.load_extension(module)

View file

@ -1,5 +1,4 @@
import aiohttp
import json
import asyncio
import discord
import re
@ -27,12 +26,16 @@ async def online_users():
except:
return {}
def check_online(online_channels, channel):
# online_channels is the dictionary of all users online currently, and channel is the name we are checking against that
# This creates a list of all users that match this channel name (should only ever be 1) and returns True as long as it is more than 0
# online_channels is the dictionary of all users online currently
# And channel is the name we are checking against that
# This creates a list of all users that match this channel name (should only ever be 1)
# And returns True as long as it is more than 0
matches = [stream for stream in online_channels if stream['channel_name'].lower() == channel.lower()]
return len(matches) > 0
class Picarto:
def __init__(self, bot):
self.bot = bot
@ -54,8 +57,9 @@ class Picarto:
user = re.search("(?<=picarto.tv/)(.*)", url).group(1)
# This is whether or not they are actually online
online = check_online(online_users_list, user)
# If they're set to notify, not live in the config file, but online currently, means they went online since the last check
# If they're set to notify, not live in the config file
# But online currently, means they went online since the last check
if not live and notify and online:
for server_id in r['servers']:
# Get the channel to send the message to, based on the saved alert's channel
@ -65,13 +69,14 @@ class Picarto:
channel = self.bot.get_channel(channel_id)
# Get the member that has just gone live
member = discord.utils.get(server.members, id=m_id)
# Set them as live in the configuration file, and send a message saying they have just gone live
picarto[m_id]['live'] = 1
fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url)
config.save_content('picarto', picarto)
await self.bot.send_message(channel, fmt)
# If they're live in the configuration file, but not online currently, means they went offline since the last check
# If they're live in the configuration file, but not online currently
# Means they went offline since the last check
elif live and not online:
for server_id in r['servers']:
# Get the channel to send the message to, based on the saved alert's channel
@ -81,8 +86,9 @@ class Picarto:
channel = self.bot.get_channel(channel_id)
# Get the member that has just gone live
member = discord.utils.get(server.members, id=m_id)
# Set them as offline in the confugration, then send a message letting the channel know they've just gone offline
# Set them as offline in the confugration
# Then send a message letting the channel know they've just gone offline
picarto[m_id]['live'] = 0
fmt = "{} has just gone offline! Catch them next time they stream at {}".format(
member.display_name,
@ -103,21 +109,22 @@ class Picarto:
except:
await self.bot.say("That user does not have a picarto url setup!")
return
# Use regex to get the actual username so that we can make a request to the API
stream = re.search("(?<=picarto.tv/)(.*)", member_url).group(1)
url = '{}/channel/{}?key={}'.format(base_url, stream, key)
async with self.session.get(url, headers=self.headers) as r:
data = await r.json()
# Not everyone has all these settings, so use this as a way to print information if it does, otherwise ignore it
things_to_print = ['channel', 'commissions_enabled', 'is_nsfw', 'program', 'tablet', 'followers',
'content_type']
# Using title and replace to provide a nice way to print the data
fmt = "\n".join(
"{}: {}".format(i.title().replace("_", " "), r) for i, r in data.items() if i in things_to_print)
# Social URL's can be given if a user wants them to show, print them if they exist, otherwise don't try to include them
# Social URL's can be given if a user wants them to show
# Print them if they exist, otherwise don't try to include them
social_links = data.get('social_urls')
if social_links:
fmt2 = "\n".join("\t{}: {}".format(i.title().replace("_", " "), r) for i, r in social_links.items())
@ -134,7 +141,8 @@ class Picarto:
# Otherwise if it doesn't match, we'll hit an AttributeError due to .group(0)
# This means that the url was just given as a user (or something complete invalid)
# So set URL as https://www.picarto.tv/[url]
# Even if this was invalid such as https://www.picarto.tv/twitch.tv/user for example, our next check handles that
# Even if this was invalid such as https://www.picarto.tv/twitch.tv/user
# For example, our next check handles that
try:
url = re.search("((?<=://)?picarto.tv/)+(.*)", url).group(0)
except AttributeError:
@ -143,7 +151,7 @@ class Picarto:
url = "https://www.{}".format(url)
api_url = '{}/channel/{}?key={}'.format(base_url, re.search("https://www.picarto.tv/(.*)", url).group(1), key)
# Check if we can find a user with the provided information, if we can't just return
async with self.session.get(api_url, headers=self.headers) as r:
if not r.status == 200:
@ -153,9 +161,10 @@ class Picarto:
picarto_urls = config.get_content('picarto') or {}
result = picarto_urls.get(ctx.message.author.id)
# If information for this user already exists, override just the url, and not the information
# Otherwise create the information with notications on, and that they're not live. The next time it's checked, they'll go 'online'
# Otherwise create the information with notications on, and that they're not live.
# The next time it's checked, they'll go 'online'
if result is not None:
picarto_urls[ctx.message.author.id]['picarto_url'] = url
else:
@ -187,7 +196,7 @@ class Picarto:
"""This can be used to turn picarto notifications on or off
Call this command by itself, to add this server to the list of servers to be notified"""
member = ctx.message.author
# If this user's picarto URL is not saved, no use in adding this server to the list that doesn't exist
picarto = config.get_content('picarto') or {}
result = picarto.get(member.id)
@ -195,12 +204,13 @@ class Picarto:
await self.bot.say(
"I do not have your picarto URL added {}. You can save your picarto url with {}picarto add".format(
member.mention, ctx.prefix))
# Append this server's ID and save the new content
picarto[member.id]['servers'].append(ctx.message.server.id)
config.save_content('picarto', picarto)
await self.bot.say(
"I have just changed which channel will be notified when you go live, to `{}`".format(channel.name))
"I have just changed which channel will be notified when you go live, to `{}`".format(
ctx.message.channel.name))
@notify.command(name='on', aliases=['start,yes'], pass_context=True, no_pm=True)
@checks.custom_perms(send_messages=True)

View file

@ -28,9 +28,12 @@ class VoiceState:
self.voice = None
self.bot = bot
self.play_next_song = asyncio.Event()
self.songs = asyncio.Queue(maxsize=10) # This is the queue that holds all VoiceEntry's
self.skip_votes = set() # a set of user_ids that voted
self.audio_player = self.bot.loop.create_task(self.audio_player_task()) # Our actual task that handles the queue system
# This is the queue that holds all VoiceEntry's
self.songs = asyncio.Queue(maxsize=10)
# a set of user_ids that voted
self.skip_votes = set()
# Our actual task that handles the queue system
self.audio_player = self.bot.loop.create_task(self.audio_player_task())
self.opts = {
'default_search': 'auto',
'quiet': True,
@ -67,7 +70,8 @@ class VoiceState:
# Clear the votes skip that were for the last song
self.skip_votes.clear()
# Set current to none while we are waiting for the next song in the queue
# If we don't do this and we hit the end of the queue, our current song will remain the song that just finished
# If we don't do this and we hit the end of the queue
# our current song will remain the song that just finished
self.current = None
# Now wait for the next song in the queue
self.current = await self.songs.get()
@ -95,7 +99,8 @@ class Music:
state = self.voice_states.get(server.id)
# Internally handle creating a voice state if there isn't a current state
# This can be used for example, in case something is skipped when not being connected, we create the voice state when checked
# This can be used for example, in case something is skipped when not being connected
# We create the voice state when checked
# This only creates the state, we are still not playing anything, which can then be handled separately
if state is None:
state = VoiceState(self.bot)
@ -196,7 +201,7 @@ class Music:
player = await state.voice.create_ytdl_player(song, ytdl_options=state.opts, after=state.toggle_next)
except youtube_dl.DownloadError:
fmt = "Sorry, either I had an issue downloading that video, or that's not a supported URL!"
await self.bot.say(fmt)
await self.bot.send_message(ctx.message.channel, fmt)
return
# Now we can create a VoiceEntry and queue it
@ -308,7 +313,7 @@ class Music:
if len(queue) == 0:
fmt = "Nothing currently in the queue"
else:
fmt = "\n\n".join(str(x) for x in state.songs._queue)
fmt = "\n\n".join(str(x) for x in queue)
await self.bot.say("Current songs in the queue:```\n{}```".format(fmt))
@commands.command(pass_context=True, no_pm=True)

View file

@ -24,7 +24,7 @@ class Stats:
# Then get a sorted list, based on the amount of times they've booped the member
# Reverse needs to be true, as we want it to go from highest to lowest
sorted_boops = sorted(boops.get(ctx.message.author.id).items(), key=lambda x: x[1], reverse=True)
# Then override the same list, checking if the member they've booped is in this server, using our previous list comprehension
# Then override the same list, checking if the member they've booped is in this server
sorted_boops = [x for x in sorted_boops if x[0] in server_member_ids]
# Since this is sorted, we just need to get the following information on the first user in the list
@ -94,7 +94,8 @@ class Stats:
sorted_server_members = sorted(server_members.items(), key=lambda x: x[1]['rating'], reverse=True)
sorted_all_members = sorted(all_members.items(), key=lambda x: x[1]['rating'], reverse=True)
# Enumurate the list so that we can go through, find the user's place in the list, and get just that for the rank
# Enumurate the list so that we can go through, find the user's place in the list
# and get just that for the rank
server_rank = [i for i, x in enumerate(sorted_server_members) if x[0] == member.id][0] + 1
total_rank = [i for i, x in enumerate(sorted_all_members) if x[0] == member.id][0] + 1
# The rest of this is straight forward, just formatting

View file

@ -55,7 +55,8 @@ class Strawpoll:
data = await response.json()
# The response for votes and options is provided as two separate lists
# We are enumarting the list of options, to print r (the option) and the votes to match it, based on the index of the option
# We are enumarting the list of options, to print r (the option)
# And the votes to match it, based on the index of the option
# The rest is simple formatting
fmt_options = "\n\t".join(
"{}: {}".format(r, data['votes'][i]) for i, r in enumerate(data['options']))
@ -78,11 +79,13 @@ class Strawpoll:
# We're using this instead of other things, to allow most used puncation inside the options
match_single = getter.findall(options)
match_multi = multi.findall(options)
# Since match_single is already going to be a list, we just set the options to match_single and remove any blank entries
# Since match_single is already going to be a list, we just set
# The options to match_single and remove any blank entries
if match_single:
options = match_single
options = [option for option in options if option]
# Otherwise, options need to be set based on the list, split by lines. Then remove blank entries like the last one
# Otherwise, options need to be set based on the list, split by lines.
# Then remove blank entries like the last one
elif match_multi:
options = match_multi[0].splitlines()
options = [option for option in options if option]