From 7756650dc337b913bba75cdc5b9f3dabc61b4c68 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Mon, 10 Aug 2020 13:03:13 -0700 Subject: Updated to comply with reviews. --- bot/exts/evergreen/conversationstarters.py | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 bot/exts/evergreen/conversationstarters.py (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py new file mode 100644 index 00000000..f4d1d3ce --- /dev/null +++ b/bot/exts/evergreen/conversationstarters.py @@ -0,0 +1,57 @@ +import json +import logging +import random +from pathlib import Path + +from discord import Embed +from discord.ext import commands + +from bot.utils.decorators import override_in_channel + + +with Path("bot/resources/easter/starter.json").open("r", encoding="utf8") as f: + STARTERS = json.load(f)["starters"] + + +with Path("bot/resources/easter/py_topics.json").open("r", encoding="utf8") as f: + # First ID is #python-general and the rest are top to bottom categories of Topical Chat/Help. + PY_TOPICS = json.load(f)["python-channels"] + ALL_PYTHON_CHANNELS = [int(channel_id) for channel_id in PY_TOPICS.keys()] + + +class ConvoStarters(commands.Cog): + """Easter conversation topics.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.command() + @override_in_channel(ALL_PYTHON_CHANNELS) + async def topic(self, ctx: commands.Context) -> None: + """Responds with a random topic to start a conversation, changing depending on channel.""" + try: + # Fetching topics. + channel_topics = PY_TOPICS[str(ctx.channel.id)] + + # If the channel isn't Python-related. + except KeyError: + await ctx.send(random.choice(starters['starters'])) + + # If the channel ID doesn't have any topics. + else: + if channel_topics: + await ctx.send(random.choice(channel_topics)) + + else: + embed = Embed( + description=( + "No topics found for this Python channel. You can suggest new ideas for topics " + "[here](https://github.com/python-discord/seasonalbot/issues/426)!" + )) + + await ctx.send(embed=embed) + + +def setup(bot: commands.Bot) -> None: + """Conversation starters Cog load.""" + bot.add_cog(ConvoStarters(bot)) -- cgit v1.2.3 From 0bcbe43557b9566f265d894b28c7f47a96765196 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Mon, 10 Aug 2020 13:08:38 -0700 Subject: Linted. --- bot/exts/evergreen/conversationstarters.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index f4d1d3ce..54ed70c1 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -1,5 +1,4 @@ import json -import logging import random from pathlib import Path @@ -35,13 +34,13 @@ class ConvoStarters(commands.Cog): # If the channel isn't Python-related. except KeyError: - await ctx.send(random.choice(starters['starters'])) + await ctx.send(random.choice(STARTERS)) # If the channel ID doesn't have any topics. else: if channel_topics: await ctx.send(random.choice(channel_topics)) - + else: embed = Embed( description=( -- cgit v1.2.3 From 3317f00e7dcd7fee1bda79fd5b82f2d9fbd2e1cc Mon Sep 17 00:00:00 2001 From: Xithrius Date: Mon, 10 Aug 2020 13:50:10 -0700 Subject: Finalized whitelist for allowed channels .topic can be used in. Changed resource path to evergreen, added WHITELISTED_CHANNELS to PY_TOPICS channels. --- bot/exts/evergreen/conversationstarters.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index 54ed70c1..4df3f068 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -6,28 +6,36 @@ from discord import Embed from discord.ext import commands from bot.utils.decorators import override_in_channel +from bot.constants import WHITELISTED_CHANNELS -with Path("bot/resources/easter/starter.json").open("r", encoding="utf8") as f: +with Path("bot/resources/evergreen/starter.json").open("r", encoding="utf8") as f: STARTERS = json.load(f)["starters"] -with Path("bot/resources/easter/py_topics.json").open("r", encoding="utf8") as f: +with Path("bot/resources/evergreen/py_topics.json").open("r", encoding="utf8") as f: # First ID is #python-general and the rest are top to bottom categories of Topical Chat/Help. PY_TOPICS = json.load(f)["python-channels"] - ALL_PYTHON_CHANNELS = [int(channel_id) for channel_id in PY_TOPICS.keys()] + + # All the allowed channels that the ".topic" command is allowed to be executed in. + ALL_ALLOWED_CHANNELS = [int(channel_id) for channel_id in PY_TOPICS.keys()].extend(WHITELISTED_CHANNELS) class ConvoStarters(commands.Cog): - """Easter conversation topics.""" + """Evergreen conversation topics.""" def __init__(self, bot: commands.Bot): self.bot = bot @commands.command() - @override_in_channel(ALL_PYTHON_CHANNELS) + @override_in_channel(ALL_ALLOWED_CHANNELS) async def topic(self, ctx: commands.Context) -> None: - """Responds with a random topic to start a conversation, changing depending on channel.""" + """Responds with a random topic to start a conversation + + If in a Python channel, a python-related topic will be given. + + Otherwise, a random conversation topic will be recieved by the user. + """ try: # Fetching topics. channel_topics = PY_TOPICS[str(ctx.channel.id)] -- cgit v1.2.3 From 77cafe8a048bfaca2b1defcedb55761acfd6f5fc Mon Sep 17 00:00:00 2001 From: Xithrius Date: Mon, 10 Aug 2020 13:51:51 -0700 Subject: Linted. --- bot/exts/evergreen/conversationstarters.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index 4df3f068..974a361e 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -5,8 +5,8 @@ from pathlib import Path from discord import Embed from discord.ext import commands -from bot.utils.decorators import override_in_channel from bot.constants import WHITELISTED_CHANNELS +from bot.utils.decorators import override_in_channel with Path("bot/resources/evergreen/starter.json").open("r", encoding="utf8") as f: @@ -30,8 +30,9 @@ class ConvoStarters(commands.Cog): @commands.command() @override_in_channel(ALL_ALLOWED_CHANNELS) async def topic(self, ctx: commands.Context) -> None: - """Responds with a random topic to start a conversation - + """ + Responds with a random topic to start a conversation. + If in a Python channel, a python-related topic will be given. Otherwise, a random conversation topic will be recieved by the user. -- cgit v1.2.3 From 141e82af3b5506135a60de8608ec96cc11739097 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Mon, 10 Aug 2020 14:18:42 -0700 Subject: ALL_ALLOWED_CHANNELS now is a list of channel IDs instead of None. --- bot/exts/evergreen/conversationstarters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index 974a361e..8a07eea2 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -18,7 +18,7 @@ with Path("bot/resources/evergreen/py_topics.json").open("r", encoding="utf8") a PY_TOPICS = json.load(f)["python-channels"] # All the allowed channels that the ".topic" command is allowed to be executed in. - ALL_ALLOWED_CHANNELS = [int(channel_id) for channel_id in PY_TOPICS.keys()].extend(WHITELISTED_CHANNELS) + ALL_ALLOWED_CHANNELS = [int(channel_id) for channel_id in PY_TOPICS.keys()] + list(WHITELISTED_CHANNELS) class ConvoStarters(commands.Cog): -- cgit v1.2.3 From df51b8c11a3a42ae13dd00d8b994d5f1d70638e7 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Mon, 10 Aug 2020 14:31:11 -0700 Subject: Gave the Embed the burple color. --- bot/exts/evergreen/conversationstarters.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index 8a07eea2..179fe478 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -2,7 +2,7 @@ import json import random from pathlib import Path -from discord import Embed +from discord import Color, Embed from discord.ext import commands from bot.constants import WHITELISTED_CHANNELS @@ -55,7 +55,9 @@ class ConvoStarters(commands.Cog): description=( "No topics found for this Python channel. You can suggest new ideas for topics " "[here](https://github.com/python-discord/seasonalbot/issues/426)!" - )) + ), + color=Color.blurple + ) await ctx.send(embed=embed) -- cgit v1.2.3 From 1c50a8180809561413ef5cfe73fc0fbc770f8fdc Mon Sep 17 00:00:00 2001 From: Xithrius <15021300+Xithrius@users.noreply.github.com> Date: Mon, 10 Aug 2020 14:43:53 -0700 Subject: Update bot/exts/evergreen/conversationstarters.py Co-authored-by: Dennis Pham --- bot/exts/evergreen/conversationstarters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index 179fe478..757ec059 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -56,7 +56,7 @@ class ConvoStarters(commands.Cog): "No topics found for this Python channel. You can suggest new ideas for topics " "[here](https://github.com/python-discord/seasonalbot/issues/426)!" ), - color=Color.blurple + color=Color.blurple() ) await ctx.send(embed=embed) -- cgit v1.2.3 From b7779d9af8aaf207252a6adb8a6f2214f6c2ed16 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Mon, 17 Aug 2020 03:18:15 -0700 Subject: Changed from json to yaml extraction of channel topics. --- bot/exts/evergreen/conversationstarters.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index 757ec059..c559c2f8 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -1,4 +1,5 @@ import json +import yaml import random from pathlib import Path @@ -13,12 +14,15 @@ with Path("bot/resources/evergreen/starter.json").open("r", encoding="utf8") as STARTERS = json.load(f)["starters"] -with Path("bot/resources/evergreen/py_topics.json").open("r", encoding="utf8") as f: +with Path("bot/resources/evergreen/py_topics.yaml").open("r", encoding="utf8") as f: # First ID is #python-general and the rest are top to bottom categories of Topical Chat/Help. - PY_TOPICS = json.load(f)["python-channels"] + PY_TOPICS = yaml.load(f, Loader=yaml.FullLoader) + + # Removing `None` from lists of topics, if not a list, it is changed to an empty one. + PY_TOPICS = {k: [i for i in v if i] if isinstance(v, list) else [] for k, v in PY_TOPICS.items()} # All the allowed channels that the ".topic" command is allowed to be executed in. - ALL_ALLOWED_CHANNELS = [int(channel_id) for channel_id in PY_TOPICS.keys()] + list(WHITELISTED_CHANNELS) + ALL_ALLOWED_CHANNELS = [channel_id for channel_id in PY_TOPICS.keys()] + list(WHITELISTED_CHANNELS) class ConvoStarters(commands.Cog): @@ -39,7 +43,7 @@ class ConvoStarters(commands.Cog): """ try: # Fetching topics. - channel_topics = PY_TOPICS[str(ctx.channel.id)] + channel_topics = PY_TOPICS[ctx.channel.id] # If the channel isn't Python-related. except KeyError: -- cgit v1.2.3 From 809a4458e418d15288749c69b061990b6b737716 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Mon, 17 Aug 2020 03:36:57 -0700 Subject: Sorted imports to comply with flake8 standards. --- bot/exts/evergreen/conversationstarters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/evergreen/conversationstarters.py') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index c559c2f8..cfa8dbce 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -1,8 +1,8 @@ import json -import yaml import random from pathlib import Path +import yaml from discord import Color, Embed from discord.ext import commands -- cgit v1.2.3