From f2b509f9fa4af2bda050a1d0e5c9504b2c02c208 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Wed, 23 May 2018 18:28:24 +0100 Subject: [PATCH 1/8] Fixed potential case of GC crippling Cite: https://docs.python.org/3/library/inspect.html#the-interpreter-stack --- src/shared_libs/ioutils.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/shared_libs/ioutils.py b/src/shared_libs/ioutils.py index 284f156..7609aa7 100644 --- a/src/shared_libs/ioutils.py +++ b/src/shared_libs/ioutils.py @@ -3,6 +3,7 @@ """ IO stuff. """ +import copy import inspect import os @@ -42,13 +43,20 @@ def in_here(first_path_bit: str, *path_bits: str, stack_depth: int=0) -> str: raise RuntimeError('Could not find a stack record. Interpreter has ' 'been shot.') else: - module = inspect.getmodule(frame[0]) + module = inspect.getmodule(frame[0]) assert hasattr(module, '__file__'), 'No `__file__\' attr, welp.' - + + # https://docs.python.org/3/library/inspect.html#the-interpreter-stack + # If Python caches strings rather than copying when we move them + # around or modify them, then this may cause a referential cycle which + # will consume more memory and stop the garbage collection system + # from working correctly. Best thing to do here is deepcopy anything + # we need and prevent this occuring. Del the references to allow them + # to be freed. file = module.__file__ - + file = copy.deepcopy(file) + del module, frame dir_name = os.path.dirname(file) abs_dir_name = os.path.abspath(dir_name) - pathish = os.path.join(abs_dir_name, first_path_bit, *path_bits) return pathish From bc8d2d31538dcd048e6c39b5bd64709ec876d128 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Wed, 23 May 2018 18:34:54 +0100 Subject: [PATCH 2/8] Update __main__.py --- src/__main__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/__main__.py b/src/__main__.py index e6b15ea..964d74e 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -11,6 +11,7 @@ import logging import random import traceback import os +import sys import discord from discord.ext import commands @@ -27,9 +28,15 @@ logging.basicConfig(level='INFO') # If uvloop is installed, change to that eventloop policy as it # is more efficient try: - import uvloop - asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) - del uvloop + # https://stackoverflow.com/a/45700730 + if sys.platform == 'win32': + loop = asyncio.ProactorEventLoop() + asyncio.set_event_loop(loop) + logging.warning('Detected Windows. Changing event loop to ProactorEventLoop.') + else: + import uvloop + asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + del uvloop except BaseException as ex: logging.warning(f'Could not load uvloop. {type(ex).__qualname__}: {ex};', 'reverting to default impl.') From 4540c36d22c438297d84d2cb8f375bdb690a79ff Mon Sep 17 00:00:00 2001 From: annihilator708 Date: Wed, 23 May 2018 19:40:23 +0200 Subject: [PATCH 3/8] Fix issue with gitignore, (i hope), Fixed import error. revert raatty change in self.ownerlist. still not working --- .gitignore | 4 ++-- src/cogs/git.py | 6 ++++-- src/config/config.py | 2 -- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index e9d30ac..1219eb2 100644 --- a/.gitignore +++ b/.gitignore @@ -108,8 +108,8 @@ venv.bak/ # mypy .mypy_cache/ -/src/config/Config.json -/src/config/PrivateConfig.json +src/config/Config.json +src/config/PrivateConfig.json # Ignore dockerfiles docker-compose.yml \ No newline at end of file diff --git a/src/cogs/git.py b/src/cogs/git.py index a49ec95..e95cfc3 100644 --- a/src/cogs/git.py +++ b/src/cogs/git.py @@ -30,11 +30,13 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import discord from discord.ext import commands import logging -from ..shared_libs.utils import paginate, run_command, loggable +from src.shared_libs.utils import paginate, run_command +from src.shared_libs.loggable import Loggable + import asyncio -class Git(loggable.Loggable): +class Git(Loggable): def __init__(self, bot): self.bot = bot diff --git a/src/config/config.py b/src/config/config.py index 4604a78..0115c11 100644 --- a/src/config/config.py +++ b/src/config/config.py @@ -17,8 +17,6 @@ class LoadConfig: # Initialize config self.ownerlist = self.config["ownerlist"] - if self.ownerlist == []: - self.ownerlist = [int(i) for i in os.getenv('ownerlist').split(',')] self.defaultprefix = self.config["prefix"] self.version = self.config["version"] self.display_name = self.config["display_name"] From 489336e251eff5314cb96c41c7070d61ee4a71f7 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Wed, 23 May 2018 18:56:04 +0100 Subject: [PATCH 4/8] Update README.md --- README.md | 62 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 167bdb1..a290b5a 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,57 @@ # Sebi-Machine -Dedicated discord bot for Sebi's bot tutorial. - -http://discord.gg/GWdhBSp +Dedicated Discord bot for [Sebi's bot tutorial](http://discord.gg/GWdhBSp). ## Important things to know This bot extends the rewrite version of discord.py. A couple of variables have been added to give you easy access to a couple of objects listed here. -> self.bot.ownerlist +> `self.bot.ownerlist` -self.ownerlist can be used to retrieve a `list` of user ID's. (`int`). Those ID's belong to contributors. -> self.bot.defaultprefix +`self.ownerlist` can be used to retrieve a `list` of user ID's. (`int`). Those ID's belong to contributors. -self.defaultprefix can be used to retrieve a `str` object of the default prefix. -> self.bot.version +> `self.bot.defaultprefix` -self.version can be used to retrieve a `float` which represent the version number of the bot. -> self.bot.display_name +`self.defaultprefix` can be used to retrieve a `str` object of the default prefix. -self.display_name returns a `str` which represent the display_name of the bot. -> self.bot.mainenance +> `self.bot.version` + +`self.version` can be used to retrieve a `float` which represent the version number of the bot. + +> `self.bot.display_name` + +`self.display_name` returns a `str` which represent the `display_name` of the bot. + +> `self.bot.mainenance` + +`self.maintenance` is equal to `True` or `False`. If you would like to exclude code in the master branch, use this. +Make sure this one is installed. Example: -self.maintenance is equal to `True` or `False`. If you would like to exclude code in the master branch, use this. -Make sure this one is installed. -example: ```py if self.bot.mainenance: print('I am in the development branch') if not self.bot.mainenance: - print('I am in the master branch) + print('I am in the master branch') ``` -With other words. self.mainenance returns False in production and True in developer modus. +In other words. `self.mainenance` returns `False` in production and `True` in developer modes. -> self.bot.embed_color +> `self.bot.embed_color` -self.embed_color can be used to use the default color of out embed theme. -``` +`self.embed_color` can be used to use the default color of out embed theme. + +```python discord.Embed(title='Foo', description='bar', color=self.bot.embed_color) ``` ## Docker environment -This bot is heavly based on docker. This means it will run in a container. Other words. The code will run in a jail. Dont be afraid for bugs that cause harm. or commands that could potential restarts the server. Its safe. +This bot is heavly based on docker. This means it will run in a container. Other words. The code will run in a jail. Dont be afraid for bugs that cause harm. or commands that could potential restarts the server. It's safe. There are a couple of things to know about docker within this project. -1. Please read the docs of docker first before editing the docker files -2. If you need a pip package, place the name into requirements.txt, docker handles the rest. -3. Everything in project folder is the workfolder of the docker container -4. Initialize cogs by adding them into cogs.txt. one line is one cogfile + +1. Please read the docs of docker first before editing the docker files; +2. If you need a pip package, place the name into requirements.txt: docker handles the rest; +3. Everything in project folder is the workfolder of the docker container; +4. Initialize cogs by adding them into `cogs.txt`: one line is one cogfile. ## Initialize a cog Put your cog in `src/cogs` and edit the `cogs.txt` file. Add the filename of your cog into `cogs.txt`. No absolute path, just the name. @@ -57,10 +61,12 @@ There is a git command available provided by Dusty. `S!git pull` should pull the If you are stuck in any way shape or form you can always contact anyone who works on this project. Dont forget to check `S!help`. ## Project links: - - http://discord.gg/GWdhBSp - - http://chillout.ueuo.com - - http://trello.com/b/x02goBbW/sebis-bot-tutorial-roadmap +- http://discord.gg/GWdhBSp +- http://chillout.ueuo.com +- http://trello.com/b/x02goBbW/sebis-bot-tutorial-roadmap ## Deploy to heroku + For testing purposes you can click the link below to build your own copy of this repo you just pick an app name fill in the config variables then switch it on in resources tab. + [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/Annihilator708/Sebi-Machine/tree/development) From 457c90a37452fac674e17b66459ec7af2649ad71 Mon Sep 17 00:00:00 2001 From: annihilator708 Date: Wed, 23 May 2018 21:58:41 +0200 Subject: [PATCH 5/8] Add on_message, ignore command not found errors --- src/__main__.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/__main__.py b/src/__main__.py index 964d74e..4431875 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -87,6 +87,11 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable): # CommandErrors triggered by other propagating errors tend to get wrapped. This means # if we have a cause, we should probably consider unwrapping that so we get a useful # message. + + # If command is not found, return + if isinstance(error, discord.ext.commands.errors.CommandNotFound): + return + error = error.__cause__ or error tb = traceback.format_exception(type(error), error, error.__traceback__, limit=2, chain=False) tb = ''.join(tb) @@ -101,6 +106,31 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable): traceback.print_exc() + async def on_message(self, message): + # Make sure people can't change the username + if message.guild: + if message.guild.me.display_name != self.display_name: + try: + await message.guild.me.edit(nick=self.display_name) + except: + pass + + # If author is a bot, ignore the message + if message.author.bot: return + + # Make sure the command get processed as if it was typed with lowercase + # Split message.content one first space + command = message.content.split(None, 1) + if command: + command[0] = command[0].lower() + message.content = ' '.join(command) + message.content = ' '.join(command) + + # process command + await self.process_commands(message) + + + client = SebiMachine() # Make sure the key stays private. # I am 99% certain this is valid! From 0e6c6ab0b1608fbb7cb58941ce102d030ee97a17 Mon Sep 17 00:00:00 2001 From: annihilator708 Date: Wed, 23 May 2018 21:59:21 +0200 Subject: [PATCH 6/8] remove unused var --- src/__main__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/__main__.py b/src/__main__.py index 4431875..0c345a1 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -97,7 +97,6 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable): tb = ''.join(tb) joke = random.choice(jokes) fmt = f'**`{self.defaultprefix}{ctx.command}`**\n{joke}\n\n**{type(error).__name__}:**:\n```py\n{tb}\n```' - simple_fmt = f'**`{self.defaultprefix}{ctx.command}`**\n{joke}\n\n**{type(error).__name__}:**:\n**`{error}`**' # Stops the error handler erroring. try: From 21d60a8f3e4658bb30f8e74a7dc6303f575e9bce Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Wed, 23 May 2018 12:14:33 -0800 Subject: [PATCH 7/8] Changed development prefix --- src/config/Config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/Config.json b/src/config/Config.json index 62c06ae..30af5fc 100644 --- a/src/config/Config.json +++ b/src/config/Config.json @@ -3,5 +3,5 @@ "display_name" : "[S!] Sebi-Machine", "maintenance": "True", "ownerlist": [], - "prefix": "S!" -} \ No newline at end of file + "prefix": "sd!" +} From bb933b4f7b8e4b2a8939344ef81c14b2054f6b15 Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Wed, 23 May 2018 12:16:42 -0800 Subject: [PATCH 8/8] Changed Display name --- src/config/Config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/Config.json b/src/config/Config.json index 30af5fc..f4a48ef 100644 --- a/src/config/Config.json +++ b/src/config/Config.json @@ -1,7 +1,7 @@ { "version": 0.1, - "display_name" : "[S!] Sebi-Machine", + "display_name" : "[ds!] Dev-Sebi", "maintenance": "True", "ownerlist": [], - "prefix": "sd!" + "prefix": "ds!" }