Skip to content

Commit

Permalink
Transition to using YAML for configs (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorialis authored Jan 17, 2018
1 parent 013130e commit 5a0f22f
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 182 deletions.
5 changes: 2 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
name: run tests
command: |
. .venv/bin/activate
cp .circleci/queuebot_config.py config.py
cp .circleci/queuebot_config.yaml config.yaml
export PYTHONPATH=$PYTHONPATH:$(pwd)
pytest -vs
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:
name: run tests
command: |
. .venv/bin/activate
cp .circleci/queuebot_config.py config.py
cp .circleci/queuebot_config.yaml config.yaml
export PYTHONPATH=$PYTHONPATH:$(pwd)
pytest -vs
Expand Down Expand Up @@ -214,5 +214,4 @@ workflows:
build:
jobs:
- "build and install 3.6"
- "build and install 3.7"
- "flake8"
37 changes: 0 additions & 37 deletions .circleci/queuebot_config.py

This file was deleted.

8 changes: 8 additions & 0 deletions .circleci/queuebot_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
token: ""

pg_credentials:
host: localhost
port: 5432
user: "qbotsql"
database: "qbottest"
timeout: 60
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ exclude = .git,
__pycache__,
config.example.py,
config.py,
config.example.yaml,
config.yaml,
.env,
.venv,
env,
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# QueueBot
queuebot.log
config.py
config.yaml
/data

# IDEA
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ addons:
before_script:
- psql -c 'create database travis_testdb;' -U postgres
- psql travis_testdb < schema.sql
- cp tests/travis_config.py config.py
- cp tests/travis_config.yaml config.yaml
- "export PYTHONPATH=$PYTHONPATH:$(pwd)"
install: "pip install -r requirements.txt"
script: pytest -vs
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This project has a number of requirements for deployment:

- ``git``, for acquiring ``discord.py@rewrite``
- A PostgreSQL >=9.6 server to store suggestion data
- A ``config.py`` file containing configuration data
- A ``config.yaml`` file containing configuration data
- ``libuv`` to enable ``uvloop``
- Python requirements as in `requirements.txt <https://github.com/slice/queuebot/blob/master/requirements.txt>`__

Expand Down Expand Up @@ -140,12 +140,12 @@ In Linux you can do this quickly by doing ``psql -d mydb -U myuser < schema.sql`

Your setup for PostgreSQL is now done and you can log out of psql by typing ``\q``.

config.py
#########
config.yaml
###########

A ``config.py`` file should be placed in the project root, alongside ``run.py``.
A ``config.yaml`` file should be placed in the project root, alongside ``run.py``.

You can find an example of how to create this config by referencing `config.example.py <https://github.com/BlobEmoji/queuebot/blob/master/config.example.py>`__.
You can find an example of how to create this config by referencing `config.example.yaml <https://github.com/BlobEmoji/queuebot/blob/master/config.example.yaml>`__.

libuv
#####
Expand Down
36 changes: 0 additions & 36 deletions config.example.py

This file was deleted.

58 changes: 58 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

token: 'token'

pg_credentials:
host: localhost
port: 5432
user: 'myuser'
database: 'mydb'
password: 'mypassword'
timeout: 60


# IDs of users who should have admin tool access (eval, reload, etc)
admins:
- 1234567890
- 9876543210


# Roles which possess extra authority over this bot (Blob Police, etc)
authority_roles:
- 1234567890
- 9876543210


# List of roles considered council (Blob Council, Council Lite, etc)
council_roles:
- 1234567890
- 9876543210


# ID of guilds in which the bot should update the emoji list
blob_guilds:
- 1234567890
- 9876543210


# Emoji info
approve_emoji_id: 1234567890
deny_emoji_id: 9876543210

approve_emoji: 'green_tick:1234567890'
deny_emoji: 'red_tick:9876543210'


# Channel IDs
bot_log: 1234567890

suggestions_channel: 1234567890
council_queue: 1234567890
approval_queue: 1234567890

suggestions_log: 1234567890
council_changelog: 1234567890


# Voting parameters
required_difference: 5 # Majority required for this blob to move into the next stage
required_votes: 15 # Minimum amount of total votes before moving to next stage
9 changes: 5 additions & 4 deletions queuebot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import discord
from discord.ext import commands

import config
from queuebot.cog import Cog

logger = logging.getLogger(__name__)
Expand All @@ -31,6 +30,8 @@ def __init__(self, *args, **kwargs):
#: List of extension names to load. We store this because `self.extensions` is volatile during reload.
self.to_load: typing.List[str] = None

self.config = kwargs.pop('config')

# Database connection to PostgreSQL
self.db: Pool = kwargs.pop('db')

Expand All @@ -50,18 +51,18 @@ async def on_ready(self):

async def log(self, content, **kwargs) -> typing.Union[discord.Message, None]:
timestamp = f'`[{datetime.datetime.utcnow().strftime("%H:%M")}]`'
channel = self.get_channel(config.bot_log)
channel = self.get_channel(self.config.bot_log)
if not channel:
return None
return await channel.send(f'{timestamp} {content}', **kwargs)

@property
def admins(self):
return set([self.owner.id] + getattr(config, 'admins', []))
return set([self.owner.id] + self.config.get('admins', []))

@property
def council_roles(self):
return set(getattr(config, 'council_roles', []))
return set(self.config.get('council_roles', []))

async def on_message(self, msg: discord.Message):
# Ignore messages from bots.
Expand Down
3 changes: 1 addition & 2 deletions queuebot/checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import config
from discord.ext import commands


Expand All @@ -14,7 +13,7 @@ def predicate(ctx: commands.Context) -> bool:
if not ctx.guild:
return False

return any(role.id in config.authority_roles for role in ctx.author.roles)
return any(role.id in ctx.bot.config.authority_roles for role in ctx.author.roles)
return commands.check(predicate)


Expand Down
1 change: 1 addition & 0 deletions queuebot/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
class Cog:
def __init__(self, bot: 'queuebot.bot.Queuebot'):
self.bot = bot
self.config = bot.config
self.db = bot.db
3 changes: 1 addition & 2 deletions queuebot/cogs/emoji_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import discord

from config import blob_guilds
from queuebot.cog import Cog


Expand Down Expand Up @@ -31,7 +30,7 @@ def format_emoji_list(guild: discord.Guild) -> List[str]:

class EmojiList(Cog):
async def on_guild_emojis_update(self, guild: discord.Guild, *_):
if guild.id not in blob_guilds:
if guild.id not in self.config.blob_guilds:
return

formatted = format_emoji_list(guild)
Expand Down
Loading

0 comments on commit 5a0f22f

Please sign in to comment.