radical-bot/ext/system.py

158 lines
5.5 KiB
Python
Raw Normal View History

2022-08-19 06:17:34 +00:00
import asyncio
import lightbulb
import hikari
from lib.config import load_config
config = load_config()
2022-08-19 06:17:34 +00:00
plugin = lightbulb.Plugin("SystemPlugin")
2022-08-20 04:41:06 +00:00
2022-08-20 02:22:48 +00:00
async def create_subprocess(*args):
proc = await asyncio.create_subprocess_exec(*args,
2022-08-20 04:41:06 +00:00
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
2022-08-20 02:22:48 +00:00
stdout, _ = await proc.communicate()
return stdout.decode("utf-8").strip()
2022-08-20 04:41:06 +00:00
2022-08-20 02:22:48 +00:00
async def get_git_latest_commit_id():
return await create_subprocess("git", "rev-parse", "--short", "HEAD")
2022-08-20 04:41:06 +00:00
2022-08-20 02:22:48 +00:00
async def get_git_branch():
return await create_subprocess("git", "rev-parse", "--abbrev-ref", "HEAD")
2022-08-20 04:41:06 +00:00
2022-08-20 02:22:48 +00:00
async def get_git_remote():
2022-08-20 03:33:30 +00:00
remote = await create_subprocess("git", "branch", "-vv")
for line in remote.split("\n"):
if "*" in line:
return line.split("[")[1].split("]")[0]
2022-08-20 02:22:48 +00:00
2022-08-20 04:41:06 +00:00
2022-08-20 02:22:48 +00:00
async def get_git_head_diff_branch(branch: str) -> str:
# diff HEAD to remote
return await create_subprocess("git", "diff", "head", branch)
2022-08-20 04:41:06 +00:00
2022-08-20 02:22:48 +00:00
async def get_git_index_diff_branch(branch: str) -> str:
# diff index to remote
return await create_subprocess("git", "diff", branch)
2022-08-20 04:41:06 +00:00
2022-08-20 02:22:48 +00:00
async def get_git_commits_ahead_behind(branch: str, remote: str) -> tuple[int, int]:
# commits ahead/behind remote
output = await create_subprocess("git", "rev-list", "--left-right", "--count", branch+"..."+remote)
commits_ahead = int(output.split()[0])
commits_behind = int(output.split()[1])
return commits_ahead, commits_behind
2022-08-20 04:41:06 +00:00
2022-08-20 05:06:48 +00:00
async def get_git_update(branch, remote) -> bool:
2022-08-20 02:22:48 +00:00
commits_behind, _ = await get_git_commits_ahead_behind(branch, remote)
2022-08-20 05:06:48 +00:00
if commits_behind > 0:
2022-08-20 02:22:48 +00:00
return True
return False
2022-08-20 04:41:06 +00:00
2022-08-19 06:17:34 +00:00
async def get_git_status() -> dict:
output = {}
2022-08-20 02:22:48 +00:00
output["commit_id"] = await get_git_latest_commit_id()
output["branch"] = await get_git_branch()
output["remote"] = await get_git_remote()
2022-08-20 05:06:48 +00:00
output['update'] = await get_git_update(output['branch'], output['remote'])
2022-08-19 06:17:34 +00:00
return output
2022-08-20 04:41:06 +00:00
2022-08-19 06:17:34 +00:00
@plugin.command
2022-08-20 02:22:48 +00:00
@lightbulb.command("ping", "pong!", ephemeral=True)
2022-08-19 06:17:34 +00:00
@lightbulb.implements(lightbulb.SlashCommand)
async def ping(ctx: lightbulb.Context) -> None:
2022-08-20 02:22:48 +00:00
embed = hikari.Embed(title="Pong!",
2022-08-20 04:41:06 +00:00
description=f"latency: {round(ctx.bot.heartbeat_latency * 1000, ndigits=2)}ms"
)
2022-08-19 06:17:34 +00:00
await ctx.respond(embed)
2022-08-20 04:41:06 +00:00
@plugin.command
@lightbulb.add_checks(lightbulb.owner_only)
@lightbulb.command("update", "update the bot!", ephemeral=True)
@lightbulb.implements(lightbulb.SlashCommand)
async def update(ctx: lightbulb.Context) -> None:
2022-08-20 05:04:19 +00:00
await create_subprocess("git", "pull")
embed = hikari.Embed(title="Restarting",
2022-08-20 04:41:06 +00:00
description="Restarting to load an update!"
)
await ctx.respond(embed)
await ctx.bot.close()
2022-08-20 04:41:06 +00:00
@plugin.command
@lightbulb.add_checks(lightbulb.owner_only)
2022-08-20 05:00:11 +00:00
@lightbulb.command("branch", "get or set the working branch the bot uses.")
2022-08-20 04:41:06 +00:00
@lightbulb.implements(lightbulb.SlashCommandGroup)
2022-08-20 05:13:28 +00:00
async def branch_group(ctx: lightbulb.Context) -> None:
2022-08-20 05:00:11 +00:00
pass # SlashCommandGroup body code isn't run
2022-08-20 04:57:18 +00:00
2022-08-20 05:13:28 +00:00
@branch_group.child
2022-08-20 05:00:11 +00:00
@lightbulb.add_checks(lightbulb.owner_only)
@lightbulb.command("get", "get the current branch", ephemeral=True)
2022-08-20 04:57:18 +00:00
@lightbulb.implements(lightbulb.SlashSubCommand)
async def branch_get(ctx: lightbulb.Context) -> None:
2022-08-20 04:41:06 +00:00
output = await create_subprocess("git", "rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD")
embed = hikari.Embed(title="Current Branch",
description=f"Currently on branch '{output}'")
await ctx.respond(embed=embed)
2022-08-20 05:13:28 +00:00
@branch_group.child
2022-08-20 05:00:11 +00:00
@lightbulb.add_checks(lightbulb.owner_only)
2022-08-20 04:41:06 +00:00
@lightbulb.option("name", "name of the branch", type=str, required=True)
2022-08-20 05:00:11 +00:00
@lightbulb.command("switch", "switch branches", ephemeral=True)
2022-08-20 04:57:18 +00:00
@lightbulb.implements(lightbulb.SlashSubCommand)
async def branch_switch(ctx: lightbulb.Context) -> None:
2022-08-20 04:41:06 +00:00
embed = hikari.Embed(title="Restarting",
description="Restarting to switch branches!"
)
2022-08-20 05:13:28 +00:00
output = await create_subprocess("git", "switch", ctx.options.name)
2022-08-20 04:41:06 +00:00
if "invalid reference" in output: # parsing output allows us more specificity.
embed.title = "Branch does not exist."
embed.description = "Please check your spelling."
await ctx.respond(embed)
return
elif "Already on" in output:
embed.title = f"Already on branch '{ctx.options.branch}'"
embed.description = "Just double checking, I presume?"
await ctx.respond(embed)
return
await ctx.respond(embed)
await ctx.bot.close()
2022-08-19 06:17:34 +00:00
@plugin.command
2022-08-20 02:22:48 +00:00
@lightbulb.command("info", "get bot information such as the version, and repository link.", ephemeral=True)
2022-08-19 06:17:34 +00:00
@lightbulb.implements(lightbulb.SlashCommand)
async def info(ctx: lightbulb.Context) -> None:
2022-08-20 05:22:42 +00:00
await create_subprocess("git", "fetch") # updates with remote
2022-08-19 06:17:34 +00:00
git_status = await get_git_status()
embed = hikari.Embed(title="About Me!")
2022-08-20 02:21:34 +00:00
embed.add_field("Git Repository", config.git_url)
2022-08-19 06:29:57 +00:00
embed.add_field("Version", git_status['commit_id'], inline=True)
embed.add_field("Branch", git_status['branch'], inline=True)
2022-08-20 02:21:34 +00:00
embed.add_field("Remote", git_status['remote'], inline=True)
embed.add_field("Needs Update?", git_status['update'])
2022-08-19 06:17:34 +00:00
await ctx.respond(embed)
2022-08-20 04:41:06 +00:00
2022-08-19 06:17:34 +00:00
def load(bot: lightbulb.BotApp):
bot.add_plugin(plugin)
2022-08-20 04:41:06 +00:00
2022-08-19 06:17:34 +00:00
def unload(bot: lightbulb.BotApp):
bot.remove_plugin(plugin)