Pylint Compliance
- Modifies codebase to match conventions set by Pylint. - Added update command to template.
This commit is contained in:
parent
f928c6df20
commit
58ca231454
5 changed files with 54 additions and 30 deletions
|
@ -7,6 +7,7 @@ import yaml
|
||||||
|
|
||||||
|
|
||||||
def read_logging_config(default_path="logging.yaml", env_key="LOG_CFG"):
|
def read_logging_config(default_path="logging.yaml", env_key="LOG_CFG"):
|
||||||
|
"""Load the logging config into memory"""
|
||||||
path = default_path
|
path = default_path
|
||||||
value = os.getenv(env_key, None)
|
value = os.getenv(env_key, None)
|
||||||
if value:
|
if value:
|
||||||
|
@ -15,11 +16,11 @@ def read_logging_config(default_path="logging.yaml", env_key="LOG_CFG"):
|
||||||
with open(path, "rt") as f:
|
with open(path, "rt") as f:
|
||||||
logging_config = yaml.safe_load(f.read())
|
logging_config = yaml.safe_load(f.read())
|
||||||
return logging_config
|
return logging_config
|
||||||
else:
|
return None
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def setup_logging(logging_config, default_level=logging.INFO):
|
def setup_logging(logging_config, default_level=logging.INFO):
|
||||||
|
"""Configure logging"""
|
||||||
if logging_config:
|
if logging_config:
|
||||||
logging.config.dictConfig(logging_config)
|
logging.config.dictConfig(logging_config)
|
||||||
else:
|
else:
|
||||||
|
|
13
bot.py
13
bot.py
|
@ -1,9 +1,12 @@
|
||||||
from applog.utils import read_logging_config, setup_logging
|
"""Bot startup code"""
|
||||||
import logging
|
import logging
|
||||||
from discord.ext import commands
|
|
||||||
import core.common as common
|
|
||||||
from pathlib import Path
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
discord_logger = logging.getLogger('discord')
|
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 repository URL: ", "repo_link")
|
||||||
common.prompt_config("config.json", "Enter bot token: ", "token")
|
common.prompt_config("config.json", "Enter bot token: ", "token")
|
||||||
common.prompt_config("config.json", "Enter bot prefix: ", "prefix")
|
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")
|
config, _ = common.load_config("config.json")
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix=config['prefix'])
|
bot = commands.Bot(command_prefix=config['prefix'])
|
||||||
|
|
||||||
|
|
||||||
def get_extensions(): # Gets extension list dynamically
|
def get_extensions():
|
||||||
|
"""Gets extension list dynamically"""
|
||||||
extensions = []
|
extensions = []
|
||||||
for file in Path("cogs").glob("**/*.py"):
|
for file in Path("cogs").glob("**/*.py"):
|
||||||
extensions.append(str(file).replace("/", ".").replace(".py", ""))
|
extensions.append(str(file).replace("/", ".").replace(".py", ""))
|
||||||
|
|
|
@ -1,27 +1,33 @@
|
||||||
from discord.ext import commands
|
"""System Module from Bot Template"""
|
||||||
import discord
|
|
||||||
import core.common as common
|
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from discord.ext import commands
|
||||||
|
import discord
|
||||||
|
|
||||||
|
import core.common as common
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class System(commands.Cog):
|
class System(commands.Cog):
|
||||||
|
"""System Cog from Bot Template"""
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.commit = subprocess.run(["git", "show", "-s", "--format=%h"],
|
self.commit = subprocess.run(["git", "show", "-s", "--format=%h"],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
encoding="utf-8").stdout.strip()
|
encoding="utf-8", check=True).stdout.strip()
|
||||||
|
|
||||||
@commands.group()
|
@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:
|
if ctx.invoked_subcommand is None:
|
||||||
await ctx.send('No subcommand invoked.')
|
await ctx.send('No subcommand invoked.')
|
||||||
|
|
||||||
@bot.command()
|
@bot_group.command()
|
||||||
async def ping(self, ctx):
|
async def ping(self, ctx):
|
||||||
|
"""Ping the bot"""
|
||||||
await ctx.message.delete()
|
await ctx.message.delete()
|
||||||
embed = discord.Embed(title="Pong!",
|
embed = discord.Embed(title="Pong!",
|
||||||
description=str(round(self.bot.latency * 1000, 1)) + "ms",
|
description=str(round(self.bot.latency * 1000, 1)) + "ms",
|
||||||
|
@ -30,9 +36,10 @@ class System(commands.Cog):
|
||||||
icon_url=ctx.author.avatar_url)
|
icon_url=ctx.author.avatar_url)
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
@bot.command()
|
@bot_group.command()
|
||||||
@commands.is_owner()
|
@commands.is_owner()
|
||||||
async def stop(self, ctx):
|
async def stop(self, ctx):
|
||||||
|
"""Stop the bot"""
|
||||||
await ctx.message.delete()
|
await ctx.message.delete()
|
||||||
embed = discord.Embed(title="Stopping Bot!",
|
embed = discord.Embed(title="Stopping Bot!",
|
||||||
color=common.random_rgb())
|
color=common.random_rgb())
|
||||||
|
@ -41,9 +48,10 @@ class System(commands.Cog):
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
@bot.command()
|
@bot_group.command()
|
||||||
@commands.is_owner()
|
@commands.is_owner()
|
||||||
async def restart(self, ctx):
|
async def restart(self, ctx):
|
||||||
|
"""Restart the bot"""
|
||||||
await ctx.message.delete()
|
await ctx.message.delete()
|
||||||
embed = discord.Embed(title="Restarting Bot!",
|
embed = discord.Embed(title="Restarting Bot!",
|
||||||
color=common.random_rgb())
|
color=common.random_rgb())
|
||||||
|
@ -52,31 +60,28 @@ class System(commands.Cog):
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
sys.exit(26)
|
sys.exit(26)
|
||||||
|
|
||||||
@commands.group()
|
@bot_group.command()
|
||||||
async def git(self, ctx):
|
|
||||||
if ctx.invoked_subcommand is None:
|
|
||||||
await ctx.send('No subcommand invoked.')
|
|
||||||
|
|
||||||
@git.command()
|
|
||||||
async def version(self, ctx):
|
async def version(self, ctx):
|
||||||
|
"""Get bot version"""
|
||||||
await ctx.message.delete()
|
await ctx.message.delete()
|
||||||
d = subprocess.run(["git", "show", "-s", "--format=%ci", self.commit],
|
commit_date = subprocess.run(["git", "show", "-s", "--format=%ci", self.commit],
|
||||||
capture_output=True,
|
capture_output=True, check=True,
|
||||||
encoding="utf-8")
|
encoding="utf-8").stdout.strip()
|
||||||
m = subprocess.run(['git', 'show', '-s', '--format=%B', self.commit],
|
commit_msg = subprocess.run(['git', 'show', '-s', '--format=%B', self.commit],
|
||||||
capture_output=True,
|
capture_output=True, check=True,
|
||||||
encoding="utf-8")
|
encoding="utf-8").stdout.strip()
|
||||||
embed = discord.Embed(title="Bot Version",
|
embed = discord.Embed(title="Bot Version",
|
||||||
description="Current Bot Version",
|
description="Current Bot Version",
|
||||||
color=common.random_rgb(self.commit))
|
color=common.random_rgb(self.commit))
|
||||||
embed.set_footer(text=f"requested by {ctx.author}", icon_url=ctx.author.avatar_url)
|
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="ID", value=self.commit, inline=False)
|
||||||
embed.add_field(name="Date", value=d.stdout, inline=False)
|
embed.add_field(name="Date", value=commit_date, inline=False)
|
||||||
embed.add_field(name="Changelog", value=m.stdout, inline=False)
|
embed.add_field(name="Changelog", value=commit_msg, inline=False)
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
@git.command()
|
@bot_group.command()
|
||||||
async def repo(self, ctx):
|
async def repo(self, ctx):
|
||||||
|
"""Display the bot repository"""
|
||||||
await ctx.message.delete()
|
await ctx.message.delete()
|
||||||
embed = discord.Embed(title="Code Repository",
|
embed = discord.Embed(title="Code Repository",
|
||||||
description="You can find my source code on [GitDab]("
|
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)
|
embed.set_footer(text=f"requested by {ctx.author}", icon_url=ctx.author.avatar_url)
|
||||||
await ctx.send(embed=embed)
|
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):
|
def setup(bot):
|
||||||
|
"""Initialize the cog"""
|
||||||
bot.add_cog(System(bot))
|
bot.add_cog(System(bot))
|
||||||
|
|
0
core/__init__.py
Normal file
0
core/__init__.py
Normal file
2
main.py
2
main.py
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/env/python
|
#!/usr/bin/env/python
|
||||||
|
"""Main process for Bot program"""
|
||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
@ -14,6 +15,7 @@ logger.info("Bot Manager Started!")
|
||||||
|
|
||||||
|
|
||||||
def start_bot():
|
def start_bot():
|
||||||
|
"""Start the bot process"""
|
||||||
bot_process = subprocess.Popen(["python3", "-B", "bot.py"], stdout=sys.stdout)
|
bot_process = subprocess.Popen(["python3", "-B", "bot.py"], stdout=sys.stdout)
|
||||||
return bot_process
|
return bot_process
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue