From 2ee473894e83f3d1d4290163d4bd1e9a659abecf Mon Sep 17 00:00:00 2001 From: mathsman5133 <38833292+mathsman5133@users.noreply.github.com> Date: Tue, 9 Oct 2018 17:48:02 +1100 Subject: Add files via upload --- bot/cogs/candy_collection.py | 186 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 bot/cogs/candy_collection.py (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py new file mode 100644 index 00000000..5a51d05c --- /dev/null +++ b/bot/cogs/candy_collection.py @@ -0,0 +1,186 @@ +import discord +from discord.ext import commands +import random +import json +import functools +import os + +json_location = os.path.join(os.getcwd(),'resources', 'candy_collection.json') + + +class CandyCollection: + def __init__(self, bot): + self.bot = bot + with open(json_location) as candy: + self.candy_json = json.load(candy) + self.msg_reacted = self.candy_json['msg_reacted'] + self.get_candyinfo = dict() + for userinfo in self.candy_json['records']: + userid = userinfo['userid'] + self.get_candyinfo[userid] = userinfo + + HACKTOBER_CHANNEL_ID = 496432022961520650 + # chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy) + ADD_CANDY_REACTION_CHANCE = 20 # 5% + ADD_CANDY_EXISTING_REACTION_CHANCE = 10 # 10% + ADD_SKULL_REACTION_CHANCE = 50 # 2% + ADD_SKULL_EXISTING_REACTION_CHANCE = 20 # 5% + + async def on_message(self, message): + """Make sure the user is not a bot and + the channel is #event-hacktoberfest. + Because the skull has a lower chance of occurring + we'll check for that first, and then add respective reactions""" + if message.author.bot: + return + if message.channel.id != self.HACKTOBER_CHANNEL_ID: + return + if random.randint(1, self.ADD_SKULL_REACTION_CHANCE) == 1: + d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} + self.msg_reacted.append(d) + return await message.add_reaction('\N{SKULL}') + if random.randint(1, self.ADD_CANDY_REACTION_CHANCE) == 1: + d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} + self.msg_reacted.append(d) + return await message.add_reaction('\N{CANDY}') + + async def on_reaction_add(self, reaction, user): + """Make sure the reaction is in #event-hacktoberfest + and the user reacting is not a bot (ie. ourselves) + Check if the reaction is a skull/candy first. """ + message = reaction.message + if message.channel.id != self.HACKTOBER_CHANNEL_ID: + return + if user.bot: + return + if str(reaction.emoji) not in ('\N{SKULL}', '\N{CANDY}'): + if message.id in await self.ten_recent_msg(): + await self.reacted_msg_chance(message) + return + for react in self.msg_reacted: + # check to see if the message id of a message we added a + # reaction to is in json file, and if nobody has won/claimed it yet + if react['msg_id'] == message.id and react['won'] is False: + react['user_reacted'] = user.id + react['won'] = True + try: + # if they have record/candies in json already it will do this + user_records = self.get_candyinfo[user.id] + if str(reaction.emoji) == '\N{CANDY}': + user_records['record'] += 1 + if str(reaction.emoji) == '\N{SKULL}': + if user_records['record'] <= 3: + user_records['record'] = 0 + lost = 'all of your' + else: + lost = random.randint(1, 3) + user_records['record'] -= lost + await self.send_spook_msg(message.channel, lost) + + except KeyError: + # otherwise it will raise KeyError so we need to add them + if str(reaction.emoji) == '\N{CANDY}': + print('ok') + d = {"userid": user.id, "record": 1} + self.candy_json['records'].append(d) + await self.remove_reactions(reaction) + + async def reacted_msg_chance(self, message): + """(Randomly) add a skull or candy to a message if there is a reaction there already + (higher probability)""" + if random.randint(1, self.ADD_SKULL_EXISTING_REACTION_CHANCE) == 1: + d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} + self.msg_reacted.append(d) + return await message.add_reaction('\N{SKULL}') + + if random.randint(1, self.ADD_CANDY_EXISTING_REACTION_CHANCE) == 1: + d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} + self.msg_reacted.append(d) + return await message.add_reaction('\N{CANDY}') + + async def ten_recent_msg(self): + """Get the last 10 messages sent in the channel""" + ten_recent = [] + recent_msg = max((x for x in self.bot._connection._messages + if x.channel.id == self.HACKTOBER_CHANNEL_ID), key=lambda x: x.id) + channel = await self.hacktober_channel() + ten_recent.append(recent_msg.id) + for i in range(9): + o = discord.Object(id=recent_msg.id + i) + msg = await channel.history(limit=1, before=o).next() + ten_recent.append(msg.id) + return ten_recent + + async def get_message(self, msg_id): + """Get the message from it's ID. Use history rather than get_message due to + poor ratelimit (50/1s vs 1/1s)""" + try: + o = discord.Object(id=msg_id + 1) + msg = await self.hacktober_channel.history(limit=1, before=o).next() + if msg.id != msg_id: + return None + return msg + except Exception: + return None + + async def hacktober_channel(self): + """Get #events-hacktober channel from it's id""" + return self.bot.get_channel(id=self.HACKTOBER_CHANNEL_ID) + + async def remove_reactions(self, reaction): + """Remove all candy/skull reactions""" + try: + async for user in reaction.users(): + await reaction.message.remove_reaction(reaction.emoji, user) + except discord.HTTPException: + pass + + async def send_spook_msg(self, channel, candies): + """Send a (lame) spooky message""" + e = discord.Embed(colour=discord.Member.colour) + e.set_author(name="You went to bed last night and the witch visited your grave." + " She left a message that you will be cursed for as long as you live." + f" You woke up this morning and found that {candies} candies had disappeared") + await channel.send(embed=e) + + def save_to_json(self): + """Save json to the file. We will do this with bad practice + async (run_in_executor) to prevent blocking""" + with open(json_location, 'w') as outfile: + json.dump(self.candy_json, outfile) + + @commands.command() + async def candy(self, ctx): + """Get the candy leaderboard and save to json when this is called""" + thing = functools.partial(self.save_to_json) + save = await self.bot.loop.run_in_executor(None, thing) + emoji = ( + '\N{FIRST PLACE MEDAL}', + '\N{SECOND PLACE MEDAL}', + '\N{THIRD PLACE MEDAL}', + '\N{SPORTS MEDAL}', + '\N{SPORTS MEDAL}' + ) + top_sorted = sorted(self.candy_json['records'], key=lambda k: k.get('record', 0), reverse=True) + top_five = top_sorted[:5] + usersid = [] + records = [] + for record in top_five: + usersid.append(record['userid']) + records.append(record['record']) + + value = '\n'.join(f'{emoji[index]} <@{usersid[index]}>: {records[index]}' + for index in range(0, len(usersid))) or 'No Candies' + e = discord.Embed(colour=discord.Colour.blurple()) + e.add_field(name="Top Candy Records", value=value, inline=False) + e.add_field(name='\u200b', + value=f"Candies will randomly appear on messages sent. " + f"\nHit the candy when it appears as fast as possible to get the candy! " + f"\nBut beware the witch...", + inline=False) + await ctx.send(embed=e) + + +def setup(bot): + bot.add_cog(CandyCollection(bot)) + -- cgit v1.2.3 From 9314d2377d5d686fac729a3f05ea8dde3b8242aa Mon Sep 17 00:00:00 2001 From: mathsman5133 <38833292+mathsman5133@users.noreply.github.com> Date: Tue, 9 Oct 2018 17:49:32 +1100 Subject: Delete candy_collection.py --- bot/cogs/candy_collection.py | 186 ------------------------------------------- 1 file changed, 186 deletions(-) delete mode 100644 bot/cogs/candy_collection.py (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py deleted file mode 100644 index 5a51d05c..00000000 --- a/bot/cogs/candy_collection.py +++ /dev/null @@ -1,186 +0,0 @@ -import discord -from discord.ext import commands -import random -import json -import functools -import os - -json_location = os.path.join(os.getcwd(),'resources', 'candy_collection.json') - - -class CandyCollection: - def __init__(self, bot): - self.bot = bot - with open(json_location) as candy: - self.candy_json = json.load(candy) - self.msg_reacted = self.candy_json['msg_reacted'] - self.get_candyinfo = dict() - for userinfo in self.candy_json['records']: - userid = userinfo['userid'] - self.get_candyinfo[userid] = userinfo - - HACKTOBER_CHANNEL_ID = 496432022961520650 - # chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy) - ADD_CANDY_REACTION_CHANCE = 20 # 5% - ADD_CANDY_EXISTING_REACTION_CHANCE = 10 # 10% - ADD_SKULL_REACTION_CHANCE = 50 # 2% - ADD_SKULL_EXISTING_REACTION_CHANCE = 20 # 5% - - async def on_message(self, message): - """Make sure the user is not a bot and - the channel is #event-hacktoberfest. - Because the skull has a lower chance of occurring - we'll check for that first, and then add respective reactions""" - if message.author.bot: - return - if message.channel.id != self.HACKTOBER_CHANNEL_ID: - return - if random.randint(1, self.ADD_SKULL_REACTION_CHANCE) == 1: - d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} - self.msg_reacted.append(d) - return await message.add_reaction('\N{SKULL}') - if random.randint(1, self.ADD_CANDY_REACTION_CHANCE) == 1: - d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} - self.msg_reacted.append(d) - return await message.add_reaction('\N{CANDY}') - - async def on_reaction_add(self, reaction, user): - """Make sure the reaction is in #event-hacktoberfest - and the user reacting is not a bot (ie. ourselves) - Check if the reaction is a skull/candy first. """ - message = reaction.message - if message.channel.id != self.HACKTOBER_CHANNEL_ID: - return - if user.bot: - return - if str(reaction.emoji) not in ('\N{SKULL}', '\N{CANDY}'): - if message.id in await self.ten_recent_msg(): - await self.reacted_msg_chance(message) - return - for react in self.msg_reacted: - # check to see if the message id of a message we added a - # reaction to is in json file, and if nobody has won/claimed it yet - if react['msg_id'] == message.id and react['won'] is False: - react['user_reacted'] = user.id - react['won'] = True - try: - # if they have record/candies in json already it will do this - user_records = self.get_candyinfo[user.id] - if str(reaction.emoji) == '\N{CANDY}': - user_records['record'] += 1 - if str(reaction.emoji) == '\N{SKULL}': - if user_records['record'] <= 3: - user_records['record'] = 0 - lost = 'all of your' - else: - lost = random.randint(1, 3) - user_records['record'] -= lost - await self.send_spook_msg(message.channel, lost) - - except KeyError: - # otherwise it will raise KeyError so we need to add them - if str(reaction.emoji) == '\N{CANDY}': - print('ok') - d = {"userid": user.id, "record": 1} - self.candy_json['records'].append(d) - await self.remove_reactions(reaction) - - async def reacted_msg_chance(self, message): - """(Randomly) add a skull or candy to a message if there is a reaction there already - (higher probability)""" - if random.randint(1, self.ADD_SKULL_EXISTING_REACTION_CHANCE) == 1: - d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} - self.msg_reacted.append(d) - return await message.add_reaction('\N{SKULL}') - - if random.randint(1, self.ADD_CANDY_EXISTING_REACTION_CHANCE) == 1: - d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} - self.msg_reacted.append(d) - return await message.add_reaction('\N{CANDY}') - - async def ten_recent_msg(self): - """Get the last 10 messages sent in the channel""" - ten_recent = [] - recent_msg = max((x for x in self.bot._connection._messages - if x.channel.id == self.HACKTOBER_CHANNEL_ID), key=lambda x: x.id) - channel = await self.hacktober_channel() - ten_recent.append(recent_msg.id) - for i in range(9): - o = discord.Object(id=recent_msg.id + i) - msg = await channel.history(limit=1, before=o).next() - ten_recent.append(msg.id) - return ten_recent - - async def get_message(self, msg_id): - """Get the message from it's ID. Use history rather than get_message due to - poor ratelimit (50/1s vs 1/1s)""" - try: - o = discord.Object(id=msg_id + 1) - msg = await self.hacktober_channel.history(limit=1, before=o).next() - if msg.id != msg_id: - return None - return msg - except Exception: - return None - - async def hacktober_channel(self): - """Get #events-hacktober channel from it's id""" - return self.bot.get_channel(id=self.HACKTOBER_CHANNEL_ID) - - async def remove_reactions(self, reaction): - """Remove all candy/skull reactions""" - try: - async for user in reaction.users(): - await reaction.message.remove_reaction(reaction.emoji, user) - except discord.HTTPException: - pass - - async def send_spook_msg(self, channel, candies): - """Send a (lame) spooky message""" - e = discord.Embed(colour=discord.Member.colour) - e.set_author(name="You went to bed last night and the witch visited your grave." - " She left a message that you will be cursed for as long as you live." - f" You woke up this morning and found that {candies} candies had disappeared") - await channel.send(embed=e) - - def save_to_json(self): - """Save json to the file. We will do this with bad practice - async (run_in_executor) to prevent blocking""" - with open(json_location, 'w') as outfile: - json.dump(self.candy_json, outfile) - - @commands.command() - async def candy(self, ctx): - """Get the candy leaderboard and save to json when this is called""" - thing = functools.partial(self.save_to_json) - save = await self.bot.loop.run_in_executor(None, thing) - emoji = ( - '\N{FIRST PLACE MEDAL}', - '\N{SECOND PLACE MEDAL}', - '\N{THIRD PLACE MEDAL}', - '\N{SPORTS MEDAL}', - '\N{SPORTS MEDAL}' - ) - top_sorted = sorted(self.candy_json['records'], key=lambda k: k.get('record', 0), reverse=True) - top_five = top_sorted[:5] - usersid = [] - records = [] - for record in top_five: - usersid.append(record['userid']) - records.append(record['record']) - - value = '\n'.join(f'{emoji[index]} <@{usersid[index]}>: {records[index]}' - for index in range(0, len(usersid))) or 'No Candies' - e = discord.Embed(colour=discord.Colour.blurple()) - e.add_field(name="Top Candy Records", value=value, inline=False) - e.add_field(name='\u200b', - value=f"Candies will randomly appear on messages sent. " - f"\nHit the candy when it appears as fast as possible to get the candy! " - f"\nBut beware the witch...", - inline=False) - await ctx.send(embed=e) - - -def setup(bot): - bot.add_cog(CandyCollection(bot)) - -- cgit v1.2.3 From 3e547aa2ff677bfe2b32af6ec41c9cefc7b6d3ca Mon Sep 17 00:00:00 2001 From: mathsman5133 Date: Tue, 9 Oct 2018 17:59:20 +1100 Subject: candy-collection --- bot/cogs/candy_collection.py | 186 +++++++++++++++++++++++++++++++ bot/cogs/resources/candy_collection.json | 25 +++++ 2 files changed, 211 insertions(+) create mode 100644 bot/cogs/candy_collection.py create mode 100644 bot/cogs/resources/candy_collection.json (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py new file mode 100644 index 00000000..41e9472f --- /dev/null +++ b/bot/cogs/candy_collection.py @@ -0,0 +1,186 @@ +import discord +from discord.ext import commands +import random +import json +import functools +import os + +json_location = os.path.join(os.getcwd(),'resources', 'candy_collection.json') + + +class CandyCollection: + def __init__(self, bot): + self.bot = bot + with open(json_location) as candy: + self.candy_json = json.load(candy) + self.msg_reacted = self.candy_json['msg_reacted'] + self.get_candyinfo = dict() + for userinfo in self.candy_json['records']: + userid = userinfo['userid'] + self.get_candyinfo[userid] = userinfo + + HACKTOBER_CHANNEL_ID = 496432022961520650 + # chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy) + ADD_CANDY_REACTION_CHANCE = 20 # 5% + ADD_CANDY_EXISTING_REACTION_CHANCE = 10 # 10% + ADD_SKULL_REACTION_CHANCE = 50 # 2% + ADD_SKULL_EXISTING_REACTION_CHANCE = 20 # 5% + + async def on_message(self, message): + """Make sure the user is not a bot and + the channel is #event-hacktoberfest. + Because the skull has a lower chance of occurring + we'll check for that first, and then add respective reactions""" + if message.author.bot: + return + if message.channel.id != self.HACKTOBER_CHANNEL_ID: + return + if random.randint(1, self.ADD_SKULL_REACTION_CHANCE) == 1: + d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} + self.msg_reacted.append(d) + return await message.add_reaction('\N{SKULL}') + if random.randint(1, self.ADD_CANDY_REACTION_CHANCE) == 1: + d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} + self.msg_reacted.append(d) + return await message.add_reaction('\N{CANDY}') + + async def on_reaction_add(self, reaction, user): + """Make sure the reaction is in #event-hacktoberfest + and the user reacting is not a bot (ie. ourselves) + Check if the reaction is a skull/candy first. """ + message = reaction.message + if message.channel.id != self.HACKTOBER_CHANNEL_ID: + return + if user.bot: + return + if str(reaction.emoji) not in ('\N{SKULL}', '\N{CANDY}'): + if message.id in await self.ten_recent_msg(): + await self.reacted_msg_chance(message) + return + for react in self.msg_reacted: + # check to see if the message id of a message we added a + # reaction to is in json file, and if nobody has won/claimed it yet + if react['msg_id'] == message.id and react['won'] is False: + react['user_reacted'] = user.id + react['won'] = True + try: + # if they have record/candies in json already it will do this + user_records = self.get_candyinfo[user.id] + if str(reaction.emoji) == '\N{CANDY}': + user_records['record'] += 1 + if str(reaction.emoji) == '\N{SKULL}': + if user_records['record'] <= 3: + user_records['record'] = 0 + lost = 'all of your' + else: + lost = random.randint(1, 3) + user_records['record'] -= lost + await self.send_spook_msg(message.channel, lost) + + except KeyError: + # otherwise it will raise KeyError so we need to add them + if str(reaction.emoji) == '\N{CANDY}': + print('ok') + d = {"userid": user.id, "record": 1} + self.candy_json['records'].append(d) + await self.remove_reactions(reaction) + + async def reacted_msg_chance(self, message): + """(Randomly) add a skull or candy to a message if there is a reaction there already + (higher probability)""" + if random.randint(1, self.ADD_SKULL_EXISTING_REACTION_CHANCE) == 1: + d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} + self.msg_reacted.append(d) + return await message.add_reaction('\N{SKULL}') + + if random.randint(1, self.ADD_CANDY_EXISTING_REACTION_CHANCE) == 1: + d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} + self.msg_reacted.append(d) + return await message.add_reaction('\N{CANDY}') + + async def ten_recent_msg(self): + """Get the last 10 messages sent in the channel""" + ten_recent = [] + recent_msg = max((x for x in self.bot._connection._messages + if x.channel.id == self.HACKTOBER_CHANNEL_ID), key=lambda x: x.id) + channel = await self.hacktober_channel() + ten_recent.append(recent_msg.id) + for i in range(9): + o = discord.Object(id=recent_msg.id + i) + msg = await channel.history(limit=1, before=o).next() + ten_recent.append(msg.id) + return ten_recent + + async def get_message(self, msg_id): + """Get the message from it's ID. Use history rather than get_message due to + poor ratelimit (50/1s vs 1/1s)""" + try: + o = discord.Object(id=msg_id + 1) + msg = await self.hacktober_channel.history(limit=1, before=o).next() + if msg.id != msg_id: + return None + return msg + except Exception: + return None + + async def hacktober_channel(self): + """Get #events-hacktober channel from it's id""" + return self.bot.get_channel(id=self.HACKTOBER_CHANNEL_ID) + + async def remove_reactions(self, reaction): + """Remove all candy/skull reactions""" + try: + async for user in reaction.users(): + await reaction.message.remove_reaction(reaction.emoji, user) + except discord.HTTPException: + pass + + async def send_spook_msg(self, channel, candies): + """Send a (lame) spooky message""" + e = discord.Embed(colour=discord.Member.colour) + e.set_author(name="You went to bed last night and the witch visited your grave." + " She left a message that you will be cursed for as long as you live." + f" You woke up this morning and found that {candies} candies had disappeared") + await channel.send(embed=e) + + def save_to_json(self): + """Save json to the file. We will do this with bad practice + async (run_in_executor) to prevent blocking""" + with open(json_location, 'w') as outfile: + json.dump(self.candy_json, outfile) + + @commands.command() + async def candy(self, ctx): + """Get the candy leaderboard and save to json when this is called""" + thing = functools.partial(self.save_to_json) + save = await self.bot.loop.run_in_executor(None, thing) + emoji = ( + '\N{FIRST PLACE MEDAL}', + '\N{SECOND PLACE MEDAL}', + '\N{THIRD PLACE MEDAL}', + '\N{SPORTS MEDAL}', + '\N{SPORTS MEDAL}' + ) + top_sorted = sorted(self.candy_json['records'], key=lambda k: k.get('record', 0), reverse=True) + top_five = top_sorted[:5] + usersid = [] + records = [] + for record in top_five: + usersid.append(record['userid']) + records.append(record['record']) + + value = '\n'.join(f'{emoji[index]} <@{usersid[index]}>: {records[index]}' + for index in range(0, len(usersid))) or 'No Candies' + e = discord.Embed(colour=discord.Colour.blurple()) + e.add_field(name="Top Candy Records", value=value, inline=False) + e.add_field(name='\u200b', + value=f"Candies will randomly appear on messages sent. " + f"\nHit the candy when it appears as fast as possible to get the candy! " + f"\nBut beware the witch...", + inline=False) + await ctx.send(embed=e) + + +def setup(bot): + bot.add_cog(CandyCollection(bot)) + diff --git a/bot/cogs/resources/candy_collection.json b/bot/cogs/resources/candy_collection.json new file mode 100644 index 00000000..2b9a30a6 --- /dev/null +++ b/bot/cogs/resources/candy_collection.json @@ -0,0 +1,25 @@ +{ + "msg_reacted": [ + { + "reaction": "\ud83c\udf6c", + "msg_id": 499064137188638730, + "won": true, + "user_reacted": 230214242618441728 + }, + { + "reaction": "\ud83c\udf6c", + "msg_id": 499064204570132500, + "won": false + } + ], + "records": [ + { + "userid": 1234567890, + "record": 4 + }, + { + "userid": 9876543210, + "record": 10 + } + ] +} \ No newline at end of file -- cgit v1.2.3 From f1f35f0b520a9efe7c4b74e3683766b3e82ef783 Mon Sep 17 00:00:00 2001 From: mathsman5133 <38833292+mathsman5133@users.noreply.github.com> Date: Wed, 10 Oct 2018 11:04:18 +1100 Subject: Update candy_collection.py --- bot/cogs/candy_collection.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py index 41e9472f..208f6686 100644 --- a/bot/cogs/candy_collection.py +++ b/bot/cogs/candy_collection.py @@ -138,9 +138,8 @@ class CandyCollection: async def send_spook_msg(self, channel, candies): """Send a (lame) spooky message""" e = discord.Embed(colour=discord.Member.colour) - e.set_author(name="You went to bed last night and the witch visited your grave." - " She left a message that you will be cursed for as long as you live." - f" You woke up this morning and found that {candies} candies had disappeared") + e.set_author(name="Ghosts and Ghouls and Jack o' lanterns at night; " + f"I took {candies} candies and quickly took flight.") await channel.send(embed=e) def save_to_json(self): @@ -176,7 +175,7 @@ class CandyCollection: e.add_field(name='\u200b', value=f"Candies will randomly appear on messages sent. " f"\nHit the candy when it appears as fast as possible to get the candy! " - f"\nBut beware the witch...", + f"\nBut beware the ghosts...", inline=False) await ctx.send(embed=e) -- cgit v1.2.3 From 4c29c2b470d6f27416a97c3be1e41f753e0a1ea6 Mon Sep 17 00:00:00 2001 From: mathsman5133 <38833292+mathsman5133@users.noreply.github.com> Date: Thu, 11 Oct 2018 21:58:13 +1100 Subject: fixed colour for spooky msg --- bot/cogs/candy_collection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py index 208f6686..1d470524 100644 --- a/bot/cogs/candy_collection.py +++ b/bot/cogs/candy_collection.py @@ -75,7 +75,7 @@ class CandyCollection: else: lost = random.randint(1, 3) user_records['record'] -= lost - await self.send_spook_msg(message.channel, lost) + await self.send_spook_msg(message.author, message.channel, lost) except KeyError: # otherwise it will raise KeyError so we need to add them @@ -135,9 +135,9 @@ class CandyCollection: except discord.HTTPException: pass - async def send_spook_msg(self, channel, candies): + async def send_spook_msg(self, author, channel, candies): """Send a (lame) spooky message""" - e = discord.Embed(colour=discord.Member.colour) + e = discord.Embed(colour=author.colour) e.set_author(name="Ghosts and Ghouls and Jack o' lanterns at night; " f"I took {candies} candies and quickly took flight.") await channel.send(embed=e) -- cgit v1.2.3 From 7d80d96bca9a18d69f6c469a1531ddfe29417425 Mon Sep 17 00:00:00 2001 From: mathsman5133 Date: Fri, 12 Oct 2018 10:24:36 +1100 Subject: Fixed docstrings, add blank lines for readibility, method of finding last 10 messages --- bot/cogs/candy_collection.py | 78 +++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 27 deletions(-) (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py index 41e9472f..1e066cb0 100644 --- a/bot/cogs/candy_collection.py +++ b/bot/cogs/candy_collection.py @@ -5,7 +5,7 @@ import json import functools import os -json_location = os.path.join(os.getcwd(),'resources', 'candy_collection.json') +json_location = os.path.join(os.getcwd(), 'resources', 'candy_collection.json') class CandyCollection: @@ -19,7 +19,7 @@ class CandyCollection: userid = userinfo['userid'] self.get_candyinfo[userid] = userinfo - HACKTOBER_CHANNEL_ID = 496432022961520650 + HACKTOBER_CHANNEL_ID = 498804484324196362 # chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy) ADD_CANDY_REACTION_CHANCE = 20 # 5% ADD_CANDY_EXISTING_REACTION_CHANCE = 10 # 10% @@ -27,36 +27,44 @@ class CandyCollection: ADD_SKULL_EXISTING_REACTION_CHANCE = 20 # 5% async def on_message(self, message): - """Make sure the user is not a bot and - the channel is #event-hacktoberfest. - Because the skull has a lower chance of occurring - we'll check for that first, and then add respective reactions""" + """Randomly adds candy or skull to certain messages""" + + # make sure its a human message if message.author.bot: return + # ensure it's hacktober channel if message.channel.id != self.HACKTOBER_CHANNEL_ID: return + + # do random check for skull first as it has the lower chance if random.randint(1, self.ADD_SKULL_REACTION_CHANCE) == 1: d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} self.msg_reacted.append(d) return await message.add_reaction('\N{SKULL}') + # check for the candy chance next if random.randint(1, self.ADD_CANDY_REACTION_CHANCE) == 1: d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} self.msg_reacted.append(d) return await message.add_reaction('\N{CANDY}') async def on_reaction_add(self, reaction, user): - """Make sure the reaction is in #event-hacktoberfest - and the user reacting is not a bot (ie. ourselves) - Check if the reaction is a skull/candy first. """ + """Add/remove candies from a person if the reaction satisfies criteria""" + message = reaction.message - if message.channel.id != self.HACKTOBER_CHANNEL_ID: - return + # check to ensure the reactor is human if user.bot: return + # check to ensure it is in correct channel + if message.channel.id != self.HACKTOBER_CHANNEL_ID: + return + + # if its not a candy or skull, and it is one of 10 most recent messages, + # proceed to add a skull/candy with higher chance if str(reaction.emoji) not in ('\N{SKULL}', '\N{CANDY}'): if message.id in await self.ten_recent_msg(): await self.reacted_msg_chance(message) return + for react in self.msg_reacted: # check to see if the message id of a message we added a # reaction to is in json file, and if nobody has won/claimed it yet @@ -75,10 +83,10 @@ class CandyCollection: else: lost = random.randint(1, 3) user_records['record'] -= lost - await self.send_spook_msg(message.channel, lost) + await self.send_spook_msg(message.author, message.channel, lost) except KeyError: - # otherwise it will raise KeyError so we need to add them + # otherwise it will raise KeyError so we need to add them to file if str(reaction.emoji) == '\N{CANDY}': print('ok') d = {"userid": user.id, "record": 1} @@ -86,8 +94,9 @@ class CandyCollection: await self.remove_reactions(reaction) async def reacted_msg_chance(self, message): - """(Randomly) add a skull or candy to a message if there is a reaction there already + """Randomly add a skull or candy to a message if there is a reaction there already (higher probability)""" + if random.randint(1, self.ADD_SKULL_EXISTING_REACTION_CHANCE) == 1: d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} self.msg_reacted.append(d) @@ -101,59 +110,71 @@ class CandyCollection: async def ten_recent_msg(self): """Get the last 10 messages sent in the channel""" ten_recent = [] - recent_msg = max((x for x in self.bot._connection._messages - if x.channel.id == self.HACKTOBER_CHANNEL_ID), key=lambda x: x.id) + recent_msg = max(message.id for message + in self.bot._connection._messages + if message.channel.id == self.HACKTOBER_CHANNEL_ID) + channel = await self.hacktober_channel() ten_recent.append(recent_msg.id) + for i in range(9): o = discord.Object(id=recent_msg.id + i) msg = await channel.history(limit=1, before=o).next() ten_recent.append(msg.id) + return ten_recent async def get_message(self, msg_id): - """Get the message from it's ID. Use history rather than get_message due to - poor ratelimit (50/1s vs 1/1s)""" + """Get the message from it's ID.""" + try: o = discord.Object(id=msg_id + 1) + # Use history rather than get_message due to + # poor ratelimit (50/1s vs 1/1s) msg = await self.hacktober_channel.history(limit=1, before=o).next() + if msg.id != msg_id: return None + return msg + except Exception: return None async def hacktober_channel(self): - """Get #events-hacktober channel from it's id""" + """Get #hacktoberbot channel from it's id""" return self.bot.get_channel(id=self.HACKTOBER_CHANNEL_ID) async def remove_reactions(self, reaction): """Remove all candy/skull reactions""" + try: async for user in reaction.users(): await reaction.message.remove_reaction(reaction.emoji, user) + except discord.HTTPException: pass - async def send_spook_msg(self, channel, candies): - """Send a (lame) spooky message""" - e = discord.Embed(colour=discord.Member.colour) - e.set_author(name="You went to bed last night and the witch visited your grave." - " She left a message that you will be cursed for as long as you live." - f" You woke up this morning and found that {candies} candies had disappeared") + async def send_spook_msg(self, author, channel, candies): + """Send a spooky message""" + e = discord.Embed(colour=author.colour) + e.set_author(name="Ghosts and Ghouls and Jack o' lanterns at night; " + f"I took {candies} candies and quickly took flight.") await channel.send(embed=e) def save_to_json(self): - """Save json to the file. We will do this with bad practice - async (run_in_executor) to prevent blocking""" + """Save json to the file.""" with open(json_location, 'w') as outfile: json.dump(self.candy_json, outfile) @commands.command() async def candy(self, ctx): """Get the candy leaderboard and save to json when this is called""" + + # use run_in_executor to prevent blocking thing = functools.partial(self.save_to_json) save = await self.bot.loop.run_in_executor(None, thing) + emoji = ( '\N{FIRST PLACE MEDAL}', '\N{SECOND PLACE MEDAL}', @@ -161,8 +182,10 @@ class CandyCollection: '\N{SPORTS MEDAL}', '\N{SPORTS MEDAL}' ) + top_sorted = sorted(self.candy_json['records'], key=lambda k: k.get('record', 0), reverse=True) top_five = top_sorted[:5] + usersid = [] records = [] for record in top_five: @@ -171,6 +194,7 @@ class CandyCollection: value = '\n'.join(f'{emoji[index]} <@{usersid[index]}>: {records[index]}' for index in range(0, len(usersid))) or 'No Candies' + e = discord.Embed(colour=discord.Colour.blurple()) e.add_field(name="Top Candy Records", value=value, inline=False) e.add_field(name='\u200b', -- cgit v1.2.3 From 7960ac4059bad5b464137cbf690c0f802ee66857 Mon Sep 17 00:00:00 2001 From: mathsman5133 Date: Fri, 12 Oct 2018 10:47:18 +1100 Subject: Fixed merged conflicts --- bot/cogs/candy_collection.py | 4 ---- 1 file changed, 4 deletions(-) (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py index 63ce9cb6..8eb6ddf9 100644 --- a/bot/cogs/candy_collection.py +++ b/bot/cogs/candy_collection.py @@ -156,11 +156,7 @@ class CandyCollection: pass async def send_spook_msg(self, author, channel, candies): -<<<<<<< HEAD """Send a spooky message""" -======= - """Send a (lame) spooky message""" ->>>>>>> 4c29c2b470d6f27416a97c3be1e41f753e0a1ea6 e = discord.Embed(colour=author.colour) e.set_author(name="Ghosts and Ghouls and Jack o' lanterns at night; " f"I took {candies} candies and quickly took flight.") -- cgit v1.2.3 From e75a59172674243986bdf73aecb1d61f2e1b3cee Mon Sep 17 00:00:00 2001 From: mathsman5133 Date: Sat, 13 Oct 2018 11:31:08 +1100 Subject: Add new line and remove save variable (not used) --- bot/cogs/candy_collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py index 8eb6ddf9..713e1f2f 100644 --- a/bot/cogs/candy_collection.py +++ b/bot/cogs/candy_collection.py @@ -173,7 +173,7 @@ class CandyCollection: # use run_in_executor to prevent blocking thing = functools.partial(self.save_to_json) - save = await self.bot.loop.run_in_executor(None, thing) + await self.bot.loop.run_in_executor(None, thing) emoji = ( '\N{FIRST PLACE MEDAL}', -- cgit v1.2.3 From 6b5ad9c4fc9f431336569ffe7359bc60becb66a1 Mon Sep 17 00:00:00 2001 From: mathsman5133 Date: Sat, 13 Oct 2018 11:33:51 +1100 Subject: Remove new line at end of file --- bot/cogs/candy_collection.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py index 713e1f2f..b1d47cdb 100644 --- a/bot/cogs/candy_collection.py +++ b/bot/cogs/candy_collection.py @@ -206,5 +206,4 @@ class CandyCollection: def setup(bot): - bot.add_cog(CandyCollection(bot)) - + bot.add_cog(CandyCollection(bot)) \ No newline at end of file -- cgit v1.2.3 From f03debf36a3b0c1155474b6f7b7d5d95478d4170 Mon Sep 17 00:00:00 2001 From: mathsman5133 Date: Sat, 13 Oct 2018 11:37:11 +1100 Subject: Add that line back lol --- bot/cogs/candy_collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py index b1d47cdb..b8a36115 100644 --- a/bot/cogs/candy_collection.py +++ b/bot/cogs/candy_collection.py @@ -206,4 +206,4 @@ class CandyCollection: def setup(bot): - bot.add_cog(CandyCollection(bot)) \ No newline at end of file + bot.add_cog(CandyCollection(bot)) -- cgit v1.2.3 From 6efbded6e068dff478ee121ede2ebe74a16292f5 Mon Sep 17 00:00:00 2001 From: mathsman5133 Date: Sun, 14 Oct 2018 20:06:36 +1100 Subject: fixed docstring and position of constants --- bot/cogs/candy_collection.py | 67 ++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 24 deletions(-) (limited to 'bot/cogs/candy_collection.py') diff --git a/bot/cogs/candy_collection.py b/bot/cogs/candy_collection.py index b8a36115..59eadd93 100644 --- a/bot/cogs/candy_collection.py +++ b/bot/cogs/candy_collection.py @@ -7,6 +7,14 @@ import os json_location = os.path.join(os.getcwd(), 'resources', 'candy_collection.json') +HACKTOBER_CHANNEL_ID = 498804484324196362 + +# chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy) +ADD_CANDY_REACTION_CHANCE = 20 # 5% +ADD_CANDY_EXISTING_REACTION_CHANCE = 10 # 10% +ADD_SKULL_REACTION_CHANCE = 50 # 2% +ADD_SKULL_EXISTING_REACTION_CHANCE = 20 # 5% + class CandyCollection: def __init__(self, bot): @@ -19,43 +27,40 @@ class CandyCollection: userid = userinfo['userid'] self.get_candyinfo[userid] = userinfo - HACKTOBER_CHANNEL_ID = 498804484324196362 - # chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy) - ADD_CANDY_REACTION_CHANCE = 20 # 5% - ADD_CANDY_EXISTING_REACTION_CHANCE = 10 # 10% - ADD_SKULL_REACTION_CHANCE = 50 # 2% - ADD_SKULL_EXISTING_REACTION_CHANCE = 20 # 5% - async def on_message(self, message): - """Randomly adds candy or skull to certain messages""" + """ + Randomly adds candy or skull to certain messages + """ # make sure its a human message if message.author.bot: return # ensure it's hacktober channel - if message.channel.id != self.HACKTOBER_CHANNEL_ID: + if message.channel.id != HACKTOBER_CHANNEL_ID: return # do random check for skull first as it has the lower chance - if random.randint(1, self.ADD_SKULL_REACTION_CHANCE) == 1: + if random.randint(1, ADD_SKULL_REACTION_CHANCE) == 1: d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} self.msg_reacted.append(d) return await message.add_reaction('\N{SKULL}') # check for the candy chance next - if random.randint(1, self.ADD_CANDY_REACTION_CHANCE) == 1: + if random.randint(1, ADD_CANDY_REACTION_CHANCE) == 1: d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} self.msg_reacted.append(d) return await message.add_reaction('\N{CANDY}') async def on_reaction_add(self, reaction, user): - """Add/remove candies from a person if the reaction satisfies criteria""" + """ + Add/remove candies from a person if the reaction satisfies criteria + """ message = reaction.message # check to ensure the reactor is human if user.bot: return # check to ensure it is in correct channel - if message.channel.id != self.HACKTOBER_CHANNEL_ID: + if message.channel.id != HACKTOBER_CHANNEL_ID: return # if its not a candy or skull, and it is one of 10 most recent messages, @@ -94,15 +99,17 @@ class CandyCollection: await self.remove_reactions(reaction) async def reacted_msg_chance(self, message): - """Randomly add a skull or candy to a message if there is a reaction there already - (higher probability)""" + """ + Randomly add a skull or candy to a message if there is a reaction there already + (higher probability) + """ - if random.randint(1, self.ADD_SKULL_EXISTING_REACTION_CHANCE) == 1: + if random.randint(1, ADD_SKULL_EXISTING_REACTION_CHANCE) == 1: d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False} self.msg_reacted.append(d) return await message.add_reaction('\N{SKULL}') - if random.randint(1, self.ADD_CANDY_EXISTING_REACTION_CHANCE) == 1: + if random.randint(1, ADD_CANDY_EXISTING_REACTION_CHANCE) == 1: d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False} self.msg_reacted.append(d) return await message.add_reaction('\N{CANDY}') @@ -125,7 +132,9 @@ class CandyCollection: return ten_recent async def get_message(self, msg_id): - """Get the message from it's ID.""" + """ + Get the message from it's ID. + """ try: o = discord.Object(id=msg_id + 1) @@ -142,11 +151,15 @@ class CandyCollection: return None async def hacktober_channel(self): - """Get #hacktoberbot channel from it's id""" - return self.bot.get_channel(id=self.HACKTOBER_CHANNEL_ID) + """ + Get #hacktoberbot channel from it's id + """ + return self.bot.get_channel(id=HACKTOBER_CHANNEL_ID) async def remove_reactions(self, reaction): - """Remove all candy/skull reactions""" + """ + Remove all candy/skull reactions + """ try: async for user in reaction.users(): @@ -156,20 +169,26 @@ class CandyCollection: pass async def send_spook_msg(self, author, channel, candies): - """Send a spooky message""" + """ + Send a spooky message + """ e = discord.Embed(colour=author.colour) e.set_author(name="Ghosts and Ghouls and Jack o' lanterns at night; " f"I took {candies} candies and quickly took flight.") await channel.send(embed=e) def save_to_json(self): - """Save json to the file.""" + """ + Save json to the file. + """ with open(json_location, 'w') as outfile: json.dump(self.candy_json, outfile) @commands.command() async def candy(self, ctx): - """Get the candy leaderboard and save to json when this is called""" + """ + Get the candy leaderboard and save to json when this is called + """ # use run_in_executor to prevent blocking thing = functools.partial(self.save_to_json) -- cgit v1.2.3