You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.9KB

  1. import os
  2. import sys
  3. import aiohttp
  4. import asyncio
  5. import checks
  6. import load_config
  7. from config.server_config import ServerConfig
  8. import discord
  9. from discord.ext.commands import bot
  10. from discord.ext.commands import group
  11. class Settings():
  12. """
  13. Settings is a mix of settings and admin stuff for the bot.
  14. """
  15. def __init__(self, Bot):
  16. self.bot = Bot
  17. self.con = ServerConfig()
  18. self.servers = self.con.servers
  19. @bot.command(pass_context=True, hidden=True)
  20. @checks.is_bot_owner()
  21. async def blacklist(self, ctx, option, *args):
  22. """
  23. Usage:
  24. .blacklist [ + | - | add | remove ] @UserName [@UserName2 ...]
  25. Add or remove users to the blacklist.
  26. Blacklisted users are forbidden from using bot commands.
  27. Only the bot owner can use this command
  28. """
  29. blacklist_amount = 0
  30. mentions = ctx.message.mentions
  31. if not mentions:
  32. return await self.bot.say("You didn't mention anyone")
  33. if option not in ['+', '-', 'add', 'remove']:
  34. return await self.bot.say('Invalid option "%s" specified, use +, -, add, or remove' % option, expire_in=20)
  35. for user in mentions:
  36. if user.id == load_config.owner:
  37. print("[Commands:Blacklist] The owner cannot be blacklisted.")
  38. await self.bot.say("The owner cannot be blacklisted.")
  39. mentions.remove(user)
  40. if option in ['+', 'add']:
  41. with open("config/blacklist.txt", "r") as fp:
  42. for user in mentions:
  43. for line in fp.readlines():
  44. if user.id + "\n" in line:
  45. mentions.remove(user)
  46. with open("config/blacklist.txt", "a+") as fp:
  47. lines = fp.readlines()
  48. for user in mentions:
  49. if user.id not in lines:
  50. fp.write("{}\n".format(user.id))
  51. blacklist_amount += 1
  52. return await self.bot.say('{} user(s) have been added to the blacklist'.format(blacklist_amount))
  53. elif option in ['-', 'remove']:
  54. with open("config/blacklist.txt", "r") as fp:
  55. lines = fp.readlines()
  56. with open("config/blacklist.txt", "w") as fp:
  57. for user in mentions:
  58. for line in lines:
  59. if user.id + "\n" != line:
  60. fp.write(line)
  61. else:
  62. fp.write("")
  63. blacklist_amount += 1
  64. return await self.bot.say('{} user(s) have been removed from the blacklist'.format(blacklist_amount))
  65. @bot.command(pass_context=True, hidden=True)
  66. @checks.is_bot_owner()
  67. async def enablesetting(self, ctx, setting):
  68. self.serverconfig = self.con.load_config()
  69. server_id = ctx.message.server.id
  70. if setting in self.serverconfig[server_id]:
  71. if not self.serverconfig[server_id][setting]["enabled"]:
  72. self.serverconfig[server_id][setting]["enabled"] = 1
  73. self.con.update_config(self.serverconfig)
  74. return await self.bot.say("'{}' was enabled!".format(setting))
  75. else:
  76. self.serverconfig[server_id][setting]["enabled"] = 0
  77. self.con.update_config(self.serverconfig)
  78. return await self.bot.say("'{}' was disabled :cry:".format(setting))
  79. else:
  80. return await self.bot.say("That module dont exist fam. You made the thing")
  81. @bot.command(pass_context=True)
  82. @checks.is_bot_owner()
  83. async def printsettings(self, ctx):
  84. self.serverconfig = self.con.load_config()
  85. config = self.serverconfig[ctx.message.server.id]
  86. em = discord.Embed(colour=0xDEADBF)
  87. em.set_author(name="RoxBot settings for {}.".format(ctx.message.server.name), icon_url=self.bot.user.avatar_url)
  88. for settings in config:
  89. settingcontent = ""
  90. for x in config[settings].items():
  91. settingcontent += str(x).strip("()") + "\n"
  92. em.add_field(name=settings, value=settingcontent)
  93. return await self.bot.say(embed=em)
  94. @group(pass_context=True, hidden=True)
  95. @checks.is_bot_owner()
  96. async def set(self, ctx):
  97. if ctx.invoked_subcommand is None:
  98. return await self.bot.say('Missing Argument')
  99. @set.command(pass_context=True, hidden=True)
  100. async def welcomechannel(self, ctx, channel: discord.Channel = None):
  101. self.serverconfig = self.con.load_config()
  102. self.serverconfig[ctx.message.server.id]["greets"]["welcome-channel"] = channel.id
  103. self.con.update_config(self.serverconfig)
  104. return await self.bot.say("{} has been set as the welcome channel!".format(channel.mention))
  105. @set.command(pass_context=True, hidden=True)
  106. async def goodbyechannel(self, ctx, channel: discord.Channel = None):
  107. self.serverconfig = self.con.load_config()
  108. self.serverconfig[ctx.message.server.id]["goodbyes"]["goodbye-channel"] = channel.id
  109. self.con.update_config(self.serverconfig)
  110. return await self.bot.say("{} has been set as the goodbye channel!".format(channel.mention))
  111. @set.command(pass_context=True, hidden=True)
  112. async def twitchchannel(self, ctx, channel: discord.Channel = None):
  113. self.serverconfig = self.con.load_config()
  114. self.serverconfig[ctx.message.server.id]["twitch"]["twitch-channel"] = channel.id
  115. self.con.update_config(self.serverconfig)
  116. return await self.bot.say("{} has been set as the twitch shilling channel!".format(channel.mention))
  117. @set.command(pass_context=True, hidden=True)
  118. async def welcomemessage(self, ctx, *, message: str):
  119. self.serverconfig = self.con.load_config()
  120. self.serverconfig[ctx.message.server.id]["greets"]["custom-message"] = message
  121. self.con.update_config(self.serverconfig)
  122. return await self.bot.say("Custom message set to '{}'".format(message))
  123. @set.command(pass_context=True, hidden=True)
  124. async def muterole(self, ctx, role: discord.Role = None):
  125. self.serverconfig = self.con.load_config()
  126. self.serverconfig[ctx.message.server.id]["mute"]["role"] = role.id
  127. self.con.update_config(self.serverconfig)
  128. return await self.bot.say("Muted role set to '{}'".format(role.name))
  129. @set.command(pass_context=True, hidden=True)
  130. async def adminrole(self, ctx, role: discord.Role = None):
  131. self.serverconfig = self.con.load_config()
  132. self.serverconfig[ctx.message.server.id]["admin_role"] = role.id
  133. self.con.update_config(self.serverconfig)
  134. return await self.bot.say("Admin role set to '{}'".format(role.name))
  135. @bot.command(pass_context=True, hidden=True, aliases=["setava", "setavatar"])
  136. @checks.is_bot_owner()
  137. async def changeavatar(self, ctx, url=None):
  138. """
  139. Usage:
  140. {command_prefix}setavatar [url]
  141. Changes the bot's avatar.
  142. Attaching a file and leaving the url parameter blank also works.
  143. """
  144. if ctx.message.attachments:
  145. thing = ctx.message.attachments[0]['url']
  146. else:
  147. thing = url.strip('<>')
  148. avaimg = 'avaimg'
  149. async with aiohttp.ClientSession() as session:
  150. async with session.get(thing) as img:
  151. with open(avaimg, 'wb') as f:
  152. f.write(await img.read())
  153. with open(avaimg, 'rb') as f:
  154. await self.bot.edit_profile(avatar=f.read())
  155. os.remove(avaimg)
  156. asyncio.sleep(2)
  157. return await self.bot.say(":ok_hand:")
  158. @bot.command(pass_context=True, hidden=True, aliases=["nick"])
  159. @checks.is_bot_owner()
  160. async def changenickname(self, ctx, *nick):
  161. if ctx.message.channel.permissions_for(ctx.message.server.me).change_nickname:
  162. await self.bot.change_nickname(ctx.message.server.me, ' '.join(nick))
  163. return await self.bot.say(":thumbsup:")
  164. else:
  165. return await self.bot.say("I don't have permission to do that :sob:", delete_after=self.con.delete_after)
  166. @bot.command(pass_context=True, hidden=True, aliases=["setgame", "game"])
  167. @checks.is_bot_owner()
  168. async def changegame(self, ctx, *, game: str):
  169. if game.lower() == "none":
  170. game_name = None
  171. else:
  172. game_name = discord.Game(name=game, type=0)
  173. await self.bot.change_presence(game=game_name, afk=False)
  174. return await self.bot.say(":ok_hand: Game set to {}".format(str(game_name)))
  175. @bot.command(pass_context=True, hidden=True, aliases=["status"])
  176. @checks.is_bot_owner()
  177. async def changestatus(self, ctx, status: str):
  178. status = status.lower()
  179. if status == 'offline' or status == 'invisible':
  180. discordStatus = discord.Status.invisible
  181. elif status == 'idle':
  182. discordStatus = discord.Status.idle
  183. elif status == 'dnd':
  184. discordStatus = discord.Status.dnd
  185. else:
  186. discordStatus = discord.Status.online
  187. await self.bot.change_presence(status=discordStatus)
  188. await self.bot.say("**:ok:** Status set to {}".format(discordStatus))
  189. @bot.command(pass_context=True, hidden=True)
  190. @checks.is_bot_owner()
  191. async def restart(self, ctx):
  192. await self.bot.logout()
  193. return os.execl(sys.executable, sys.executable, *sys.argv)
  194. @bot.command(pass_context=True, hidden=True)
  195. @checks.is_bot_owner()
  196. async def shutdown(self, ctx):
  197. await self.bot.logout()
  198. return exit(0)
  199. @bot.command(pass_context=True, hidden=True)
  200. @checks.is_bot_owner()
  201. async def announce(self, ctx, *announcement):
  202. """
  203. ONLY USE FOR SERIOUS ANNOUNCEMENTS
  204. """
  205. # TODO: Make colour top level role colour
  206. # TODO: Custom message for annoucement footer
  207. embed = discord.Embed(title="RoxBot Announcement", colour=discord.Colour(0x306f99), description=' '.join(announcement))
  208. embed.set_footer(text="This message has be automatically generated by a cute ass Roxie",
  209. icon_url=self.bot.user.avatar_url)
  210. for server in self.bot.servers:
  211. await self.bot.send_message(server, embed=embed)
  212. return await self.bot.say("Done!", delete_after=self.con.delete_after)
  213. def setup(Bot):
  214. Bot.add_cog(Settings(Bot))