From 2f516ae313a619262349b7fef9f82c57c945e491 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 04:17:49 +0100 Subject: [PATCH 01/10] Persistent audio settings --- cogs/audio.py | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/cogs/audio.py b/cogs/audio.py index 42f45f4f1..6442773a4 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -20,8 +20,6 @@ if not discord.opus.is_loaded(): main_path = os.path.dirname(os.path.realpath(__main__.__file__)) -settings = {"VOLUME" : 0.5, "MAX_LENGTH" : 3700, "QUEUE_MODE" : True} - youtube_dl_options = { 'format': 'bestaudio/best', 'extractaudio': True, @@ -40,6 +38,7 @@ class Audio: def __init__(self, bot): self.bot = bot self.music_player = EmptyPlayer() + self.settings = fileIO("data/audio/settings.json", "load") self.queue_mode = False self.queue = [] self.playlist = [] @@ -253,7 +252,7 @@ class Audio: await self.bot.say("I'm already playing a playlist.") async def is_alone_or_admin(self, author): #Direct control. fix everything - if not settings["QUEUE_MODE"]: + if not self.settings["QUEUE_MODE"]: return True elif discord.utils.get(author.roles, name=checks.settings["ADMIN_ROLE"]) is not None: return True @@ -325,7 +324,7 @@ class Audio: """Changes audio module settings""" if ctx.invoked_subcommand is None: msg = "```" - for k, v in settings.items(): + for k, v in self.settings.items(): msg += str(k) + ": " + str(v) + "\n" msg += "\nType help audioset to see the list of commands.```" await self.bot.say(msg) @@ -333,35 +332,32 @@ class Audio: @audioset.command(name="queue") async def queueset(self, status : str): """Enables/disables queue""" - global settings status = status.lower() if status == "on" or status == "true": - settings["QUEUE_MODE"] = True + self.settings["QUEUE_MODE"] = True await self.bot.say("Queue mode is now on.") elif status == "off" or status == "false": - settings["QUEUE_MODE"] = False + self.settings["QUEUE_MODE"] = False await self.bot.say("Queue mode is now off.") else: await self.bot.say("Queue status can be either on or off.") return - self.save_settings() + fileIO(main_path + "/data/audio/settings.json", "save", self.settings) @audioset.command() async def maxlength(self, length : int): """Maximum track length for requested links""" - global settings - settings["MAX_LENGTH"] = length + self.settings["MAX_LENGTH"] = length await self.bot.say("Maximum length is now " + str(length) + " seconds.") - self.save_settings() + fileIO(main_path + "/data/audio/settings.json", "save", self.settings) @audioset.command() async def volume(self, level : float): """Sets the volume (0-1)""" - global settings if level >= 0 and level <= 1: - settings["VOLUME"] = level + self.settings["VOLUME"] = level await self.bot.say("Volume is now set at " + str(level) + ". It will take effect after the current track.") - self.save_settings() + fileIO(main_path + "/data/audio/settings.json", "save", self.settings) else: await self.bot.say("Volume must be between 0 and 1. Example: 0.40") @@ -379,7 +375,7 @@ class Audio: if self.downloader["ID"]: try: self.music_player.stop() - self.music_player = self.bot.voice.create_ffmpeg_player(path + self.downloader["ID"], options='''-filter:a "volume={}"'''.format(settings["VOLUME"])) + self.music_player = self.bot.voice.create_ffmpeg_player(path + self.downloader["ID"], options='''-filter:a "volume={}"'''.format(self.settings["VOLUME"])) self.music_player.start() if path != "": await self.bot.change_status(discord.Game(name=self.downloader["TITLE"])) except discord.errors.ClientException: @@ -448,7 +444,7 @@ class Audio: self.downloader["DOWNLOADING"] = True yt = youtube_dl.YoutubeDL(youtube_dl_options) v = yt.extract_info(url, download=False) - if v["duration"] > settings["MAX_LENGTH"]: raise MaximumLength("Track exceeded maximum length. See help audioset maxlength") + if v["duration"] > self.settings["MAX_LENGTH"]: raise MaximumLength("Track exceeded maximum length. See help audioset maxlength") if not os.path.isfile("data/audio/cache/" + v["id"]): v = yt.extract_info(url, download=True) audio.downloader = {"DONE" : True, "TITLE" : v["title"], "ID" : v["id"], "URL" : url, "DURATION" : v["duration"], "DOWNLOADING" : False} #Errors out here if invalid link @@ -564,10 +560,6 @@ class Audio: except: return False - def save_settings(self): - with open(main_path + "/data/audio/settings.json", "w") as f: - f.write(json.dumps(settings)) - class EmptyPlayer(): #dummy player def __init__(self): pass @@ -591,24 +583,26 @@ def check_folders(): print("Creating " + folder + " folder...") os.makedirs(folder) -def check_files(n): +def check_files(): + + settings = {"VOLUME" : 0.5, "MAX_LENGTH" : 3700, "QUEUE_MODE" : True} + if not os.path.isfile(main_path + "/data/audio/settings.json"): print("Creating default audio settings.json...") - n.save_settings() + fileIO(main_path + "/data/audio/settings.json", "save", settings) allowed = ["^(https:\/\/www\\.youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/youtu.be\/...........*)", "^(https:\/\/youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/soundcloud\\.com\/.*)"] if not os.path.isfile(main_path + "/data/audio/accepted_links.json"): print("Creating accepted_links.json...") - with open(main_path + "/data/audio/accepted_links.json", "w") as f: - f.write(json.dumps(allowed)) + fileIO(main_path + "/data/audio/accepted_links.json", "save", allowed) def setup(bot): + check_folders() + check_files() loop = asyncio.get_event_loop() n = Audio(bot) - check_folders() - check_files(n) loop.create_task(n.queue_manager()) bot.add_listener(n.incoming_messages, "on_message") bot.add_cog(n) \ No newline at end of file From e581230789f62253caef53e140166a6e2e426fb9 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 04:54:34 +0100 Subject: [PATCH 02/10] image cog polish --- cogs/image.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/cogs/image.py b/cogs/image.py index b2664e32f..7a907146c 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -1,7 +1,10 @@ import discord from discord.ext import commands from random import randint -from imgurpython import ImgurClient +try: + from imgurpython import ImgurClient +except: + print("imgurpython is not installed. Do 'pip install imgurpython' to use this cog.\n") import aiohttp import random @@ -16,8 +19,9 @@ class Image: @commands.command(no_pm=True) async def imgur(self, *text): - """Retrieves a random imgur picture. - imgur search [keyword] - retrieves first hit of search query. + """Retrieves a random imgur picture + + imgur search [keyword] - Retrieves first hit of search query. imgur [subreddit section] [top or new] - retrieves top 3 hottest or latest pictures of today for given a subreddit section, e.g. 'funny'.""" imgurclient = ImgurClient("1fd3ef04daf8cab", "f963e574e8e3c17993c933af4f0522e1dc01e230") if text == (): @@ -46,7 +50,9 @@ class Image: @commands.command(no_pm=True) async def gif(self, *text): - """ gif [keyword] - retrieves first search result from giphy """ + """Retrieves first search result from giphy + + gif [keyword]""" if len(text) > 0: if len(text[0]) > 1 and len(text[0]) < 20: try: @@ -68,7 +74,9 @@ class Image: @commands.command(no_pm=True) async def gifr(self, *text): - """ gifr [keyword] - retrieves a random gif from a giphy search """ + """Retrieves a random gif from a giphy search + + gifr [keyword]""" random.seed() if len(text) > 0: if len(text[0]) > 1 and len(text[0]) < 20: @@ -88,7 +96,7 @@ class Image: else: await self.bot.say("Invalid search.") else: - await self.bot.say("gif [text]") + await self.bot.say("gifr [text]") def setup(bot): bot.add_cog(Image(bot)) From f7fa78403dc0db945df57bed99e21520d5299308 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 05:18:10 +0100 Subject: [PATCH 03/10] More image cog polish --- cogs/image.py | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/cogs/image.py b/cogs/image.py index 7a907146c..28ec5b09e 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -1,10 +1,6 @@ import discord from discord.ext import commands from random import randint -try: - from imgurpython import ImgurClient -except: - print("imgurpython is not installed. Do 'pip install imgurpython' to use this cog.\n") import aiohttp import random @@ -22,7 +18,7 @@ class Image: """Retrieves a random imgur picture imgur search [keyword] - Retrieves first hit of search query. - imgur [subreddit section] [top or new] - retrieves top 3 hottest or latest pictures of today for given a subreddit section, e.g. 'funny'.""" + imgur [subreddit section] [top or new] - Retrieves top 3 hottest or latest pictures of today for given a subreddit section, e.g. 'funny'.""" imgurclient = ImgurClient("1fd3ef04daf8cab", "f963e574e8e3c17993c933af4f0522e1dc01e230") if text == (): rand = randint(0, 59) #60 results per generated page @@ -35,18 +31,21 @@ class Image: else: await self.bot.say(items[0].link) elif text[0] != (): - if text[1] == "top": - imgSort = "top" - elif text[1] == "new": - imgSort = "time" - else: - await self.bot.say("Only top or new is a valid subcommand.") - return - items = imgurclient.subreddit_gallery(text[0], sort=imgSort, window='day', page=0) - if (len(items) < 3): - await self.bot.say("This subreddit section does not exist, try 'funny'") - else: - await self.bot.say("{} {} {}".format(items[0].link, items[1].link, items[2].link)) + try: + if text[1] == "top": + imgSort = "top" + elif text[1] == "new": + imgSort = "time" + else: + await self.bot.say("Only top or new is a valid subcommand.") + return + items = imgurclient.subreddit_gallery(text[0], sort=imgSort, window='day', page=0) + if (len(items) < 3): + await self.bot.say("This subreddit section does not exist, try 'funny'") + else: + await self.bot.say("{} {} {}".format(items[0].link, items[1].link, items[2].link)) + except: + await self.bot.say("Type help imgur for details.") @commands.command(no_pm=True) async def gif(self, *text): @@ -98,5 +97,16 @@ class Image: else: await self.bot.say("gifr [text]") +class ModuleNotFound(Exception): + def __init__(self, m): + self.message = m + def __str__(self): + return self.message + def setup(bot): + global ImgurClient + try: + from imgurpython import ImgurClient + except: + raise ModuleNotFound("imgurpython is not installed. Do 'pip install imgurpython' to use this cog.") bot.add_cog(Image(bot)) From 6385c3614fa844d3adc4d357646f793a999acd9b Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 07:36:50 +0100 Subject: [PATCH 04/10] Fixed !loop, added title to !song Audio now recognizes owner --- cogs/audio.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cogs/audio.py b/cogs/audio.py index 6442773a4..188c244da 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -62,10 +62,9 @@ class Audio: if await self.check_voice(msg.author, msg): if self.is_playlist_valid([link]): # reusing a function if await self.is_alone_or_admin(msg.author): - self.queue = [] - self.playlist = [] + self.queue = [] self.current = -1 - await self.play_video(link) + self.playlist = [link] else: self.playlist = [] self.current = -1 @@ -79,7 +78,9 @@ class Audio: """Shows song title """ if self.downloader["TITLE"]: - await self.bot.say(self.downloader["TITLE"]) + url = "" + if self.downloader["URL"]: url = 'Link : "' + self.downloader["URL"] + '"' + await self.bot.say(self.downloader["TITLE"] + "\n" + url) else: await self.bot.say("No title available.") @@ -182,10 +183,7 @@ class Audio: msg = ctx.message if self.music_player.is_playing(): if await self.is_alone_or_admin(msg.author): - if self.playlist: - self.playlist = self.playlist[[self.current]] - elif self.queue: - self.playlist = self.playlist[[self.queue[0]]] + self.playlist = [self.downloader["URL"]] await self.bot.say("I will play this song on repeat.") else: await self.bot.say("I'm in queue mode. Controls are disabled if you're in a room with multiple people.") @@ -254,6 +252,8 @@ class Audio: async def is_alone_or_admin(self, author): #Direct control. fix everything if not self.settings["QUEUE_MODE"]: return True + elif author.id == checks.settings["OWNER"]: + return True elif discord.utils.get(author.roles, name=checks.settings["ADMIN_ROLE"]) is not None: return True elif discord.utils.get(author.roles, name=checks.settings["MOD_ROLE"]) is not None: From 1e592cb7957ae229d6f41036da44f605149cb9ac Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 07:46:35 +0100 Subject: [PATCH 05/10] Small fixes --- cogs/audio.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cogs/audio.py b/cogs/audio.py index 188c244da..67a526bab 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -65,6 +65,7 @@ class Audio: self.queue = [] self.current = -1 self.playlist = [link] + self.music_player.stop() else: self.playlist = [] self.current = -1 @@ -77,7 +78,7 @@ class Audio: async def song(self): """Shows song title """ - if self.downloader["TITLE"]: + if self.downloader["TITLE"] and "localtracks" not in self.downloader["TITLE"]: url = "" if self.downloader["URL"]: url = 'Link : "' + self.downloader["URL"] + '"' await self.bot.say(self.downloader["TITLE"] + "\n" + url) From 533c59de9769b05f058bf681c2eaaca8294434b7 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 10:32:01 +0100 Subject: [PATCH 06/10] Hotfix for loop, minor typos --- cogs/audio.py | 1 + cogs/image.py | 2 +- red.py | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cogs/audio.py b/cogs/audio.py index 67a526bab..10ec54443 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -184,6 +184,7 @@ class Audio: msg = ctx.message if self.music_player.is_playing(): if await self.is_alone_or_admin(msg.author): + self.current = -1 self.playlist = [self.downloader["URL"]] await self.bot.say("I will play this song on repeat.") else: diff --git a/cogs/image.py b/cogs/image.py index 28ec5b09e..e7e714c04 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -108,5 +108,5 @@ def setup(bot): try: from imgurpython import ImgurClient except: - raise ModuleNotFound("imgurpython is not installed. Do 'pip install imgurpython' to use this cog.") + raise ModuleNotFound("imgurpython is not installed. Do 'pip3 install imgurpython' to use this cog.") bot.add_cog(Image(bot)) diff --git a/red.py b/red.py index 205dd0d7d..4e6f64f74 100644 --- a/red.py +++ b/red.py @@ -187,6 +187,7 @@ async def setprefix(*text): @bot.command(name="uptime") async def _uptime(): + """Shows Red's uptime""" up = abs(bot.uptime - int(time.perf_counter())) up = str(datetime.timedelta(seconds=up)) await bot.say("`Uptime: {}`".format(up)) From 47d683fd2f1f0b4066bd0dbbd75579993d3e8e62 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 14:10:05 +0100 Subject: [PATCH 07/10] Economy cog --- cogs/economy.py | 239 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 cogs/economy.py diff --git a/cogs/economy.py b/cogs/economy.py new file mode 100644 index 000000000..1db000b9c --- /dev/null +++ b/cogs/economy.py @@ -0,0 +1,239 @@ +import discord +from discord.ext import commands +from .utils.dataIO import fileIO +from .utils import checks +from random import randint +import os +import time +import __main__ + +main_path = os.path.dirname(os.path.realpath(__main__.__file__)) + +slot_payouts = """Slot machine payouts: + :two: :two: :six: Bet * 5000 + :four_leaf_clover: :four_leaf_clover: :four_leaf_clover: +1000 + :cherries: :cherries: :cherries: +800 + :two: :six: Bet * 4 + :cherries: :cherries: Bet * 3 + + Three symbols: +500 + Two symbols: Bet * 2""" + + +class Economy: + """Economy + + Get rich and have fun with imaginary currency!""" + + def __init__(self, bot): + self.bot = bot + self.bank = fileIO("data/economy/bank.json", "load") + self.settings = fileIO("data/economy/settings.json", "load") + self.payday_register = {} + + @commands.group(name="bank", pass_context=True) + async def _bank(self, ctx): + """Bank operations""" + if ctx.invoked_subcommand is None: + await self.bot.say("Type help bank for info.") + + @_bank.command(pass_context=True) + async def register(self, ctx): + """Registers an account at the Twentysix bank""" + user = ctx.message.author + if user.id not in self.bank: + self.bank[user.id] = {"name" : user.name, "balance" : 100} + fileIO("data/economy/bank.json", "save", self.bank) + await self.bot.say("{} `Account opened. Current balance: {}`".format(user.mention, str(self.check_balance(user.id)))) + else: + await self.bot.say("{} `You already have an account at the Twentysix bank.`".format(user.mention)) + + @_bank.command(pass_context=True) + async def balance(self, ctx): + """Shows your current balance""" + user = ctx.message.author + if self.account_check(user.id): + await self.bot.say("{} `Your balance is: {}`".format(user.mention, str(self.check_balance(user.id)))) + else: + await self.bot.say("{} `You don't have an account at the Twentysix bank. Type !register to open one.`".format(user.mention, str(self.check_balance(user.id)))) + + @commands.command(pass_context=True, no_pm=True) + async def payday(self, ctx): + """Get some free credits""" + author = ctx.message.author + id = author.id + if self.account_check(id): + if id in self.payday_register: + if abs(self.payday_register[id] - int(time.perf_counter())) >= self.settings["PAYDAY_TIME"]: + self.add_money(id, self.settings["PAYDAY_CREDITS"]) + self.payday_register[id] = int(time.perf_counter()) + await self.bot.say("{} `Here, take some credits. Enjoy! (+{} credits!)`".format(author.mention, str(self.settings["PAYDAY_CREDITS"]))) + else: + await self.bot.say("{} `Too soon. You have to wait {} seconds between each payday.`".format(author.mention, str(self.settings["PAYDAY_TIME"]))) + else: + self.payday_register[id] = int(time.perf_counter()) + self.add_money(id, self.settings["PAYDAY_CREDITS"]) + await self.bot.say("{} `Here, take some credits. Enjoy! (+{} credits!)`".format(author.mention, str(self.settings["PAYDAY_CREDITS"]))) + else: + await self.bot.say("{} `You need an account to receive credits. (!economy)`".format(author.mention)) + + @commands.command(pass_context=True) + async def payouts(self, ctx): + """Shows slot machine payouts""" + await self.bot.send_message(ctx.message.author, slot_payouts) + + @commands.command(pass_context=True, no_pm=True) + async def slot(self, ctx, bid : int): + """Play the slot machine""" + author = ctx.message.author + if self.enough_money(author.id, bid): + if bid >= self.settings["SLOT_MIN"] and bid <= self.settings["SLOT_MAX"]: + await self.slot_machine(ctx.message, bid) + else: + await self.bot.say("{} `Bid must be between {0} and {1}.`".format(author.mention, self.settings["SLOT_MIN"], self.settings["SLOT_MAX"])) + else: + await self.bot.say("{0} `You need an account with enough funds to play the slot machine.`".format(author.mention)) + + async def slot_machine(self, message, bid): + reel_pattern = [":cherries:", ":cookie:", ":two:", ":four_leaf_clover:", ":cyclone:", ":sunflower:", ":six:", ":mushroom:", ":heart:", ":snowflake:"] + padding_before = [":mushroom:", ":heart:", ":snowflake:"] # padding prevents index errors + padding_after = [":cherries:", ":cookie:", ":two:"] + reel = padding_before + reel_pattern + padding_after + reels = [] + for i in range(0, 3): + n = randint(3,12) + reels.append([reel[n - 1], reel[n], reel[n + 1]]) + line = [reels[0][1], reels[1][1], reels[2][1]] + + display_reels = " " + reels[0][0] + " " + reels[1][0] + " " + reels[2][0] + "\n" + display_reels += ">" + reels[0][1] + " " + reels[1][1] + " " + reels[2][1] + "\n" + display_reels += " " + reels[0][2] + " " + reels[1][2] + " " + reels[2][2] + "\n" + + if line[0] == ":two:" and line[1] == ":two:" and line[2] == ":six:": + bid = bid * 5000 + await self.bot.send_message(message.channel, "{}{} `226! Your bet is multiplied * 5000! {}!` ".format(display_reels, message.author.mention, str(bid))) + elif line[0] == ":four_leaf_clover:" and line[1] == ":four_leaf_clover:" and line[2] == ":four_leaf_clover:": + bid += 1000 + await self.bot.send_message(message.channel, "{}{} `Three FLC! +1000!` ".format(display_reels, message.author.mention)) + elif line[0] == ":cherries:" and line[1] == ":cherries:" and line[2] == ":cherries:": + bid += 800 + await self.bot.send_message(message.channel, "{}{} `Three cherries! +800!` ".format(display_reels, message.author.mention)) + elif line[0] == line[1] == line[2]: + bid += 500 + await self.bot.send_message(message.channel, "{}{} `Three symbols! +500!` ".format(display_reels, message.author.mention)) + elif line[0] == ":two:" and line[1] == ":six:" or line[1] == ":two:" and line[2] == ":six:": + bid = bid * 4 + await self.bot.send_message(message.channel, "{}{} `26! Your bet is multiplied * 4! {}!` ".format(display_reels, message.author.mention, str(bid))) + elif line[0] == ":cherries:" and line[1] == ":cherries:" or line[1] == ":cherries:" and line[2] == ":cherries:": + bid = bid * 3 + await self.bot.send_message(message.channel, "{}{} `Two cherries! Your bet is multiplied * 3! {}!` ".format(display_reels, message.author.mention, str(bid))) + elif line[0] == line[1] or line[1] == line[2]: + bid = bid * 2 + await self.bot.send_message(message.channel, "{}{} `Two symbols! Your bet is multiplied * 2! {}!` ".format(display_reels, message.author.mention, str(bid))) + else: + await self.bot.send_message(message.channel, "{}{} `Nothing! Lost bet.` ".format(display_reels, message.author.mention)) + self.withdraw_money(message.author.id, bid) + await self.bot.send_message(message.channel, "`Credits left: {}`".format(str(self.check_balance(message.author.id)))) + return True + self.add_money(message.author.id, bid) + await self.bot.send_message(message.channel, "`Current credits: {}`".format(str(self.check_balance(message.author.id)))) + + @commands.group(pass_context=True, no_pm=True) + @checks.mod_or_permissions() + async def economyset(self, ctx): + """Changes economy module settings""" + if ctx.invoked_subcommand is None: + msg = "```" + for k, v in self.settings.items(): + msg += str(k) + ": " + str(v) + "\n" + msg += "\nType help economyset to see the list of commands.```" + await self.bot.say(msg) + + @economyset.command() + async def slotmin(self, bid : int): + """Minimum slot machine bid""" + self.settings["SLOT_MIN"] = bid + await self.bot.say("Minimum bid is now " + str(bid) + " credits.") + fileIO(main_path + "/data/economy/settings.json", "save", self.settings) + + @economyset.command() + async def slotmax(self, bid : int): + """Maximum slot machine bid""" + self.settings["SLOT_MAX"] = bid + await self.bot.say("Maximum bid is now " + str(bid) + " credits.") + fileIO(main_path + "/data/economy/settings.json", "save", self.settings) + + @economyset.command() + async def paydaytime(self, seconds : int): + """Seconds between each payday""" + self.settings["PAYDAY_TIME"] = seconds + await self.bot.say("Value modified. At least " + str(seconds) + " seconds must pass between each payday.") + fileIO(main_path + "/data/economy/settings.json", "save", self.settings) + + @economyset.command() + async def paydaycredits(self, credits : int): + """Credits earned each payday""" + self.settings["PAYDAY_CREDITS"] = credits + await self.bot.say("Every payday will now give " + str(credits) + " credits.") + fileIO(main_path + "/data/economy/settings.json", "save", self.settings) + + def account_check(self, id): + if id in self.bank: + return True + else: + return False + + def check_balance(self, id): + if self.account_check(id): + return self.bank[id]["balance"] + else: + return False + + def add_money(self, id, amount): + if self.account_check(id): + self.bank[id]["balance"] = self.bank[id]["balance"] + int(amount) + fileIO("data/economy/bank.json", "save", self.bank) + else: + return False + + def withdraw_money(self, id, amount): + if self.account_check(id): + if self.bank[id]["balance"] >= int(amount): + self.bank[id]["balance"] = self.bank[id]["balance"] - int(amount) + fileIO("data/economy/bank.json", "save", self.bank) + else: + return False + else: + return False + + def enough_money(self, id, amount): + if self.account_check(id): + if self.bank[id]["balance"] >= int(amount): + return True + else: + return False + else: + return False + +def check_folders(): + if not os.path.exists("data/economy"): + print("Creating data/economy folder...") + os.makedirs("data/economy") + +def check_files(): + settings = {"PAYDAY_TIME" : 300, "PAYDAY_CREDITS" : 120, "SLOT_MIN" : 5, "SLOT_MAX" : 100} + + f = "data/economy/settings.json" + if not fileIO(f, "check"): + print("Creating default economy's settings.json...") + fileIO(f, "save", settings) + + f = "data/economy/bank.json" + if not fileIO(f, "check"): + print("Creating empty bank.json...") + fileIO(f, "save", {}) + +def setup(bot): + check_folders() + check_files() + bot.add_cog(Economy(bot)) \ No newline at end of file From 885139cb9ff883b76634229c90d6d1ecf668dfbd Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 14:15:27 +0100 Subject: [PATCH 08/10] Typo --- cogs/economy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/economy.py b/cogs/economy.py index 1db000b9c..5657cb540 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -37,7 +37,7 @@ class Economy: if ctx.invoked_subcommand is None: await self.bot.say("Type help bank for info.") - @_bank.command(pass_context=True) + @_bank.command(pass_context=True, no_pm=True) async def register(self, ctx): """Registers an account at the Twentysix bank""" user = ctx.message.author From 6518c6b8d8aeabdac0df9039897ed543fd02ee86 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 14:37:32 +0100 Subject: [PATCH 09/10] No comment I don't want to talk about this --- cogs/audio.py | 17 +++++++---------- cogs/economy.py | 12 ++++-------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/cogs/audio.py b/cogs/audio.py index 10ec54443..6c4a8ebc5 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -12,14 +12,11 @@ import glob import re import aiohttp from bs4 import BeautifulSoup -import __main__ import json if not discord.opus.is_loaded(): discord.opus.load_opus('libopus-0.dll') -main_path = os.path.dirname(os.path.realpath(__main__.__file__)) - youtube_dl_options = { 'format': 'bestaudio/best', 'extractaudio': True, @@ -344,14 +341,14 @@ class Audio: else: await self.bot.say("Queue status can be either on or off.") return - fileIO(main_path + "/data/audio/settings.json", "save", self.settings) + fileIO("data/audio/settings.json", "save", self.settings) @audioset.command() async def maxlength(self, length : int): """Maximum track length for requested links""" self.settings["MAX_LENGTH"] = length await self.bot.say("Maximum length is now " + str(length) + " seconds.") - fileIO(main_path + "/data/audio/settings.json", "save", self.settings) + fileIO("data/audio/settings.json", "save", self.settings) @audioset.command() async def volume(self, level : float): @@ -359,7 +356,7 @@ class Audio: if level >= 0 and level <= 1: self.settings["VOLUME"] = level await self.bot.say("Volume is now set at " + str(level) + ". It will take effect after the current track.") - fileIO(main_path + "/data/audio/settings.json", "save", self.settings) + fileIO("data/audio/settings.json", "save", self.settings) else: await self.bot.say("Volume must be between 0 and 1. Example: 0.40") @@ -589,16 +586,16 @@ def check_files(): settings = {"VOLUME" : 0.5, "MAX_LENGTH" : 3700, "QUEUE_MODE" : True} - if not os.path.isfile(main_path + "/data/audio/settings.json"): + if not os.path.isfile("data/audio/settings.json"): print("Creating default audio settings.json...") - fileIO(main_path + "/data/audio/settings.json", "save", settings) + fileIO("data/audio/settings.json", "save", settings) allowed = ["^(https:\/\/www\\.youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/youtu.be\/...........*)", "^(https:\/\/youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/soundcloud\\.com\/.*)"] - if not os.path.isfile(main_path + "/data/audio/accepted_links.json"): + if not os.path.isfile("data/audio/accepted_links.json"): print("Creating accepted_links.json...") - fileIO(main_path + "/data/audio/accepted_links.json", "save", allowed) + fileIO("data/audio/accepted_links.json", "save", allowed) def setup(bot): check_folders() diff --git a/cogs/economy.py b/cogs/economy.py index 5657cb540..b16fb170e 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -5,9 +5,6 @@ from .utils import checks from random import randint import os import time -import __main__ - -main_path = os.path.dirname(os.path.realpath(__main__.__file__)) slot_payouts = """Slot machine payouts: :two: :two: :six: Bet * 5000 @@ -19,7 +16,6 @@ slot_payouts = """Slot machine payouts: Three symbols: +500 Two symbols: Bet * 2""" - class Economy: """Economy @@ -154,28 +150,28 @@ class Economy: """Minimum slot machine bid""" self.settings["SLOT_MIN"] = bid await self.bot.say("Minimum bid is now " + str(bid) + " credits.") - fileIO(main_path + "/data/economy/settings.json", "save", self.settings) + fileIO("data/economy/settings.json", "save", self.settings) @economyset.command() async def slotmax(self, bid : int): """Maximum slot machine bid""" self.settings["SLOT_MAX"] = bid await self.bot.say("Maximum bid is now " + str(bid) + " credits.") - fileIO(main_path + "/data/economy/settings.json", "save", self.settings) + fileIO("data/economy/settings.json", "save", self.settings) @economyset.command() async def paydaytime(self, seconds : int): """Seconds between each payday""" self.settings["PAYDAY_TIME"] = seconds await self.bot.say("Value modified. At least " + str(seconds) + " seconds must pass between each payday.") - fileIO(main_path + "/data/economy/settings.json", "save", self.settings) + fileIO("data/economy/settings.json", "save", self.settings) @economyset.command() async def paydaycredits(self, credits : int): """Credits earned each payday""" self.settings["PAYDAY_CREDITS"] = credits await self.bot.say("Every payday will now give " + str(credits) + " credits.") - fileIO(main_path + "/data/economy/settings.json", "save", self.settings) + fileIO("data/economy/settings.json", "save", self.settings) def account_check(self, id): if id in self.bank: From 6def72334745a2c2b01ae13c93e5de0d69af6c1d Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 11 Feb 2016 21:04:19 +0100 Subject: [PATCH 10/10] Typo --- cogs/economy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/economy.py b/cogs/economy.py index b16fb170e..f6e773ea2 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -86,7 +86,7 @@ class Economy: if bid >= self.settings["SLOT_MIN"] and bid <= self.settings["SLOT_MAX"]: await self.slot_machine(ctx.message, bid) else: - await self.bot.say("{} `Bid must be between {0} and {1}.`".format(author.mention, self.settings["SLOT_MIN"], self.settings["SLOT_MAX"])) + await self.bot.say("{0} `Bid must be between {1} and {2}.`".format(author.mention, self.settings["SLOT_MIN"], self.settings["SLOT_MAX"])) else: await self.bot.say("{0} `You need an account with enough funds to play the slot machine.`".format(author.mention))