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.

317 lines
12KB

  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)
  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. @set.command(pass_context=True, hidden=True)
  133. async def loggingchannel(self, ctx, channel: discord.Channel = None):
  134. self.servers = self.con.load_config()
  135. self.servers[ctx.message.server.id]["gss"]["logging_channel"] = channel.id
  136. self.con.update_config(self.servers)
  137. return await self.bot.say("Logging Channel set to '{}'".format(channel.name))
  138. @set.command(pass_context=True, hidden=True)
  139. async def requireddays(self, ctx, days: int):
  140. self.servers = self.con.load_config()
  141. self.servers[ctx.message.server.id]["gss"]["required_days"] = str(days)
  142. self.con.update_config(self.servers)
  143. return await self.bot.say("Required days set to '{}'".format(str(days)))
  144. @set.command(pass_context=True, hidden=True)
  145. async def requiredscore(self, ctx, score: int):
  146. self.servers = self.con.load_config()
  147. self.servers[ctx.message.server.id]["gss"]["required_score"] = str(score)
  148. self.con.update_config(self.servers)
  149. return await self.bot.say("Required score set to '{}'".format(str(score)))
  150. @group(pass_context=True)
  151. @checks.is_owner_or_admin()
  152. async def add(self, ctx):
  153. "OWNER OR ADMIN ONLY: Adds to lists like admin roles."
  154. if ctx.invoked_subcommand is None:
  155. return await self.bot.say('Missing Argument')
  156. @add.command(pass_context=True, aliases=["adminrole"])
  157. async def addadminrole(self, ctx, *, role: discord.Role = None):
  158. self.servers = self.con.load_config()
  159. if role.id not in self.servers[ctx.message.server.id]["perm_roles"]["admin"]:
  160. self.servers[ctx.message.server.id]["perm_roles"]["admin"].append(role.id)
  161. self.con.update_config(self.servers)
  162. return await self.bot.say("'{}' has been added to the Admin role list.".format(role.name))
  163. else:
  164. return await self.bot.say("'{}' is already in the list.".format(role.name))
  165. @add.command(pass_context=True, aliases=["modrole"])
  166. async def addmodrole(self, ctx, *, role: discord.Role = None):
  167. self.servers = self.con.load_config()
  168. if role.id not in self.servers[ctx.message.server.id]["perm_roles"]["mod"]:
  169. self.servers[ctx.message.server.id]["perm_roles"]["mod"].append(role.id)
  170. self.con.update_config(self.servers)
  171. return await self.bot.say("'{}' has been added to the Mod role list.".format(role.name))
  172. else:
  173. return await self.bot.say("'{}' is already in the list.".format(role.name))
  174. @group(pass_context=True)
  175. @checks.is_owner_or_admin()
  176. async def remove(self, ctx):
  177. "OWNER OR ADMIN ONLY: Removes things like admin roles."
  178. if ctx.invoked_subcommand is None:
  179. return await self.bot.say('Missing Argument')
  180. @remove.command(pass_context=True, aliases=["adminrole"])
  181. async def readminrole(self, ctx, *, role: discord.Role = None):
  182. self.servers = self.con.load_config()
  183. try:
  184. self.servers[ctx.message.server.id]["perm_roles"]["admin"].remove(role.id)
  185. except ValueError:
  186. return await self.bot.say("That role was not in the list.")
  187. self.con.update_config(self.servers)
  188. return await self.bot.say("'{}' has been removed from the Admin role list.".format(role.name))
  189. @remove.command(pass_context=True, aliases=["modrole"])
  190. async def remodrole(self, ctx, *, role: discord.Role = None):
  191. self.servers = self.con.load_config()
  192. try:
  193. self.servers[ctx.message.server.id]["perm_roles"]["mod"].remove(role.id)
  194. except ValueError:
  195. return await self.bot.say("That role was not in the list.")
  196. self.con.update_config(self.servers)
  197. return await self.bot.say("'{}' has been removed from the Mod role list.".format(role.name))
  198. @bot.command(pass_context=True, hidden=True, aliases=["setava", "setavatar"])
  199. @checks.is_bot_owner()
  200. async def changeavatar(self, ctx, url=None):
  201. """
  202. Usage:
  203. {command_prefix}setavatar [url]
  204. Changes the bot's avatar.
  205. Attaching a file and leaving the url parameter blank also works.
  206. """
  207. if ctx.message.attachments:
  208. thing = ctx.message.attachments[0]['url']
  209. else:
  210. thing = url.strip('<>')
  211. avaimg = 'avaimg'
  212. async with aiohttp.ClientSession() as session:
  213. async with session.get(thing) as img:
  214. with open(avaimg, 'wb') as f:
  215. f.write(await img.read())
  216. with open(avaimg, 'rb') as f:
  217. await self.bot.edit_profile(avatar=f.read())
  218. os.remove(avaimg)
  219. asyncio.sleep(2)
  220. return await self.bot.say(":ok_hand:")
  221. @bot.command(pass_context=True, hidden=True, aliases=["nick"])
  222. @checks.is_bot_owner()
  223. async def changenickname(self, ctx, *nick):
  224. if ctx.message.channel.permissions_for(ctx.message.server.me).change_nickname:
  225. await self.bot.change_nickname(ctx.message.server.me, ' '.join(nick))
  226. return await self.bot.say(":thumbsup:")
  227. else:
  228. return await self.bot.say("I don't have permission to do that :sob:", delete_after=self.con.delete_after)
  229. @bot.command(pass_context=True, hidden=True, aliases=["setgame", "game"])
  230. @checks.is_bot_owner()
  231. async def changegame(self, ctx, *, game: str):
  232. if game.lower() == "none":
  233. game_name = None
  234. else:
  235. game_name = discord.Game(name=game, type=0)
  236. await self.bot.change_presence(game=game_name, afk=False)
  237. return await self.bot.say(":ok_hand: Game set to {}".format(str(game_name)))
  238. @bot.command(pass_context=True, hidden=True, aliases=["status"])
  239. @checks.is_bot_owner()
  240. async def changestatus(self, ctx, status: str):
  241. status = status.lower()
  242. if status == 'offline' or status == 'invisible':
  243. discordStatus = discord.Status.invisible
  244. elif status == 'idle':
  245. discordStatus = discord.Status.idle
  246. elif status == 'dnd':
  247. discordStatus = discord.Status.dnd
  248. else:
  249. discordStatus = discord.Status.online
  250. await self.bot.change_presence(status=discordStatus)
  251. await self.bot.say("**:ok:** Status set to {}".format(discordStatus))
  252. @bot.command(hidden=True)
  253. @checks.is_bot_owner()
  254. async def restart(self):
  255. await self.bot.logout()
  256. return os.execl(sys.executable, sys.executable, *sys.argv)
  257. @bot.command(hidden=True)
  258. @checks.is_bot_owner()
  259. async def shutdown(self):
  260. await self.bot.logout()
  261. return exit(0)
  262. @bot.command(pass_context=True, hidden=True)
  263. @checks.is_bot_owner()
  264. async def announce(self, ctx, *announcement):
  265. """
  266. ONLY USE FOR SERIOUS ANNOUNCEMENTS
  267. """
  268. # TODO: Make colour top level role colour
  269. # TODO: Custom message for annoucement footer
  270. embed = discord.Embed(title="RoxBot Announcement", colour=discord.Colour(0x306f99), description=' '.join(announcement))
  271. embed.set_footer(text="This message has be automatically generated by a cute ass Roxie",
  272. icon_url=self.bot.user.avatar_url)
  273. for server in self.bot.servers:
  274. await self.bot.send_message(server, embed=embed)
  275. return await self.bot.say("Done!", delete_after=self.con.delete_after)
  276. def setup(Bot):
  277. Bot.add_cog(Settings(Bot))