From f6af25bc92eb6106797db7a5a2fd11f98bd08075 Mon Sep 17 00:00:00 2001 From: Chris Gallardo Date: Thu, 1 Oct 2020 10:42:56 -0700 Subject: Add Cog and add a couple responses --- bot/exts/easter/save_the_planet.py | 51 +++++++++++++++++++++++++++++++ bot/resources/easter/save_the_planet.json | 17 +++++++++++ 2 files changed, 68 insertions(+) create mode 100644 bot/exts/easter/save_the_planet.py create mode 100644 bot/resources/easter/save_the_planet.json diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py new file mode 100644 index 00000000..1a20134e --- /dev/null +++ b/bot/exts/easter/save_the_planet.py @@ -0,0 +1,51 @@ +from discord.ext import commands +from pathlib import Path +from discord import Embed +import random +import json + +embeds = [] + +with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding="utf8") as f: + responses = json.load(f) + +# convert what's in the json to discord embed objects https://discord.com/developers/docs/resources/channel#embed-object +for response in responses["embeds"]: + response["title"] = f"Save the Planet: {response['topic']}" + response["footer"] = "The best thing you can do is sharing this information!" + response["image"] = { + "url": response["image_url"] + } + response["fields"] = [ + { + "name": "The Problem", + "value": response["problem"], + "inline": False + }, + { + "name": "What you can do", + "value": response["solution"] + } + ] + + embeds.append(Embed.from_dict(response)) + + +# cog +class SaveThePlanet(commands.Cog): + """A cog that teaches users how they can help our planet.""" + + def __init__(self, bot: commands.Bot) -> None: + self.bot = bot + + def get_response(self) -> Embed: + return random.choice(embeds) + + @commands.command(aliases=('save_the_earth',)) + async def save_the_planet(self, ctx: commands.Context) -> None: + """Responds with a random tip on how to be ecofriendly and help our planet.""" + await ctx.send(embed=self.get_response()) + +def setup(bot: commands.Bot) -> None: + """save_the_planet Cog load.""" + bot.add_cog(SaveThePlanet(bot)) \ No newline at end of file diff --git a/bot/resources/easter/save_the_planet.json b/bot/resources/easter/save_the_planet.json new file mode 100644 index 00000000..83c4a966 --- /dev/null +++ b/bot/resources/easter/save_the_planet.json @@ -0,0 +1,17 @@ +{ + "embeds": [ + { + "topic": "Choose Reneweable Energy", + "image_url": "https://cdn.dnaindia.com/sites/default/files/styles/full/public/2019/07/23/851602-renewable-energy-istock-072419.jpg", + "problem": "Getting energy from oil or fossil fuels isn't a good idea, because there is only so much of it.", + "solution": "Use renewable energy, such as wind, solar, and hydro, because it is healthier and is not a finite resource!" + }, + + { + "topic": "Save the Trees!", + "image_url": "https://www.thecollegesolution.com/wp-content/uploads/2014/07/crumpled-paper-1.jpg", + "problem": "We often waste trees on making paper, and just getting rid of them for no good reason.", + "solution": "Make sure you only use paper when absolutely neccesary. Find ways to plant trees (participate in Hacktober!) to combat losing them." + } + ] +} \ No newline at end of file -- cgit v1.2.3 From a5a032ff351c6c636955c518598e3819a675ef93 Mon Sep 17 00:00:00 2001 From: Chris Gallardo Date: Thu, 1 Oct 2020 16:21:38 -0700 Subject: Fix errors --- bot/exts/easter/save_the_planet.py | 49 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 1a20134e..84bd44ab 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -6,31 +6,6 @@ import json embeds = [] -with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding="utf8") as f: - responses = json.load(f) - -# convert what's in the json to discord embed objects https://discord.com/developers/docs/resources/channel#embed-object -for response in responses["embeds"]: - response["title"] = f"Save the Planet: {response['topic']}" - response["footer"] = "The best thing you can do is sharing this information!" - response["image"] = { - "url": response["image_url"] - } - response["fields"] = [ - { - "name": "The Problem", - "value": response["problem"], - "inline": False - }, - { - "name": "What you can do", - "value": response["solution"] - } - ] - - embeds.append(Embed.from_dict(response)) - - # cog class SaveThePlanet(commands.Cog): """A cog that teaches users how they can help our planet.""" @@ -48,4 +23,28 @@ class SaveThePlanet(commands.Cog): def setup(bot: commands.Bot) -> None: """save_the_planet Cog load.""" + + with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding="utf8") as f: + responses = json.load(f) + + # convert what's in the json to discord embed objects https://discord.com/developers/docs/resources/channel#embed-object + for response in responses["embeds"]: + response["title"] = f"Save the Planet: {response['topic']}" + response["footer"] = {"text": "The best thing you can do is sharing this information!"} + response["image"] = { + "url": response["image_url"] + } + response["fields"] = [ + { + "name": "The Problem", + "value": response["problem"], + "inline": False + }, + { + "name": "What you can do", + "value": response["solution"] + } + ] + + embeds.append(Embed.from_dict(response)) bot.add_cog(SaveThePlanet(bot)) \ No newline at end of file -- cgit v1.2.3 From 7a3d22fbeea48e9f699b7401e77d51cd3197c428 Mon Sep 17 00:00:00 2001 From: Chris Gallardo <40961096+ChrisGallardo@users.noreply.github.com> Date: Fri, 2 Oct 2020 07:04:17 -0700 Subject: Take @thomaspet's suggestion for SaveThePlanetCog Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/easter/save_the_planet.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 84bd44ab..4ea7a5fb 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -6,7 +6,6 @@ import json embeds = [] -# cog class SaveThePlanet(commands.Cog): """A cog that teaches users how they can help our planet.""" @@ -47,4 +46,4 @@ def setup(bot: commands.Bot) -> None: ] embeds.append(Embed.from_dict(response)) - bot.add_cog(SaveThePlanet(bot)) \ No newline at end of file + bot.add_cog(SaveThePlanet(bot)) -- cgit v1.2.3 From cc6b211f3b65d43afc12da439b1d8755e90f2c18 Mon Sep 17 00:00:00 2001 From: Chris Gallardo Date: Fri, 2 Oct 2020 07:30:59 -0700 Subject: run lint --- bot/exts/easter/save_the_planet.py | 16 ++++++++++------ bot/resources/easter/save_the_planet.json | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 84bd44ab..d8bbef20 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -1,11 +1,14 @@ -from discord.ext import commands +import json +import random from pathlib import Path + from discord import Embed -import random -import json +from discord.ext import commands + embeds = [] + # cog class SaveThePlanet(commands.Cog): """A cog that teaches users how they can help our planet.""" @@ -14,6 +17,7 @@ class SaveThePlanet(commands.Cog): self.bot = bot def get_response(self) -> Embed: + """Return a random Embed from the list of embeds.""" return random.choice(embeds) @commands.command(aliases=('save_the_earth',)) @@ -21,13 +25,13 @@ class SaveThePlanet(commands.Cog): """Responds with a random tip on how to be ecofriendly and help our planet.""" await ctx.send(embed=self.get_response()) + def setup(bot: commands.Bot) -> None: """save_the_planet Cog load.""" - with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding="utf8") as f: responses = json.load(f) - # convert what's in the json to discord embed objects https://discord.com/developers/docs/resources/channel#embed-object + # convert what's in the json to discord embed objects for response in responses["embeds"]: response["title"] = f"Save the Planet: {response['topic']}" response["footer"] = {"text": "The best thing you can do is sharing this information!"} @@ -47,4 +51,4 @@ def setup(bot: commands.Bot) -> None: ] embeds.append(Embed.from_dict(response)) - bot.add_cog(SaveThePlanet(bot)) \ No newline at end of file + bot.add_cog(SaveThePlanet(bot)) diff --git a/bot/resources/easter/save_the_planet.json b/bot/resources/easter/save_the_planet.json index 83c4a966..c7064f71 100644 --- a/bot/resources/easter/save_the_planet.json +++ b/bot/resources/easter/save_the_planet.json @@ -14,4 +14,4 @@ "solution": "Make sure you only use paper when absolutely neccesary. Find ways to plant trees (participate in Hacktober!) to combat losing them." } ] -} \ No newline at end of file +} -- cgit v1.2.3 From dd4b79873b8ea001dd3f343763fcab7821a107ca Mon Sep 17 00:00:00 2001 From: Chris Gallardo <40961096+ChrisGallardo@users.noreply.github.com> Date: Fri, 2 Oct 2020 07:41:50 -0700 Subject: Fix styling --- bot/exts/easter/save_the_planet.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 4ea7a5fb..6019304d 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -1,11 +1,14 @@ -from discord.ext import commands +import json +import random from pathlib import Path + from discord import Embed -import random -import json +from discord.ext import commands + embeds = [] + class SaveThePlanet(commands.Cog): """A cog that teaches users how they can help our planet.""" @@ -13,6 +16,7 @@ class SaveThePlanet(commands.Cog): self.bot = bot def get_response(self) -> Embed: + """Return a random Embed from the list of embeds.""" return random.choice(embeds) @commands.command(aliases=('save_the_earth',)) @@ -20,13 +24,13 @@ class SaveThePlanet(commands.Cog): """Responds with a random tip on how to be ecofriendly and help our planet.""" await ctx.send(embed=self.get_response()) + def setup(bot: commands.Bot) -> None: """save_the_planet Cog load.""" - with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding="utf8") as f: responses = json.load(f) - # convert what's in the json to discord embed objects https://discord.com/developers/docs/resources/channel#embed-object + # convert what's in the json to discord embed objects for response in responses["embeds"]: response["title"] = f"Save the Planet: {response['topic']}" response["footer"] = {"text": "The best thing you can do is sharing this information!"} -- cgit v1.2.3 From af275fdbb150d86849fed5d04c23c266b1ac79d1 Mon Sep 17 00:00:00 2001 From: Chris Gallardo Date: Fri, 2 Oct 2020 15:27:57 -0700 Subject: Added responses --- bot/exts/easter/save_the_planet.py | 4 ++-- bot/resources/easter/save_the_planet.json | 36 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index ff4893a8..1494d9f5 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -14,8 +14,8 @@ class SaveThePlanet(commands.Cog): def __init__(self, bot: commands.Bot) -> None: self.bot = bot with open(Path("bot/resources/save_the_planet.json"), 'r', encoding='utf8') as f: - for key, embed in json.load(f).items(): - self.json_embeds[key] = embed + for embed in json.load(f): + self.json_embeds.append(embed) @commands.command(aliases=('savetheearth', 'saveplanet', 'saveearth')) async def savetheplanet(self, ctx: commands.Context) -> None: diff --git a/bot/resources/easter/save_the_planet.json b/bot/resources/easter/save_the_planet.json index a8aa1f31..eb4304a3 100644 --- a/bot/resources/easter/save_the_planet.json +++ b/bot/resources/easter/save_the_planet.json @@ -2,6 +2,7 @@ { "title": "Save the Planet: Choose Reneweable Energy", "image": {"url": "https://cdn.dnaindia.com/sites/default/files/styles/full/public/2019/07/23/851602-renewable-energy-istock-072419.jpg"}, + "footer": {"text": "The best thing you can do is sharing this information!"}, "fields": [ { "name": "The Problem", @@ -20,6 +21,7 @@ { "title": "Save the Planet: Save the Trees!", "image": {"url": "https://www.thecollegesolution.com/wp-content/uploads/2014/07/crumpled-paper-1.jpg"}, + "footer": {"text": "The best thing you can do is sharing this information!"}, "fields": [ { "name": "The Problem", @@ -29,7 +31,39 @@ { "name": "What you can do", - "value": "Make sure you only use paper when absolutely neccesary. Find ways to plant trees (Hacktober Fest!) to combat losing them.", + "value": "Make sure you only use paper when absolutely neccesary. When you do, make sure to use recycled paper because making new paper causes pollution. Find ways to plant trees (Hacktober Fest!) to combat losing them.", + "inline": false + } + ] + }, + + { + "title": "Save the Planet: Less time in the Car!", + "image": {"url": "https://www.careeraddict.com/uploads/article/55294/businessman-riding-bike.jpg"}, + "footer": {"text": "The best thing you can do is sharing this information!"}, + "fields": [ + { + "name": "The Problem", + "value": "Every mile you drive to work produces about a pound of C0₂. That's crazy! What's crazier is how clean the planet could be if we spent less time in the car!", + "inline": false + }, + + { + "name": "What you can do", + "value": "Instead of using your car, ride your bike if possible! Not only does it save that pound of C0₂, it is also great excercise and is cheaper!", + "inline": false + } + ] + }, + + { + "title":"Save the Planet: Paint your roof white!", + "image": {"url": "https://modernize.com/wp-content/uploads/2016/10/Cool-roof.jpg"}, + "footer": {"text":"The best thing you can do is sharing this information!"}, + "fields": [ + { + "name": "But why?!", + "value": "Having a light-colored roof will reduce the heat coming into your house, which will save your energy bill. Also, Researchers at the Lawrence Berkeley National Laboratory estimated that if 80 percent of roofs in tropical and temperate climate areas were painted white, it could offset the greenhouse gas emissions of 300 million automobiles around the world.", "inline": false } ] -- cgit v1.2.3 From a43a650e57093f2154f073ecc703f2a7bf274659 Mon Sep 17 00:00:00 2001 From: Chris Gallardo Date: Sat, 3 Oct 2020 10:09:59 -0700 Subject: @gustavwilliam's suggestions --- bot/exts/easter/save_the_planet.py | 13 +++++++------ bot/resources/easter/save_the_planet.json | 32 ++++++++++++++++++------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 1494d9f5..46bcb514 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -9,20 +9,21 @@ from discord.ext import commands class SaveThePlanet(commands.Cog): """A cog that teaches users how they can help our planet.""" - json_embeds = [] + embed_data = [] def __init__(self, bot: commands.Bot) -> None: self.bot = bot - with open(Path("bot/resources/save_the_planet.json"), 'r', encoding='utf8') as f: + with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding='utf8') as f: for embed in json.load(f): - self.json_embeds.append(embed) + self.embed_data.append(embed) @commands.command(aliases=('savetheearth', 'saveplanet', 'saveearth')) async def savetheplanet(self, ctx: commands.Context) -> None: - """Responds with a random tip on how to be ecofriendly and help our planet.""" - await ctx.send(embed=Embed.from_dict(random.choice(self.json_embeds))) + """Responds with a random tip on how to be eco-friendly and help our planet.""" + return_embed = Embed.from_dict(random.choice(self.embed_data)) + await ctx.send(embed=return_embed) def setup(bot: commands.Bot) -> None: - """save_the_planet Cog load.""" + """Save the Planet Cog load.""" bot.add_cog(SaveThePlanet(bot)) diff --git a/bot/resources/easter/save_the_planet.json b/bot/resources/easter/save_the_planet.json index eb4304a3..816611c4 100644 --- a/bot/resources/easter/save_the_planet.json +++ b/bot/resources/easter/save_the_planet.json @@ -1,11 +1,11 @@ [ { - "title": "Save the Planet: Choose Reneweable Energy", + "title": "Choose reneweable energy", "image": {"url": "https://cdn.dnaindia.com/sites/default/files/styles/full/public/2019/07/23/851602-renewable-energy-istock-072419.jpg"}, - "footer": {"text": "The best thing you can do is sharing this information!"}, + "footer": {"text": "Help out by sharing this information!"}, "fields": [ { - "name": "The Problem", + "name": "The problem", "value": "Getting energy from oil or fossil fuels isn't a good idea, because there is only so much of it.", "inline": false }, @@ -19,12 +19,12 @@ }, { - "title": "Save the Planet: Save the Trees!", + "title": "Save the trees!", "image": {"url": "https://www.thecollegesolution.com/wp-content/uploads/2014/07/crumpled-paper-1.jpg"}, - "footer": {"text": "The best thing you can do is sharing this information!"}, + "footer": {"text": "Help out by sharing this information!"}, "fields": [ { - "name": "The Problem", + "name": "The problem", "value": "We often waste trees on making paper, and just getting rid of them for no good reason.", "inline": false }, @@ -38,12 +38,12 @@ }, { - "title": "Save the Planet: Less time in the Car!", + "title": "Less time in the car!", "image": {"url": "https://www.careeraddict.com/uploads/article/55294/businessman-riding-bike.jpg"}, - "footer": {"text": "The best thing you can do is sharing this information!"}, + "footer": {"text": "Help out by sharing this information!"}, "fields": [ { - "name": "The Problem", + "name": "The problem", "value": "Every mile you drive to work produces about a pound of C0₂. That's crazy! What's crazier is how clean the planet could be if we spent less time in the car!", "inline": false }, @@ -57,13 +57,19 @@ }, { - "title":"Save the Planet: Paint your roof white!", + "title":"Paint your roof white!", "image": {"url": "https://modernize.com/wp-content/uploads/2016/10/Cool-roof.jpg"}, - "footer": {"text":"The best thing you can do is sharing this information!"}, + "footer": {"text":"Help out by sharing this information!"}, "fields": [ { - "name": "But why?!", - "value": "Having a light-colored roof will reduce the heat coming into your house, which will save your energy bill. Also, Researchers at the Lawrence Berkeley National Laboratory estimated that if 80 percent of roofs in tropical and temperate climate areas were painted white, it could offset the greenhouse gas emissions of 300 million automobiles around the world.", + "name": "The problem", + "value": "People with dark roofs often spend 20 to 40% more on their electricity bills because of the extra heat, which means more electricity needs to be made, and a lot of it isn't renewable.", + "inline": false + }, + + { + "name":"What you can do", + "value": "Having a light colored roof will save you money, and also researchers at the Lawrence Berkeley National Laboratory estimated that if 80 percent of roofs in tropical and temperate climate areas were painted white, it could offset the greenhouse gas emissions of 300 million automobiles around the world.", "inline": false } ] -- cgit v1.2.3 From dae123f9de268604cbe933479de87ea5e5f64a67 Mon Sep 17 00:00:00 2001 From: Chris Gallardo <40961096+ChrisGallardo@users.noreply.github.com> Date: Sat, 3 Oct 2020 17:22:24 -0700 Subject: Apply suggestions from @gustavwilliam's code review Co-authored-by: gustavwilliam <65498475+gustavwilliam@users.noreply.github.com> --- bot/exts/easter/save_the_planet.py | 7 +++---- bot/resources/easter/save_the_planet.json | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 46bcb514..41a9e25d 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -9,13 +9,12 @@ from discord.ext import commands class SaveThePlanet(commands.Cog): """A cog that teaches users how they can help our planet.""" - embed_data = [] - def __init__(self, bot: commands.Bot) -> None: self.bot = bot + self.embed_data = [] + with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding='utf8') as f: - for embed in json.load(f): - self.embed_data.append(embed) + self.embed_data = [embed for embed in json.load(f)] @commands.command(aliases=('savetheearth', 'saveplanet', 'saveearth')) async def savetheplanet(self, ctx: commands.Context) -> None: diff --git a/bot/resources/easter/save_the_planet.json b/bot/resources/easter/save_the_planet.json index 816611c4..f22261b7 100644 --- a/bot/resources/easter/save_the_planet.json +++ b/bot/resources/easter/save_the_planet.json @@ -1,6 +1,6 @@ [ { - "title": "Choose reneweable energy", + "title": "Choose renewable energy", "image": {"url": "https://cdn.dnaindia.com/sites/default/files/styles/full/public/2019/07/23/851602-renewable-energy-istock-072419.jpg"}, "footer": {"text": "Help out by sharing this information!"}, "fields": [ @@ -31,7 +31,7 @@ { "name": "What you can do", - "value": "Make sure you only use paper when absolutely neccesary. When you do, make sure to use recycled paper because making new paper causes pollution. Find ways to plant trees (Hacktober Fest!) to combat losing them.", + "value": "Make sure you only use paper when absolutely necessary. When you do, make sure to use recycled paper because making new paper causes pollution. Find ways to plant trees (Hacktober Fest!) to combat losing them.", "inline": false } ] @@ -50,7 +50,7 @@ { "name": "What you can do", - "value": "Instead of using your car, ride your bike if possible! Not only does it save that pound of C0₂, it is also great excercise and is cheaper!", + "value": "Instead of using your car, ride your bike if possible! Not only does it save that pound of C0₂, it is also great exercise and is cheaper!", "inline": false } ] -- cgit v1.2.3 From 122e471690a588c1bfc6224ca8afa85063001691 Mon Sep 17 00:00:00 2001 From: Chris Gallardo <40961096+ChrisGallardo@users.noreply.github.com> Date: Sun, 4 Oct 2020 12:00:44 -0700 Subject: Apply suggestions from code review Co-authored-by: gustavwilliam <65498475+gustavwilliam@users.noreply.github.com> --- bot/exts/easter/save_the_planet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 41a9e25d..811feb93 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -14,7 +14,7 @@ class SaveThePlanet(commands.Cog): self.embed_data = [] with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding='utf8') as f: - self.embed_data = [embed for embed in json.load(f)] + self.embed_data = json.load(f) @commands.command(aliases=('savetheearth', 'saveplanet', 'saveearth')) async def savetheplanet(self, ctx: commands.Context) -> None: -- cgit v1.2.3 From 46ece4e009788e930c17aaf3d0f67d130c7f0514 Mon Sep 17 00:00:00 2001 From: Chris Gallardo Date: Mon, 5 Oct 2020 08:55:45 -0700 Subject: apply @Den4200's suggestions Co-Authored-By: Dennis Pham --- bot/exts/easter/save_the_planet.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 811feb93..99f06bbc 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -1,25 +1,28 @@ import json -import random from pathlib import Path from discord import Embed from discord.ext import commands +from bot.utils import RandomCycle + +EMBED_DATA = [] + class SaveThePlanet(commands.Cog): """A cog that teaches users how they can help our planet.""" def __init__(self, bot: commands.Bot) -> None: self.bot = bot - self.embed_data = [] - with open(Path("bot/resources/easter/save_the_planet.json"), 'r', encoding='utf8') as f: - self.embed_data = json.load(f) + with Path("bot/resources/easter/save_the_planet.json").open('r', encoding='utf8') as f: + global EMBED_DATA + EMBED_DATA = RandomCycle(json.load(f)) @commands.command(aliases=('savetheearth', 'saveplanet', 'saveearth')) async def savetheplanet(self, ctx: commands.Context) -> None: """Responds with a random tip on how to be eco-friendly and help our planet.""" - return_embed = Embed.from_dict(random.choice(self.embed_data)) + return_embed = Embed.from_dict(next(EMBED_DATA)) await ctx.send(embed=return_embed) -- cgit v1.2.3 From afaa15d3dc6e81f09ccf9e0d4cb33715aae8aedc Mon Sep 17 00:00:00 2001 From: Chris Gallardo Date: Mon, 5 Oct 2020 14:54:50 -0700 Subject: apply @Den4200's suggestions Co-Authored-By: Dennis Pham --- bot/exts/easter/save_the_planet.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/bot/exts/easter/save_the_planet.py b/bot/exts/easter/save_the_planet.py index 99f06bbc..8f644259 100644 --- a/bot/exts/easter/save_the_planet.py +++ b/bot/exts/easter/save_the_planet.py @@ -4,9 +4,11 @@ from pathlib import Path from discord import Embed from discord.ext import commands -from bot.utils import RandomCycle +from bot.utils.randomization import RandomCycle -EMBED_DATA = [] + +with Path("bot/resources/easter/save_the_planet.json").open('r', encoding='utf8') as f: + EMBED_DATA = RandomCycle(json.load(f)) class SaveThePlanet(commands.Cog): @@ -15,10 +17,6 @@ class SaveThePlanet(commands.Cog): def __init__(self, bot: commands.Bot) -> None: self.bot = bot - with Path("bot/resources/easter/save_the_planet.json").open('r', encoding='utf8') as f: - global EMBED_DATA - EMBED_DATA = RandomCycle(json.load(f)) - @commands.command(aliases=('savetheearth', 'saveplanet', 'saveearth')) async def savetheplanet(self, ctx: commands.Context) -> None: """Responds with a random tip on how to be eco-friendly and help our planet.""" -- cgit v1.2.3