From 1d493b8e9db8f85e552ae8ebebd3ba15d6c4cd86 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:10:38 -0400 Subject: [PATCH 01/17] v3 initial commit --- hangman/__init__.py | 7 + hangman/hangman.py | 384 ++++++++++++++++++++------------------------ 2 files changed, 183 insertions(+), 208 deletions(-) create mode 100644 hangman/__init__.py diff --git a/hangman/__init__.py b/hangman/__init__.py new file mode 100644 index 0000000..aee87e2 --- /dev/null +++ b/hangman/__init__.py @@ -0,0 +1,7 @@ +from .hangman import Hangman + + +def setup(bot): + n = Hangman(bot) + bot.add_cog(n) + bot.add_listener(n._on_react, "on_reaction_add") \ No newline at end of file diff --git a/hangman/hangman.py b/hangman/hangman.py index 987b3a2..2b3776e 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -1,11 +1,11 @@ import discord import os +from collections import defaultdict from discord.ext import commands from random import randint -from .utils.dataIO import dataIO -from .utils import checks +from redbot.core import Config, checks, RedContext class Hangman: @@ -13,190 +13,187 @@ class Hangman: def __init__(self, bot): self.bot = bot - self.path = "data/Fox-Cogs/hangman" - self.file_path = "data/Fox-Cogs/hangman/hangman.json" - self.answer_path = "data/hangman/hanganswers.txt" - self.the_data = dataIO.load_json(self.file_path) - self.winbool = False + self.config = Config.get_conf(self, identifier=1049711010310997110) + default_guild = { + "theface": ':thinking:', + } + + self.config.register_guild(**default_guild) + + self.the_data = defaultdict(lambda:{"running": False, "hangman": 0, "guesses": [], "trackmessage": False, "answer": ''}) + self.answer_path = "hangman/data/hanganswers.txt" + self.winbool = defaultdict(lambda: False) + self.letters = "🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿" self.navigate = "🔼🔽" - self._updateHanglist() - - def _updateHanglist(self): - self.hanglist = ( - """> - \_________ - |/ - | - | - | - | - | - |\___ - """, - - """> - \_________ - |/ | - | - | - | - | - | - |\___ - H""", - - """> - \_________ - |/ | - | """+self.the_data["theface"]+""" - | - | - | - | - |\___ - HA""", - - """> - \________ - |/ | - | """+self.the_data["theface"]+""" - | | - | | - | - | - |\___ - HAN""", - - - """> - \_________ - |/ | - | """+self.the_data["theface"]+""" - | /| - | | - | - | - |\___ - HANG""", - - - """> - \_________ - |/ | - | """+self.the_data["theface"]+""" - | /|\ - | | - | - | - |\___ - HANGM""", - - - - """> - \________ - |/ | - | """+self.the_data["theface"]+""" - | /|\ - | | - | / - | - |\___ - HANGMA""", - - - """> - \________ - |/ | - | """+self.the_data["theface"]+""" - | /|\ - | | - | / \ - | - |\___ - HANGMAN""") - - def save_data(self): - """Saves the json""" - dataIO.save_json(self.file_path, self.the_data) - + + self.hanglist = {} + + async def _update_hanglist(self): + for guild in self.bot.guilds: + theface = await self.config.guild(guild).theface() + self.hanglist[guild] = ( + """> + \_________ + |/ + | + | + | + | + | + |\___ + """, + + """> + \_________ + |/ | + | + | + | + | + | + |\___ + H""", + + """> + \_________ + |/ | + | """+theface+""" + | + | + | + | + |\___ + HA""", + + """> + \________ + |/ | + | """+theface+""" + | | + | | + | + | + |\___ + HAN""", + + + """> + \_________ + |/ | + | """+theface+""" + | /| + | | + | + | + |\___ + HANG""", + + + """> + \_________ + |/ | + | """+theface+""" + | /|\ + | | + | + | + |\___ + HANGM""", + + + + """> + \________ + |/ | + | """+theface+""" + | /|\ + | | + | / + | + |\___ + HANGMA""", + + + """> + \________ + |/ | + | """+theface+""" + | /|\ + | | + | / \ + | + |\___ + HANGMAN""") + @commands.group(aliases=['sethang'], pass_context=True) @checks.mod_or_permissions(administrator=True) async def hangset(self, ctx): """Adjust hangman settings""" - if ctx.invoked_subcommand is None: - await self.bot.send_cmd_help(ctx) - + if not ctx.invoked_subcommand: + await ctx.send_help() + @hangset.command(pass_context=True) - async def face(self, ctx, theface): + async def face(self, ctx: commands.Context, theface): message = ctx.message #Borrowing FlapJack's emoji validation (https://github.com/flapjax/FlapJack-Cogs/blob/master/smartreact/smartreact.py) if theface[:2] == "<:": - theface = [r for server in self.bot.servers for r in server.emojis if r.id == theface.split(':')[2][:-1]][0] - + theface = [r for r in self.bot.emojis if r.id == theface.split(':')[2][:-1]][0] + try: # Use the face as reaction to see if it's valid (THANKS FLAPJACK <3) - await self.bot.add_reaction(message, theface) - self.the_data["theface"] = str(theface) - self.save_data() - self._updateHanglist() - await self.bot.say("Face has been updated!") - + await message.add_reaction(theface) except discord.errors.HTTPException: - await self.bot.say("That's not an emoji I recognize.") + await ctx.send("That's not an emoji I recognize.") + return + + await self.config.guild(ctx.guild).theface.set(theface) + await self._update_hanglist() + await ctx.send("Face has been updated!") @commands.command(aliases=['hang'], pass_context=True) async def hangman(self, ctx, guess: str=None): """Play a game of hangman against the bot!""" if guess is None: - if self.the_data["running"]: - await self.bot.say("Game of hangman is already running!\nEnter your guess!") - self._printgame() + if self.the_data[ctx.guild]["running"]: + await ctx.send("Game of hangman is already running!\nEnter your guess!") + self._printgame(ctx.channel) """await self.bot.send_cmd_help(ctx)""" else: - await self.bot.say("Starting a game of hangman!") - self._startgame() - await self._printgame() - elif not self.the_data["running"]: - await self.bot.say("Game of hangman is not yet running!\nStarting a game of hangman!") - self._startgame() - await self._printgame() + await ctx.send("Starting a game of hangman!") + self._startgame(ctx.guild) + await self._printgame(ctx.channel) + elif not self.the_data[ctx.guild]["running"]: + await ctx.send("Game of hangman is not yet running!\nStarting a game of hangman!") + self._startgame(ctx.guild) + await self._printgame(ctx.channel) else: - await self._guessletter(guess) + await self._guessletter(guess, ctx.channel) - - def _startgame(self): + def _startgame(self, guild): """Starts a new game of hangman""" - self.the_data["answer"] = self._getphrase().upper() - self.the_data["hangman"] = 0 - self.the_data["guesses"] = [] - self.winbool = False - self.the_data["running"] = True - self.the_data["trackmessage"] = False - self.save_data() + self.the_data[guild]["answer"] = self._getphrase().upper() + self.the_data[guild]["hangman"] = 0 + self.the_data[guild]["guesses"] = [] + self.winbool[guild] = False + self.the_data[guild]["running"] = True + self.the_data[guild]["trackmessage"] = False - def _stopgame(self): + def _stopgame(self, guild): """Stops the game in current state""" - self.the_data["running"] = False - self.save_data() - - async def _checkdone(self, channel=None): - if self.winbool: - if channel: - await self.bot.send_message(channel, "You Win!") - else: - await self.bot.say("You Win!") - self._stopgame() - - if self.the_data["hangman"] >= 7: - if channel: - await self.bot.send_message(channel, "You Lose!\nThe Answer was: **"+self.the_data["answer"]+"**") - else: - await self.bot.say("You Lose!\nThe Answer was: **"+self.the_data["answer"]+"**") + self.the_data[guild]["running"] = False + + async def _checkdone(self, channel): + if self.winbool[channel.guild]: + await channel.send("You Win!") + self._stopgame(channel.guild) + return - self._stopgame() + if self.the_data[channel.guild]["hangman"] >= 7: + await channel.send("You Lose!\nThe Answer was: **"+self.the_data[channel.guild]["answer"]+"**") + + self._stopgame(channel.guild) def _getphrase(self): """Get a new phrase for the game and returns it""" @@ -206,7 +203,6 @@ class Hangman: outphrase = "" while outphrase == "": outphrase = phrases[randint(0, len(phrases)-1)].partition(" (")[0] -# outphrase = phrases[randint(0,10)].partition(" (")[0] return outphrase def _hideanswer(self): @@ -234,27 +230,19 @@ class Hangman: return out_str - async def _guessletter(self, guess, channel=None): + async def _guessletter(self, guess, channel): """Checks the guess on a letter and prints game if acceptable guess""" - if not guess.upper() in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or not len(guess) == 1: - if channel: - await self.bot.send_message(channel, "Invalid guess. Only A-Z is accepted") - else: - await self.bot.say("Invalid guess. Only A-Z is accepted") + if guess.upper() not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or len(guess) != 1: + await channel.send("Invalid guess. Only A-Z is accepted") return - if guess.upper() in self.the_data["guesses"]: - if channel: - await self.bot.send_message(channel, "Already guessed that! Try again") - else: - await self.bot.say("Already guessed that! Try again") + if guess.upper() in self.the_data[channel.guild]["guesses"]: + await channel.send("Already guessed that! Try again") return - - if not guess.upper() in self.the_data["answer"]: + if guess.upper() not in self.the_data[channel.guild]["answer"]: self.the_data["hangman"] += 1 - self.the_data["guesses"].append(guess.upper()) - self.save_data() + self.the_data[channel.guild]["guesses"].append(guess.upper()) await self._printgame(channel) @@ -290,66 +278,46 @@ class Hangman: async def _reactmessage_menu(self, message): """React with menu options""" - await self.bot.clear_reactions(message) + await message.clear_reactions() - await self.bot.add_reaction(message, self.navigate[0]) - await self.bot.add_reaction(message, self.navigate[-1]) + await message.add_reaction(self.navigate[0]) + await message.add_reaction(self.navigate[-1]) async def _reactmessage_am(self, message): - await self.bot.clear_reactions(message) + await message.clear_reactions() for x in range(len(self.letters)): if x in [i for i,b in enumerate("ABCDEFGHIJKLM") if b not in self._guesslist()]: - await self.bot.add_reaction(message, self.letters[x]) + await message.add_reaction(self.letters[x]) - await self.bot.add_reaction(message, self.navigate[-1]) - + await message.add_reaction(self.navigate[-1]) async def _reactmessage_nz(self, message): await self.bot.clear_reactions(message) for x in range(len(self.letters)): if x in [i for i,b in enumerate("NOPQRSTUVWXYZ") if b not in self._guesslist()]: - await self.bot.add_reaction(message, self.letters[x+13]) + await message.add_reaction(self.letters[x+13]) - await self.bot.add_reaction(message, self.navigate[0]) + await message.add_reaction(self.navigate[0]) - async def _printgame(self, channel=None): + async def _printgame(self, channel): """Print the current state of game""" cSay = ("Guess this: " + str(self._hideanswer()) + "\n" + "Used Letters: " + str(self._guesslist()) + "\n" + self.hanglist[self.the_data["hangman"]] + "\n" + self.navigate[0]+" for A-M, "+self.navigate[-1]+" for N-Z") - if channel: - message = await self.bot.send_message(channel, cSay) - else: - message = await self.bot.say(cSay) - + + message = await channel.send(cSay) + self.the_data["trackmessage"] = message.id - self.save_data() + await self._reactmessage_menu(message) await self._checkdone(channel) - -def check_folders(): - if not os.path.exists("data/Fox-Cogs"): - print("Creating data/Fox-Cogs folder...") - os.makedirs("data/Fox-Cogs") - - if not os.path.exists("data/Fox-Cogs/hangman"): - print("Creating data/Fox-Cogs/hangman folder...") - os.makedirs("data/Fox-Cogs/hangman") - - -def check_files(): - if not dataIO.is_valid_json("data/Fox-Cogs/hangman/hangman.json"): - dataIO.save_json("data/Fox-Cogs/hangman/hangman.json", {"running": False, "hangman": 0, "guesses": [], "theface": "<:never:336861463446814720>", "trackmessage": False}) - def setup(bot): - check_folders() - check_files() n = Hangman(bot) bot.add_cog(n) bot.add_listener(n._on_react, "on_reaction_add") From 601c362308e5369303f80b29cc1528fd320801a0 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:13:41 -0400 Subject: [PATCH 02/17] info update --- hangman/info.json | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/hangman/info.json b/hangman/info.json index ac146f1..d845d0f 100644 --- a/hangman/info.json +++ b/hangman/info.json @@ -1,14 +1,20 @@ { - "AUTHOR": "Bobloy", - "DESCRIPTION": "Hangman Cog for Red Discord bot. Play a game of Hangman with your friends!", - "INSTALL_MSG": "Thank you for installing Hangman! Play with [p]hangman, edit with [p]hangset", - "NAME": "Hangman", - "SHORT": "Play a game of Hangman with your friends!", - "TAGS": [ - "fox", - "bobloy", - "fun", - "game" - ], - "HIDDEN": false + "author": [ + "Bobloy" + ], + "bot_version": [ + 3, + 0, + 0 + ], + "description": "Play Hangman with your friends", + "hidden": false, + "install_msg": "Thank you for installing Hangman!", + "requirements": [], + "short": "Play Hangman", + "tags": [ + "game", + "fun", + "bobloy" + ] } \ No newline at end of file From d88e8040d75c7378051f73ed793798a6a1bda3c0 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:20:47 -0400 Subject: [PATCH 03/17] reformat pep8 --- hangman/__init__.py | 2 +- hangman/hangman.py | 109 +++++++++++++++++++------------------------- hangman/info.json | 2 +- 3 files changed, 50 insertions(+), 63 deletions(-) diff --git a/hangman/__init__.py b/hangman/__init__.py index aee87e2..8b6ec76 100644 --- a/hangman/__init__.py +++ b/hangman/__init__.py @@ -4,4 +4,4 @@ from .hangman import Hangman def setup(bot): n = Hangman(bot) bot.add_cog(n) - bot.add_listener(n._on_react, "on_reaction_add") \ No newline at end of file + bot.add_listener(n._on_react, "on_reaction_add") diff --git a/hangman/hangman.py b/hangman/hangman.py index 2b3776e..7e6376d 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -1,11 +1,9 @@ -import discord -import os - from collections import defaultdict -from discord.ext import commands from random import randint -from redbot.core import Config, checks, RedContext +import discord +from discord.ext import commands +from redbot.core import Config, checks class Hangman: @@ -20,7 +18,8 @@ class Hangman: self.config.register_guild(**default_guild) - self.the_data = defaultdict(lambda:{"running": False, "hangman": 0, "guesses": [], "trackmessage": False, "answer": ''}) + self.the_data = defaultdict( + lambda: {"running": False, "hangman": 0, "guesses": [], "trackmessage": False, "answer": ''}) self.answer_path = "hangman/data/hanganswers.txt" self.winbool = defaultdict(lambda: False) @@ -58,7 +57,7 @@ class Hangman: """> \_________ |/ | - | """+theface+""" + | """ + theface + """ | | | @@ -69,7 +68,7 @@ class Hangman: """> \________ |/ | - | """+theface+""" + | """ + theface + """ | | | | | @@ -77,11 +76,10 @@ class Hangman: |\___ HAN""", - """> \_________ |/ | - | """+theface+""" + | """ + theface + """ | /| | | | @@ -89,11 +87,10 @@ class Hangman: |\___ HANG""", - """> \_________ |/ | - | """+theface+""" + | """ + theface + """ | /|\ | | | @@ -101,12 +98,10 @@ class Hangman: |\___ HANGM""", - - """> \________ |/ | - | """+theface+""" + | """ + theface + """ | /|\ | | | / @@ -114,11 +109,10 @@ class Hangman: |\___ HANGMA""", - """> \________ |/ | - | """+theface+""" + | """ + theface + """ | /|\ | | | / \ @@ -136,7 +130,7 @@ class Hangman: @hangset.command(pass_context=True) async def face(self, ctx: commands.Context, theface): message = ctx.message - #Borrowing FlapJack's emoji validation (https://github.com/flapjax/FlapJack-Cogs/blob/master/smartreact/smartreact.py) + # Borrowing FlapJack's emoji validation (https://github.com/flapjax/FlapJack-Cogs/blob/master/smartreact/smartreact.py) if theface[:2] == "<:": theface = [r for r in self.bot.emojis if r.id == theface.split(':')[2][:-1]][0] @@ -150,9 +144,9 @@ class Hangman: await self.config.guild(ctx.guild).theface.set(theface) await self._update_hanglist() await ctx.send("Face has been updated!") - + @commands.command(aliases=['hang'], pass_context=True) - async def hangman(self, ctx, guess: str=None): + async def hangman(self, ctx, guess: str = None): """Play a game of hangman against the bot!""" if guess is None: if self.the_data[ctx.guild]["running"]: @@ -167,10 +161,9 @@ class Hangman: await ctx.send("Game of hangman is not yet running!\nStarting a game of hangman!") self._startgame(ctx.guild) await self._printgame(ctx.channel) - else: + else: await self._guessletter(guess, ctx.channel) - def _startgame(self, guild): """Starts a new game of hangman""" self.the_data[guild]["answer"] = self._getphrase().upper() @@ -179,7 +172,7 @@ class Hangman: self.winbool[guild] = False self.the_data[guild]["running"] = True self.the_data[guild]["trackmessage"] = False - + def _stopgame(self, guild): """Stops the game in current state""" self.the_data[guild]["running"] = False @@ -189,47 +182,47 @@ class Hangman: await channel.send("You Win!") self._stopgame(channel.guild) return - + if self.the_data[channel.guild]["hangman"] >= 7: - await channel.send("You Lose!\nThe Answer was: **"+self.the_data[channel.guild]["answer"]+"**") + await channel.send("You Lose!\nThe Answer was: **" + self.the_data[channel.guild]["answer"] + "**") self._stopgame(channel.guild) - + def _getphrase(self): """Get a new phrase for the game and returns it""" phrasefile = open(self.answer_path, 'r') phrases = phrasefile.readlines() - + outphrase = "" while outphrase == "": - outphrase = phrases[randint(0, len(phrases)-1)].partition(" (")[0] + outphrase = phrases[randint(0, len(phrases) - 1)].partition(" (")[0] return outphrase - + def _hideanswer(self): """Returns the obscured answer""" out_str = "" - + self.winbool = True for i in self.the_data["answer"]: if i == " " or i == "-": - out_str += i*2 + out_str += i * 2 elif i in self.the_data["guesses"] or i not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": - out_str += "__"+i+"__ " + out_str += "__" + i + "__ " else: out_str += "**\_** " self.winbool = False - + return out_str - + def _guesslist(self): """Returns the current letter list""" out_str = "" for i in self.the_data["guesses"]: out_str += str(i) + "," out_str = out_str[:-1] - + return out_str - + async def _guessletter(self, guess, channel): """Checks the guess on a letter and prints game if acceptable guess""" if guess.upper() not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or len(guess) != 1: @@ -241,73 +234,68 @@ class Hangman: return if guess.upper() not in self.the_data[channel.guild]["answer"]: self.the_data["hangman"] += 1 - + self.the_data[channel.guild]["guesses"].append(guess.upper()) - + await self._printgame(channel) - + async def _on_react(self, reaction, user): """ Thanks to flapjack reactpoll for guidelines https://github.com/flapjax/FlapJack-Cogs/blob/master/reactpoll/reactpoll.py""" - - - + if not self.the_data["trackmessage"]: return - + if user == self.bot.user: return # Don't remove bot's own reactions message = reaction.message emoji = reaction.emoji - + if not message.id == self.the_data["trackmessage"]: return - + if str(emoji) in self.letters: letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[self.letters.index(str(emoji))] await self._guessletter(letter, message.channel) - - + if str(emoji) in self.navigate: if str(emoji) == self.navigate[0]: await self._reactmessage_am(message) - + if str(emoji) == self.navigate[-1]: await self._reactmessage_nz(message) - - + async def _reactmessage_menu(self, message): """React with menu options""" await message.clear_reactions() - + await message.add_reaction(self.navigate[0]) await message.add_reaction(self.navigate[-1]) - + async def _reactmessage_am(self, message): await message.clear_reactions() for x in range(len(self.letters)): - if x in [i for i,b in enumerate("ABCDEFGHIJKLM") if b not in self._guesslist()]: + if x in [i for i, b in enumerate("ABCDEFGHIJKLM") if b not in self._guesslist()]: await message.add_reaction(self.letters[x]) - + await message.add_reaction(self.navigate[-1]) async def _reactmessage_nz(self, message): await self.bot.clear_reactions(message) for x in range(len(self.letters)): - if x in [i for i,b in enumerate("NOPQRSTUVWXYZ") if b not in self._guesslist()]: - await message.add_reaction(self.letters[x+13]) - - await message.add_reaction(self.navigate[0]) + if x in [i for i, b in enumerate("NOPQRSTUVWXYZ") if b not in self._guesslist()]: + await message.add_reaction(self.letters[x + 13]) + await message.add_reaction(self.navigate[0]) async def _printgame(self, channel): """Print the current state of game""" cSay = ("Guess this: " + str(self._hideanswer()) + "\n" + "Used Letters: " + str(self._guesslist()) + "\n" + self.hanglist[self.the_data["hangman"]] + "\n" - + self.navigate[0]+" for A-M, "+self.navigate[-1]+" for N-Z") + + self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z") message = await channel.send(cSay) @@ -315,10 +303,9 @@ class Hangman: await self._reactmessage_menu(message) await self._checkdone(channel) - + def setup(bot): n = Hangman(bot) bot.add_cog(n) bot.add_listener(n._on_react, "on_reaction_add") - diff --git a/hangman/info.json b/hangman/info.json index d845d0f..655f00c 100644 --- a/hangman/info.json +++ b/hangman/info.json @@ -14,7 +14,7 @@ "short": "Play Hangman", "tags": [ "game", - "fun", + "fun", "bobloy" ] } \ No newline at end of file From 8379bd1572bdea3c4d1d6f0b1aefb283a64efe75 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:27:59 -0400 Subject: [PATCH 04/17] more guild nonsense --- hangman/hangman.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 7e6376d..695df84 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -198,26 +198,26 @@ class Hangman: outphrase = phrases[randint(0, len(phrases) - 1)].partition(" (")[0] return outphrase - def _hideanswer(self): + def _hideanswer(self, guild): """Returns the obscured answer""" out_str = "" self.winbool = True - for i in self.the_data["answer"]: + for i in self.the_data[guild]["answer"]: if i == " " or i == "-": out_str += i * 2 - elif i in self.the_data["guesses"] or i not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": + elif i in self.the_data[guild]["guesses"] or i not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": out_str += "__" + i + "__ " else: out_str += "**\_** " - self.winbool = False + self.winbool[guild] = False return out_str - def _guesslist(self): + def _guesslist(self, guild): """Returns the current letter list""" out_str = "" - for i in self.the_data["guesses"]: + for i in self.the_data[guild]["guesses"]: out_str += str(i) + "," out_str = out_str[:-1] @@ -233,7 +233,7 @@ class Hangman: await channel.send("Already guessed that! Try again") return if guess.upper() not in self.the_data[channel.guild]["answer"]: - self.the_data["hangman"] += 1 + self.the_data[channel.guild]["hangman"] += 1 self.the_data[channel.guild]["guesses"].append(guess.upper()) @@ -243,7 +243,7 @@ class Hangman: """ Thanks to flapjack reactpoll for guidelines https://github.com/flapjax/FlapJack-Cogs/blob/master/reactpoll/reactpoll.py""" - if not self.the_data["trackmessage"]: + if reaction.message.id != self.the_data[user.guild]["trackmessage"]: return if user == self.bot.user: @@ -251,9 +251,6 @@ class Hangman: message = reaction.message emoji = reaction.emoji - if not message.id == self.the_data["trackmessage"]: - return - if str(emoji) in self.letters: letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[self.letters.index(str(emoji))] await self._guessletter(letter, message.channel) @@ -292,14 +289,14 @@ class Hangman: async def _printgame(self, channel): """Print the current state of game""" - cSay = ("Guess this: " + str(self._hideanswer()) + "\n" - + "Used Letters: " + str(self._guesslist()) + "\n" - + self.hanglist[self.the_data["hangman"]] + "\n" + cSay = ("Guess this: " + str(self._hideanswer(channel.guild)) + "\n" + + "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" + + self.hanglist[self.the_data[channel.guild]["hangman"]] + "\n" + self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z") message = await channel.send(cSay) - self.the_data["trackmessage"] = message.id + self.the_data[channel.guild]["trackmessage"] = message.id await self._reactmessage_menu(message) await self._checkdone(channel) From 619b62336f3d14fbd558c853a38dae454dd5d75b Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:30:15 -0400 Subject: [PATCH 05/17] more guild --- hangman/hangman.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 695df84..3e25c22 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -130,7 +130,8 @@ class Hangman: @hangset.command(pass_context=True) async def face(self, ctx: commands.Context, theface): message = ctx.message - # Borrowing FlapJack's emoji validation (https://github.com/flapjax/FlapJack-Cogs/blob/master/smartreact/smartreact.py) + # Borrowing FlapJack's emoji validation + # (https://github.com/flapjax/FlapJack-Cogs/blob/master/smartreact/smartreact.py) if theface[:2] == "<:": theface = [r for r in self.bot.emojis if r.id == theface.split(':')[2][:-1]][0] @@ -202,7 +203,7 @@ class Hangman: """Returns the obscured answer""" out_str = "" - self.winbool = True + self.winbool[guild] = True for i in self.the_data[guild]["answer"]: if i == " " or i == "-": out_str += i * 2 @@ -273,7 +274,7 @@ class Hangman: await message.clear_reactions() for x in range(len(self.letters)): - if x in [i for i, b in enumerate("ABCDEFGHIJKLM") if b not in self._guesslist()]: + if x in [i for i, b in enumerate("ABCDEFGHIJKLM") if b not in self._guesslist(message.guild)]: await message.add_reaction(self.letters[x]) await message.add_reaction(self.navigate[-1]) @@ -282,19 +283,19 @@ class Hangman: await self.bot.clear_reactions(message) for x in range(len(self.letters)): - if x in [i for i, b in enumerate("NOPQRSTUVWXYZ") if b not in self._guesslist()]: + if x in [i for i, b in enumerate("NOPQRSTUVWXYZ") if b not in self._guesslist(message.guild)]: await message.add_reaction(self.letters[x + 13]) await message.add_reaction(self.navigate[0]) async def _printgame(self, channel): """Print the current state of game""" - cSay = ("Guess this: " + str(self._hideanswer(channel.guild)) + "\n" - + "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" - + self.hanglist[self.the_data[channel.guild]["hangman"]] + "\n" - + self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z") + c_say = ("Guess this: " + str(self._hideanswer(channel.guild)) + "\n" + + "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" + + self.hanglist[self.the_data[channel.guild]["hangman"]] + "\n" + + self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z") - message = await channel.send(cSay) + message = await channel.send(c_say) self.the_data[channel.guild]["trackmessage"] = message.id From 86985476e2d6c697fdc851907918b190d841e4d9 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:35:15 -0400 Subject: [PATCH 06/17] better navigate? --- hangman/hangman.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 3e25c22..8a6e0aa 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -8,6 +8,8 @@ from redbot.core import Config, checks class Hangman: """Lets anyone play a game of hangman with custom phrases""" + navigate = "🔼🔽" + letters = "🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿" def __init__(self, bot): self.bot = bot @@ -23,9 +25,6 @@ class Hangman: self.answer_path = "hangman/data/hanganswers.txt" self.winbool = defaultdict(lambda: False) - self.letters = "🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿" - self.navigate = "🔼🔽" - self.hanglist = {} async def _update_hanglist(self): @@ -290,10 +289,10 @@ class Hangman: async def _printgame(self, channel): """Print the current state of game""" - c_say = ("Guess this: " + str(self._hideanswer(channel.guild)) + "\n" - + "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" - + self.hanglist[self.the_data[channel.guild]["hangman"]] + "\n" - + self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z") + c_say =("Guess this: " + str(self._hideanswer(channel.guild)) + "\n" + + "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" + + self.hanglist[self.the_data[channel.guild]["hangman"]] + "\n" + + self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z") message = await channel.send(c_say) From 79b5b6155caaf4884434c291f87301a6d0a341f5 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:40:39 -0400 Subject: [PATCH 07/17] clearer errors --- hangman/hangman.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 8a6e0aa..c156136 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -289,10 +289,16 @@ class Hangman: async def _printgame(self, channel): """Print the current state of game""" - c_say =("Guess this: " + str(self._hideanswer(channel.guild)) + "\n" - + "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" - + self.hanglist[self.the_data[channel.guild]["hangman"]] + "\n" - + self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z") + if channel.guild not in self.hanglist: + await self._update_hanglist() + + c_say ="Guess this: " + str(self._hideanswer(channel.guild)) + "\n" + + c_say += "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" + + c_say += self.hanglist[channel.guild[self.the_data[channel.guild]["hangman"]]] + "\n" + + c_say += self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z" message = await channel.send(c_say) From a9609516edd9f4843e65a3be53e6e4bd2ca98fbe Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:41:25 -0400 Subject: [PATCH 08/17] await co --- hangman/hangman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index c156136..e159f2e 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -151,7 +151,7 @@ class Hangman: if guess is None: if self.the_data[ctx.guild]["running"]: await ctx.send("Game of hangman is already running!\nEnter your guess!") - self._printgame(ctx.channel) + await self._printgame(ctx.channel) """await self.bot.send_cmd_help(ctx)""" else: await ctx.send("Starting a game of hangman!") From 03fcab9817884abda6acddce56cffabb6397926c Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:42:09 -0400 Subject: [PATCH 09/17] even better use of brackets --- hangman/hangman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index e159f2e..502dfb5 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -296,7 +296,7 @@ class Hangman: c_say += "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" - c_say += self.hanglist[channel.guild[self.the_data[channel.guild]["hangman"]]] + "\n" + c_say += self.hanglist[channel.guild][self.the_data[channel.guild]["hangman"]] + "\n" c_say += self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z" From 77ca0e2891e6ba450061f64c1cd4c38bdaf2175f Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:44:29 -0400 Subject: [PATCH 10/17] v3 --- hangman/hangman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 502dfb5..563e7ee 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -279,7 +279,7 @@ class Hangman: await message.add_reaction(self.navigate[-1]) async def _reactmessage_nz(self, message): - await self.bot.clear_reactions(message) + await message.clear_reactions() for x in range(len(self.letters)): if x in [i for i, b in enumerate("NOPQRSTUVWXYZ") if b not in self._guesslist(message.guild)]: From 175586dfacf5116a79121815d45517756d8a15f9 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 16:53:57 -0400 Subject: [PATCH 11/17] make_say, reprint game --- hangman/hangman.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 563e7ee..0e1da72 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -223,8 +223,9 @@ class Hangman: return out_str - async def _guessletter(self, guess, channel): + async def _guessletter(self, guess, message): """Checks the guess on a letter and prints game if acceptable guess""" + channel = message.channel if guess.upper() not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or len(guess) != 1: await channel.send("Invalid guess. Only A-Z is accepted") return @@ -237,7 +238,7 @@ class Hangman: self.the_data[channel.guild]["guesses"].append(guess.upper()) - await self._printgame(channel) + await self._reprintgame(message) async def _on_react(self, reaction, user): """ Thanks to flapjack reactpoll for guidelines @@ -254,6 +255,8 @@ class Hangman: if str(emoji) in self.letters: letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[self.letters.index(str(emoji))] await self._guessletter(letter, message.channel) + await message.remove_reaction(emoji, user) + await message.remove_reaction(emoji, self.bot.user) if str(emoji) in self.navigate: if str(emoji) == self.navigate[0]: @@ -287,18 +290,34 @@ class Hangman: await message.add_reaction(self.navigate[0]) - async def _printgame(self, channel): - """Print the current state of game""" - if channel.guild not in self.hanglist: + def _make_say(self, guild): + c_say = "Guess this: " + str(self._hideanswer(guild)) + "\n" + + c_say += "Used Letters: " + str(self._guesslist(guild)) + "\n" + + c_say += self.hanglist[guild][self.the_data[guild]["hangman"]] + "\n" + + c_say += self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z" + + return c_say + + async def _reprintgame(self, message): + if message.guild not in self.hanglist: await self._update_hanglist() - c_say ="Guess this: " + str(self._hideanswer(channel.guild)) + "\n" + c_say = self._make_say(message.guild) - c_say += "Used Letters: " + str(self._guesslist(channel.guild)) + "\n" + await message.edit(c_say) + self.the_data[message.guild]["trackmessage"] = message.id - c_say += self.hanglist[channel.guild][self.the_data[channel.guild]["hangman"]] + "\n" + await self._checkdone(message.channel) - c_say += self.navigate[0] + " for A-M, " + self.navigate[-1] + " for N-Z" + async def _printgame(self, channel): + """Print the current state of game""" + if channel.guild not in self.hanglist: + await self._update_hanglist() + + c_say = self._make_say(channel.guild) message = await channel.send(c_say) From 8db4d7f6639b3c31822ec41e2398d4ac008fdb8a Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 17:05:17 -0400 Subject: [PATCH 12/17] correct guesses --- hangman/hangman.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 0e1da72..bea9ded 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -162,7 +162,8 @@ class Hangman: self._startgame(ctx.guild) await self._printgame(ctx.channel) else: - await self._guessletter(guess, ctx.channel) + await ctx.send("Guess by reacting to the message") + # await self._guessletter(guess, ctx.channel) def _startgame(self, guild): """Starts a new game of hangman""" @@ -254,7 +255,7 @@ class Hangman: if str(emoji) in self.letters: letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[self.letters.index(str(emoji))] - await self._guessletter(letter, message.channel) + await self._guessletter(letter, message) await message.remove_reaction(emoji, user) await message.remove_reaction(emoji, self.bot.user) From 9bb1d9ca5a69dcaceed49f33baada34153630c7f Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 4 May 2018 13:12:53 -0400 Subject: [PATCH 13/17] path? --- hangman/hangman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index bea9ded..00424e0 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -22,7 +22,7 @@ class Hangman: self.the_data = defaultdict( lambda: {"running": False, "hangman": 0, "guesses": [], "trackmessage": False, "answer": ''}) - self.answer_path = "hangman/data/hanganswers.txt" + self.answer_path = ".data/hanganswers.txt" self.winbool = defaultdict(lambda: False) self.hanglist = {} From ef81693050111c535e260ead5d46c35974f0eba8 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 4 May 2018 13:18:08 -0400 Subject: [PATCH 14/17] ? --- hangman/hangman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 00424e0..d502296 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -22,7 +22,7 @@ class Hangman: self.the_data = defaultdict( lambda: {"running": False, "hangman": 0, "guesses": [], "trackmessage": False, "answer": ''}) - self.answer_path = ".data/hanganswers.txt" + self.answer_path = "./data/hanganswers.txt" self.winbool = defaultdict(lambda: False) self.hanglist = {} From 81bba03affac1c9fcdbb47768c61d575f0697ce0 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 4 May 2018 15:23:27 -0400 Subject: [PATCH 15/17] closer than ever --- hangman/__init__.py | 2 ++ hangman/hangman.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/hangman/__init__.py b/hangman/__init__.py index 8b6ec76..2168fdf 100644 --- a/hangman/__init__.py +++ b/hangman/__init__.py @@ -1,7 +1,9 @@ from .hangman import Hangman +from redbot.core import data_manager def setup(bot): n = Hangman(bot) + data_manager.load_bundled_data(n, __file__) bot.add_cog(n) bot.add_listener(n._on_react, "on_reaction_add") diff --git a/hangman/hangman.py b/hangman/hangman.py index d502296..faa90bf 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -4,6 +4,7 @@ from random import randint import discord from discord.ext import commands from redbot.core import Config, checks +from redbot.core.data_manager import cog_data_path, load_basic_configuration class Hangman: @@ -13,6 +14,7 @@ class Hangman: def __init__(self, bot): self.bot = bot + load_basic_configuration("hangman") self.config = Config.get_conf(self, identifier=1049711010310997110) default_guild = { "theface": ':thinking:', @@ -22,7 +24,7 @@ class Hangman: self.the_data = defaultdict( lambda: {"running": False, "hangman": 0, "guesses": [], "trackmessage": False, "answer": ''}) - self.answer_path = "./data/hanganswers.txt" + self.answer_path = "/hanganswers.txt" self.winbool = defaultdict(lambda: False) self.hanglist = {} @@ -191,8 +193,9 @@ class Hangman: def _getphrase(self): """Get a new phrase for the game and returns it""" - phrasefile = open(self.answer_path, 'r') - phrases = phrasefile.readlines() + + with cog_data_path("hangman").open('r') as phrasefile: + phrases = phrasefile.readlines() outphrase = "" while outphrase == "": From 03d74f24fc8debf93f6d10b964030adaf4eb40da Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 4 May 2018 17:01:56 -0400 Subject: [PATCH 16/17] more attempts --- hangman/hangman.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index faa90bf..60e4589 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -193,8 +193,11 @@ class Hangman: def _getphrase(self): """Get a new phrase for the game and returns it""" + openpath = cog_data_path("hangman") - with cog_data_path("hangman").open('r') as phrasefile: + openpath = openpath.joinpath("data") + + with openpath.open('r') as phrasefile: phrases = phrasefile.readlines() outphrase = "" From 0291bd9904c4eae3cc324fe9d81359ac95342197 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Mon, 7 May 2018 13:18:12 -0400 Subject: [PATCH 17/17] almost there --- hangman/hangman.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/hangman/hangman.py b/hangman/hangman.py index 60e4589..766177f 100644 --- a/hangman/hangman.py +++ b/hangman/hangman.py @@ -14,7 +14,6 @@ class Hangman: def __init__(self, bot): self.bot = bot - load_basic_configuration("hangman") self.config = Config.get_conf(self, identifier=1049711010310997110) default_guild = { "theface": ':thinking:', @@ -24,7 +23,10 @@ class Hangman: self.the_data = defaultdict( lambda: {"running": False, "hangman": 0, "guesses": [], "trackmessage": False, "answer": ''}) - self.answer_path = "/hanganswers.txt" + self.path = str(cog_data_path(self)).replace('\\', '/') + + self.answer_path = self.path+"/bundled_data/hanganswers.txt" + self.winbool = defaultdict(lambda: False) self.hanglist = {} @@ -179,25 +181,21 @@ class Hangman: def _stopgame(self, guild): """Stops the game in current state""" self.the_data[guild]["running"] = False + self.the_data[guild]["trackmessage"] = False async def _checkdone(self, channel): if self.winbool[channel.guild]: await channel.send("You Win!") self._stopgame(channel.guild) - return - - if self.the_data[channel.guild]["hangman"] >= 7: + elif self.the_data[channel.guild]["hangman"] >= 7: await channel.send("You Lose!\nThe Answer was: **" + self.the_data[channel.guild]["answer"] + "**") self._stopgame(channel.guild) def _getphrase(self): """Get a new phrase for the game and returns it""" - openpath = cog_data_path("hangman") - - openpath = openpath.joinpath("data") - with openpath.open('r') as phrasefile: + with open(self.answer_path, 'r') as phrasefile: phrases = phrasefile.readlines() outphrase = "" @@ -314,7 +312,7 @@ class Hangman: c_say = self._make_say(message.guild) - await message.edit(c_say) + await message.edit(content=c_say) self.the_data[message.guild]["trackmessage"] = message.id await self._checkdone(message.channel)