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.

296 lines
11KB

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