Merge remote-tracking branch 'origin/development' into development

# Conflicts:
#	src/cogs/git.py
This commit is contained in:
Dustin Pianalto 2018-05-23 14:13:59 -08:00
commit a3c7f25109
7 changed files with 186 additions and 138 deletions

4
.gitignore vendored
View File

@ -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

View File

@ -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)

View File

@ -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.')
@ -80,12 +87,16 @@ 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)
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:
@ -94,6 +105,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!

View File

@ -1,95 +1,95 @@
"""
===
MIT License
Copyright (c) 2018 Dusty.P https://github.com/dustinpianalto
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import discord
from discord.ext import commands
from src.shared_libs.utils import paginate, run_command
from src.shared_libs.loggable import Loggable
import asyncio
class Git(Loggable):
def __init__(self, bot):
self.bot = bot
@commands.group(case_insensitive=True, invoke_without_command=True)
async def git(self, ctx):
"""Run help git for more info"""
await ctx.send('https://github.com/Annihilator708/Sebi-Machine/')
@commands.command(case_insensitive=True, brief='Gets the Trello link.')
async def trello(self, ctx):
await ctx.send('<https://trello.com/b/x02goBbW/sebis-bot-tutorial-roadmap>')
@git.command()
async def pull(self, ctx):
self.logger.warning('Invoking git-pull')
await ctx.trigger_typing()
if ctx.author.id not in self.bot.ownerlist:
return await ctx.send('Only my contributors can use me like this :blush:', delete_after=10)
em = discord.Embed(style='rich',
title=f'Git Pull',
color=self.bot.embed_color)
em.set_thumbnail(url=f'{ctx.guild.me.avatar_url}')
# Pretty sure you can just do await run_command() if that is async,
# or run in a TPE otherwise.
result = await asyncio.wait_for(self.bot.loop.create_task(
run_command('git fetch --all')), 120) + '\n'
result += await asyncio.wait_for(self.bot.loop.create_task(
run_command('git reset --hard origin/$(git rev-parse '
'--symbolic-full-name --abbrev-ref HEAD)')),
120) + '\n\n'
result += await asyncio.wait_for(self.bot.loop.create_task(
run_command('git show --stat | sed "s/.*@.*[.].*/ /g"')), 10)
results = paginate(result, maxlen=1014)
for page in results[:5]:
em.add_field(name='\uFFF0', value=f'{page}')
await ctx.send(embed=em)
@git.command()
async def status(self, ctx):
await ctx.trigger_typing()
if ctx.author.id not in self.bot.ownerlist:
return await ctx.send('Only my contributors can use me like this :blush:', delete_after=10)
em = discord.Embed(style='rich',
title=f'Git Status',
color=self.bot.embed_color)
em.set_thumbnail(url=f'{ctx.guild.me.avatar_url}')
result = await asyncio.wait_for(self.bot.loop.create_task(
run_command('git status')), 10)
results = paginate(result, maxlen=1014)
for page in results[:5]:
em.add_field(name='\uFFF0', value=f'{page}')
await ctx.send(embed=em)
def setup(bot):
bot.add_cog(Git(bot))
"""
===
MIT License
Copyright (c) 2018 Dusty.P https://github.com/dustinpianalto
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import discord
from discord.ext import commands
from src.shared_libs.utils import paginate, run_command
from src.shared_libs.loggable import Loggable
import asyncio
class Git(Loggable):
def __init__(self, bot):
self.bot = bot
@commands.group(case_insensitive=True, invoke_without_command=True)
async def git(self, ctx):
"""Run help git for more info"""
await ctx.send('https://github.com/Annihilator708/Sebi-Machine/')
@commands.command(case_insensitive=True, brief='Gets the Trello link.')
async def trello(self, ctx):
await ctx.send('<https://trello.com/b/x02goBbW/sebis-bot-tutorial-roadmap>')
@git.command()
async def pull(self, ctx):
self.logger.warning('Invoking git-pull')
await ctx.trigger_typing()
if ctx.author.id not in self.bot.ownerlist:
return await ctx.send('Only my contributors can use me like this :blush:', delete_after=10)
em = discord.Embed(style='rich',
title=f'Git Pull',
color=self.bot.embed_color)
em.set_thumbnail(url=f'{ctx.guild.me.avatar_url}')
# Pretty sure you can just do await run_command() if that is async,
# or run in a TPE otherwise.
result = await asyncio.wait_for(self.bot.loop.create_task(
run_command('git fetch --all')), 120) + '\n'
result += await asyncio.wait_for(self.bot.loop.create_task(
run_command('git reset --hard origin/$(git rev-parse '
'--symbolic-full-name --abbrev-ref HEAD)')),
120) + '\n\n'
result += await asyncio.wait_for(self.bot.loop.create_task(
run_command('git show --stat | sed "s/.*@.*[.].*/ /g"')), 10)
results = paginate(result, maxlen=1014)
for page in results[:5]:
em.add_field(name='\uFFF0', value=f'{page}')
await ctx.send(embed=em)
@git.command()
async def status(self, ctx):
await ctx.trigger_typing()
if ctx.author.id not in self.bot.ownerlist:
return await ctx.send('Only my contributors can use me like this :blush:', delete_after=10)
em = discord.Embed(style='rich',
title=f'Git Status',
color=self.bot.embed_color)
em.set_thumbnail(url=f'{ctx.guild.me.avatar_url}')
result = await asyncio.wait_for(self.bot.loop.create_task(
run_command('git status')), 10)
results = paginate(result, maxlen=1014)
for page in results[:5]:
em.add_field(name='\uFFF0', value=f'{page}')
await ctx.send(embed=em)
def setup(bot):
bot.add_cog(Git(bot))

View File

@ -1,7 +1,7 @@
{
"version": 0.1,
"display_name" : "[S!] Sebi-Machine",
"display_name" : "[ds!] Dev-Sebi",
"maintenance": "True",
"ownerlist": [],
"prefix": "S!"
}
"prefix": "ds!"
}

View File

@ -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"]

View File

@ -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