Pylint Compliance

- Modifies codebase to match conventions set by Pylint.
- Added update command to template.
This commit is contained in:
InValidFire 2021-03-30 18:42:07 -04:00
parent f928c6df20
commit 58ca231454
5 changed files with 54 additions and 30 deletions

View File

@ -7,6 +7,7 @@ import yaml
def read_logging_config(default_path="logging.yaml", env_key="LOG_CFG"):
"""Load the logging config into memory"""
path = default_path
value = os.getenv(env_key, None)
if value:
@ -15,11 +16,11 @@ def read_logging_config(default_path="logging.yaml", env_key="LOG_CFG"):
with open(path, "rt") as f:
logging_config = yaml.safe_load(f.read())
return logging_config
else:
return None
return None
def setup_logging(logging_config, default_level=logging.INFO):
"""Configure logging"""
if logging_config:
logging.config.dictConfig(logging_config)
else:

13
bot.py
View File

@ -1,9 +1,12 @@
from applog.utils import read_logging_config, setup_logging
"""Bot startup code"""
import logging
from discord.ext import commands
import core.common as common
from pathlib import Path
from discord.ext import commands
from applog.utils import read_logging_config, setup_logging
import core.common as common
logger = logging.getLogger(__name__)
discord_logger = logging.getLogger('discord')
@ -13,14 +16,14 @@ setup_logging(log_config_dict)
common.prompt_config("config.json", "Enter repository URL: ", "repo_link")
common.prompt_config("config.json", "Enter bot token: ", "token")
common.prompt_config("config.json", "Enter bot prefix: ", "prefix")
common.prompt_config("config.json", "Enter ServerAPI token: ", "api_token")
config, _ = common.load_config("config.json")
bot = commands.Bot(command_prefix=config['prefix'])
def get_extensions(): # Gets extension list dynamically
def get_extensions():
"""Gets extension list dynamically"""
extensions = []
for file in Path("cogs").glob("**/*.py"):
extensions.append(str(file).replace("/", ".").replace(".py", ""))

View File

@ -1,27 +1,33 @@
from discord.ext import commands
import discord
import core.common as common
"""System Module from Bot Template"""
import sys
import subprocess
import logging
from discord.ext import commands
import discord
import core.common as common
logger = logging.getLogger(__name__)
class System(commands.Cog):
"""System Cog from Bot Template"""
def __init__(self, bot):
self.bot = bot
self.commit = subprocess.run(["git", "show", "-s", "--format=%h"],
capture_output=True,
encoding="utf-8").stdout.strip()
encoding="utf-8", check=True).stdout.strip()
@commands.group()
async def bot(self, ctx):
async def bot_group(self, ctx):
"""Command group for core bot commands"""
if ctx.invoked_subcommand is None:
await ctx.send('No subcommand invoked.')
@bot.command()
@bot_group.command()
async def ping(self, ctx):
"""Ping the bot"""
await ctx.message.delete()
embed = discord.Embed(title="Pong!",
description=str(round(self.bot.latency * 1000, 1)) + "ms",
@ -30,9 +36,10 @@ class System(commands.Cog):
icon_url=ctx.author.avatar_url)
await ctx.send(embed=embed)
@bot.command()
@bot_group.command()
@commands.is_owner()
async def stop(self, ctx):
"""Stop the bot"""
await ctx.message.delete()
embed = discord.Embed(title="Stopping Bot!",
color=common.random_rgb())
@ -41,9 +48,10 @@ class System(commands.Cog):
await ctx.send(embed=embed)
sys.exit()
@bot.command()
@bot_group.command()
@commands.is_owner()
async def restart(self, ctx):
"""Restart the bot"""
await ctx.message.delete()
embed = discord.Embed(title="Restarting Bot!",
color=common.random_rgb())
@ -52,31 +60,28 @@ class System(commands.Cog):
await ctx.send(embed=embed)
sys.exit(26)
@commands.group()
async def git(self, ctx):
if ctx.invoked_subcommand is None:
await ctx.send('No subcommand invoked.')
@git.command()
@bot_group.command()
async def version(self, ctx):
"""Get bot version"""
await ctx.message.delete()
d = subprocess.run(["git", "show", "-s", "--format=%ci", self.commit],
capture_output=True,
encoding="utf-8")
m = subprocess.run(['git', 'show', '-s', '--format=%B', self.commit],
capture_output=True,
encoding="utf-8")
commit_date = subprocess.run(["git", "show", "-s", "--format=%ci", self.commit],
capture_output=True, check=True,
encoding="utf-8").stdout.strip()
commit_msg = subprocess.run(['git', 'show', '-s', '--format=%B', self.commit],
capture_output=True, check=True,
encoding="utf-8").stdout.strip()
embed = discord.Embed(title="Bot Version",
description="Current Bot Version",
color=common.random_rgb(self.commit))
embed.set_footer(text=f"requested by {ctx.author}", icon_url=ctx.author.avatar_url)
embed.add_field(name="ID", value=self.commit, inline=False)
embed.add_field(name="Date", value=d.stdout, inline=False)
embed.add_field(name="Changelog", value=m.stdout, inline=False)
embed.add_field(name="Date", value=commit_date, inline=False)
embed.add_field(name="Changelog", value=commit_msg, inline=False)
await ctx.send(embed=embed)
@git.command()
@bot_group.command()
async def repo(self, ctx):
"""Display the bot repository"""
await ctx.message.delete()
embed = discord.Embed(title="Code Repository",
description="You can find my source code on [GitDab]("
@ -85,6 +90,19 @@ class System(commands.Cog):
embed.set_footer(text=f"requested by {ctx.author}", icon_url=ctx.author.avatar_url)
await ctx.send(embed=embed)
@bot_group.command()
@commands.is_owner()
async def update(self, ctx):
"""Update the bot"""
ctx: commands.Context
await ctx.message.delete()
response = subprocess.run(["git", "pull"], capture_output=True, check=True,
encoding="utf-8").stdout.strip()
embed=discord.Embed(title="Update Report", description=response, color=common.random_rgb())
embed.set_footer(text=f"requested by {ctx.author}", icon_url=ctx.author.avatar_url)
await ctx.send(embed=embed)
def setup(bot):
"""Initialize the cog"""
bot.add_cog(System(bot))

0
core/__init__.py Normal file
View File

View File

@ -1,4 +1,5 @@
#!/usr/bin/env/python
"""Main process for Bot program"""
import subprocess
import logging
import sys
@ -14,6 +15,7 @@ logger.info("Bot Manager Started!")
def start_bot():
"""Start the bot process"""
bot_process = subprocess.Popen(["python3", "-B", "bot.py"], stdout=sys.stdout)
return bot_process