async def add(self, ctx, server): """Upload a server's JSON file to the bot from an attachment.""" if len(ctx.message.attachments) == 0: embed = discord.Embed(color=ebed.randomrgb()) embed.description = "Please attach a JSON file to add." await ctx.send(embed=embed) else: for file in ctx.message.attachments: savefile = getserverjson(server) await common.download_file(file.url, savefile) embed = discord.Embed(color=ebed.randomrgb()) embed.description = "The file '{}.json' was added to the server list." await ctx.send(embed=embed)
async def replace(self, ctx, server): """Replace a server's JSON data with new data from an attachment.""" if len(ctx.message.attachments) == 0: embed = discord.Embed(color=ebed.randomrgb()) embed.description = "Please attach a file to replace '{}.json'".format( server) await ctx.send(embed=embed) else: for file in ctx.message.attachments: savefile = getserverjson(server) await common.download_file(file.url, savefile) embed = discord.Embed(color=ebed.randomrgb()) embed.description = "The file '{}.json' was overwritten with new data.".format( server) await ctx.send(embed=embed)
async def restart(self, ctx): """Restart the bot.""" print("Running restart") embed = discord.Embed(color=ebed.randomrgb()) embed.description = "Be right back!" await ctx.send(embed=embed) os.execl(sys.executable, sys.executable, *sys.argv)
async def status(self, ctx): """Get system status.""" color = ebed.randomrgb() embed = discord.Embed(title="System Status", timestamp=self.sys_status["UPDATE"], color=color, description="Updated every minute.") embed.add_field(name="RAM", value="{}%".format(self.sys_status["RAM"])) embed.add_field(name="CPU", value="{}%".format(self.sys_status["CPU"])) embed.add_field(name="DISK", value="{}%".format(self.sys_status["DISK"])) if not math.isnan(self.sys_status['LATENCY'] ): # if called before this has a value. embed.add_field(name="PING", value="{}ms".format( round(self.sys_status["LATENCY"] * 1000))) if self.platform == 'linux': # platform specific feature embed.add_field(name="Temperature", value="{}°C/{}°F".format(self.sys_status["TEMPC"], self.sys_status["TEMPF"])) embed.add_field(name="Boot Time", value=self.sys_status["BOOT"], inline=False) embed.add_field(name="IP Address", value=self.sys_status["IP"], inline=False) embed.set_footer(text=ebed.rgb_to_hex(color.to_rgb())) await ctx.send(embed=embed)
async def user(self, ctx, github_profile): """Look up bot contributions for the given Github User.""" embed = discord.Embed(color=ebed.randomrgb()) repo = self.g.get_repo("InValidFire/Pi-Controller") try: user = self.g.get_user(github_profile) commits = repo.get_commits(author=user) if commits.totalCount == 0: embed.description = "No contributions found for user '{}'".format( github_profile) else: for i, commit in enumerate(commits): gc = repo.get_git_commit(commit.sha) embed.add_field(name=gc.message.split("\n")[0], value="[{sha}]({url})".format( sha=gc.sha, url=gc.html_url), inline=False) embed.title = "Last {} commits for user: {}".format( i + 1, github_profile) if i == 9: break except github.UnknownObjectException: embed.description = "The Github user '{}' could not be found.".format( github_profile) finally: await ctx.send(embed=embed)
async def getbotdir(self, ctx): """Get bot directory information.""" embed = discord.Embed(color=ebed.randomrgb()) embed.add_field(name="Bot Dir", value=common.getbotdir()) embed.add_field(name="Arg 0", value="name: {}".format(str(sys.argv[0]))) embed.add_field(name="Folder", value="name: {}".format( str(os.path.dirname(sys.argv[0])))) embed.add_field(name="Full Path", value=os.path.realpath(os.path.dirname(sys.argv[0]))) await ctx.send(embed=embed)
async def link(self, ctx, user: discord.User, github_profile): """Link a Discord user to a Github Profile for use with other commands.""" data = await common.loadjson( os.path.join(common.getbotdir(), "data", "data.json")) data['contributors'][user.id] = {} data['contributors'][user.id]['github'] = github_profile await common.dumpjson( data, os.path.join(common.getbotdir(), "data", "data.json")) embed = discord.Embed(color=ebed.randomrgb()) embed.description = "{} has been linked to the Github Profile '{}'".format( user.mention, github_profile) await ctx.send(embed=embed)
async def remove(self, ctx, user: discord.User): """Revoke admin permissions.""" data = await common.loadjson("data/data.json") embed = discord.Embed(color=ebed.randomrgb()) if user.id in data['admins']: data['admins'].pop(data['admins'].index(user.id)) await common.dumpjson( data, os.path.join(common.getbotdir(), "data/data.json")) embed.description = "{} is no longer a bot admin.".format( user.mention) else: embed.description = "{} is not a bot admin.".format(user.mention) await ctx.send(embed=embed)
async def load_embed(meta: dict) -> discord.Embed: """Common embed builder used to create embeds used with server messages.""" if "embed_color" in meta: # load color if isinstance(meta['embed_color'], str): embed = discord.Embed(color=ebed.hex_to_rgb(meta['embed_color'])) elif isinstance(meta['embed_color'], list): # TODO: Add error handling for JSON format. embed = discord.Embed(color=ebed.randomrgb( colorlist=meta['embed_color'])) else: embed = discord.Embed(color=ebed.randomrgb()) else: embed = discord.Embed(color=ebed.randomrgb()) if "desc" in meta and "icon" in meta: # load footer embed.set_footer(icon_url=meta['icon'], text="{name} - {desc}".format(name=meta['name'], desc=meta['desc'])) elif "desc" in meta: embed.set_footer(text="{name} - {desc}".format(name=meta['name'], desc=meta['desc'])) elif "icon" in meta: embed.set_footer(icon_url=meta['icon'], text=meta['name']) return embed
async def contributors(self, ctx): if ctx.invoked_subcommand is None: repo = self.g.get_repo("InValidFire/Pi-Controller") contributors = repo.get_contributors() embed = discord.Embed(color=ebed.randomrgb()) for contributor in contributors: latest_commit = None for commit in repo.get_commits(author=contributor): latest_commit = commit break embed.add_field( name="{}'s latest contribution".format(contributor.login), value="[{sha}]({url})".format(sha=latest_commit.sha, url=latest_commit.html_url)) await ctx.send(embed=embed)
async def list(self, ctx): """List all servers available to launch.""" color = ebed.randomrgb() embed = discord.Embed(title="Servers Listed", color=color) count = 0 msg = "" for file in os.listdir(os.path.join(common.getbotdir(), "data", "json")): count += 1 msg += "\n**-** {}".format(os.path.splitext(file)[0]) embed.add_field(name="{} available".format(count), value=msg, inline=False) embed.set_footer(text=ebed.rgb_to_hex(color.to_rgb())) await ctx.send(embed=embed)
async def run(self, ctx, command): """Run a command from the server's JSON file""" if self.server_data is None: embed = discord.Embed(color=ebed.randomrgb()) embed.description = "No server running." await ctx.send(embed=embed) else: embed = await load_embed(self.server_data['meta']) if command == "start" or command == "setup": embed.description = "System command. Unable to run through this method." await ctx.send(embed=embed) elif command in self.server_data['commands']: embed.description = "Running command: {}".format(command) await ctx.send(embed=embed) await self.run_command(command) else: embed.description = "No command '{command}' found.".format( command=command) await ctx.send(embed=embed)
async def list(self, ctx): """List all admins.""" data = await common.loadjson("data/data.json") count = 0 msg = "" for admin in data['admins']: try: user = self.bot.get_user(admin) msg += "\n{}".format(user.mention) count += 1 except commands.BadArgument: continue if count == 1: admin_count = "1 admin" else: admin_count = "{} admins".format(count) color = ebed.randomrgb() embed = discord.Embed(title="Bot Admins", color=color) embed.add_field(name=admin_count, value=msg) embed.set_footer(text=ebed.rgb_to_hex(color.to_rgb())) await ctx.send(embed=embed)
async def shutdown(self, ctx): """Shut down the bot.""" embed = discord.Embed(color=ebed.randomrgb()) embed.description = "Goodbye!" await ctx.send(embed=embed) sys.exit()
async def github(self, ctx): embed = discord.Embed(color=ebed.randomrgb()) embed.description = "[Bot Github Link!](https://github.com/InValidFire/Pi-Controller)" await ctx.send(embed=embed)