Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

433 linhas
15KB

  1. # RoxBot
  2. # Version = 1.2
  3. # Author = Roxxers
  4. ##############
  5. # To-do List #
  6. ##############
  7. # TODO: On member role assign, welcome member using on_member_update
  8. # TODO: Better help menu- AutoGen using <command>.help
  9. # TODO: WaifuRater - Mention user and RNG a rating
  10. # TODO: Admin tools - For commands already in and things like purge a chat
  11. # TODO: Overwatch stats - Using Overwatch-API lib
  12. # TODO: Move away from using ID's for everthing. Maybe replace list with dict
  13. # TODO: Add check for no channel id when a module is enabled
  14. # Fixed bug with blacklist remove options. So that any option not '+' or 'add' is no longer accepted as the remove option.
  15. # Optimised removerole command
  16. # New names for some commands so it is easier to know what module they change.
  17. # Twitch Shilling
  18. # Fixed major bug with the owner only commands
  19. import json
  20. import random
  21. import discord
  22. from discord.ext.commands import Bot
  23. bot = Bot(command_prefix=".")
  24. # bot.remove_command("help")
  25. # TODO: Take these from a file, not the program
  26. token = 'MzA4MDc3ODg3MDYyNTQwMjg5.DEW5YA.JfLfU5jPjTFQi0xFI6B_-SKvC54'
  27. owner_id = "142735312626515979"
  28. config_template = {
  29. "example": {
  30. "greets": {
  31. "enabled": 0,
  32. "welcome-channel": "",
  33. "member-role": ""
  34. },
  35. "goodbyes": {
  36. "enabled": 0,
  37. "goodbye-channel": ""
  38. },
  39. "self-assign_roles": {
  40. "enabled": 0,
  41. "roles": []
  42. },
  43. "twitch_shilling": {
  44. "enabled": 0,
  45. "twitch-channel": "",
  46. "whitelist": {
  47. "enabled": 0,
  48. "list": []
  49. }
  50. }
  51. }
  52. }
  53. def load_config():
  54. with open('config.json', 'r') as config_file:
  55. return json.load(config_file)
  56. def updateconfig():
  57. with open('config.json', 'w') as conf_file:
  58. json.dump(config, conf_file)
  59. def config_errorcheck():
  60. # Checks for errors in the config files and fixes them automatically
  61. for server in bot.servers:
  62. if server.id not in config:
  63. config[server.id] = config_template["example"]
  64. updateconfig()
  65. print("WARNING: The config file for {} was not found. A template has been loaded and saved. All modules are turned off by default.".format(server.name.upper()))
  66. else:
  67. for module_setting in config_template["example"]:
  68. if module_setting not in config[server.id]:
  69. config[server.id][module_setting] = config_template["example"][module_setting]
  70. updateconfig()
  71. print("WARNING: The config file for {} was missing the {} module. This has been fixed with the template version. It is disabled by default.".format(server.name.upper(), module_setting.upper()))
  72. def owner(ctx):
  73. if owner_id == ctx.message.author.id:
  74. return True
  75. else:
  76. return False
  77. def mention_commandee(ctx):
  78. return ctx.message.author
  79. def blacklisted(user):
  80. with open("blacklist.txt", "r") as fp:
  81. for line in fp.readlines():
  82. if user.id+"\n" == line:
  83. return True
  84. return False
  85. def dice_roll(num):
  86. if num == 100:
  87. step = 10
  88. else:
  89. step = 1
  90. return random.randrange(step, num+1, step)
  91. @bot.event
  92. async def on_ready():
  93. # TODO: First part needs to be moved to wait_until_ready
  94. config_errorcheck()
  95. print("Client logged in\n")
  96. print("Servers I am currently in:")
  97. for server in bot.servers:
  98. print(server)
  99. @bot.event
  100. async def on_member_update(member_b, member_a):
  101. # Twitch Shilling Part
  102. ts_enabled = config[member_a.server.id]["twitch_shilling"]["enabled"]
  103. if ts_enabled:
  104. if not config[member_a.server.id]["twitch_shilling"]["whitelist"]["enabled"] or member_a.id in config[member_a.server.id]["twitch_shilling"]["whitelist"]["list"]:
  105. if member_a.game:
  106. if member_a.game.type:
  107. channel = discord.Object(config[member_a.server.id]["twitch_shilling"]["twitch-channel"])
  108. return await bot.send_message(channel, content=":video_game:**{} is live!**:video_game:\n {}\n{}".format(member_a.name, member_a.game.name, member_a.game.url))
  109. @bot.event
  110. async def on_message(message):
  111. if blacklisted(message.author):
  112. return
  113. else:
  114. return await bot.process_commands(message)
  115. @bot.event
  116. async def on_member_join(member):
  117. """
  118. Greets users when they join a server.
  119. :param member:
  120. :return:
  121. """
  122. if not config[member.server.id]["greets"]["enabled"]:
  123. return
  124. em = discord.Embed(
  125. title="Welcome to {}!".format(member.server),
  126. description='Hey {}! Welcome to {}! Be sure to read the rules.'.format(member.mention, member.server),
  127. colour=0xDEADBF)
  128. if config[member.server.id]["greets"]["welcome-channel"]:
  129. channel = discord.Object(config[member.server.id]["greets"]["welcome-channel"])
  130. else:
  131. channel = member.server.default_channel
  132. return await bot.send_message(channel,embed=em)
  133. @bot.event
  134. async def on_member_remove(member):
  135. if not config[member.server.id]["goodbyes"]["enabled"]:
  136. return
  137. else:
  138. return await bot.send_message(member.server,embed=discord.Embed(
  139. description="{}#{} has left or been beaned.".format(member.name, member.discriminator), colour=0xDEADBF))
  140. @bot.event
  141. async def on_server_join(server):
  142. config[server.id] = config_template["example"]
  143. updateconfig()
  144. @bot.event
  145. async def on_server_remove(server):
  146. config.pop(server.id)
  147. updateconfig()
  148. @bot.command(pass_context=True)
  149. async def iam(ctx, role: discord.Role = None, *, user: discord.User = None, server: discord.Server = None):
  150. if not config[ctx.message.server.id]["self-assign_roles"]["enabled"]:
  151. return
  152. user = ctx.message.author
  153. server = ctx.message.server
  154. if role not in server.roles:
  155. return await bot.say("That role doesn't exist. Roles are case sensitive. ")
  156. if role in user.roles:
  157. return await bot.say("You already have that role.")
  158. if role.id in config[ctx.message.server.id]["self-assign_roles"]["roles"]:
  159. await bot.add_roles(user, role)
  160. print("{} added {} to themselves in {} on {}".format(user.display_name, role.name, ctx.message.channel,
  161. ctx.message.server))
  162. return await bot.say("Yay {}! You now have the {} role!".format(user.mention, role.name))
  163. else:
  164. return await bot.say("That role is not self-assignable.")
  165. @bot.command(pass_context=True, enabled=False)
  166. async def dice(ctx, num, *, user: discord.User = None):
  167. # TODO: Change to ndx format
  168. die = ("4","6","8","10","12","20","100")
  169. if num not in die:
  170. if num == "help":
  171. return await bot.say("!dice - This command random roles a dice. The die I support are (4, 6, 8, 10, 12, 20, 100) like the ones used in Table Top games.")
  172. else:
  173. return await bot.say("That is not a dice I know. Try !dice help for help!")
  174. user = mention_commandee(ctx)
  175. roll = dice_roll(int(num))
  176. return await bot.say("You rolled a {}, {}".format(roll,user.mention))
  177. @bot.command(pass_context=True)
  178. async def suck(ctx, user: discord.User = None):
  179. if user is None:
  180. try:
  181. user = ctx.message.mentions[0]
  182. except:
  183. return await bot.say("You didn't mention someone for me to suck")
  184. return await bot.say(":eggplant: :sweat_drops: :tongue: {}".format(user.mention))
  185. @bot.command(enabled=False)
  186. async def printcommands():
  187. for command in bot.commands:
  188. print(command)
  189. return await bot.say("Done.")
  190. @bot.command(pass_context=True)
  191. async def listroles(ctx):
  192. roles = []
  193. for role in config[ctx.message.server.id]["self-assign_roles"]["roles"]:
  194. for serverrole in ctx.message.server.roles:
  195. if role == serverrole.id:
  196. roles.append(serverrole.name)
  197. return await bot.say(roles)
  198. #################
  199. # Owner Commands#
  200. #################
  201. @bot.command(pass_context=True)
  202. async def blacklist(ctx, option, *mentions):
  203. """
  204. Usage:
  205. .blacklist [ + | - | add | remove ] @UserName [@UserName2 ...]
  206. Add or remove users to the blacklist.
  207. Blacklisted users are forbidden from using bot commands.
  208. Only the bot owner can use this command
  209. """
  210. if not owner(ctx):
  211. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  212. blacklist_amount = 0
  213. mentions = ctx.message.mentions
  214. if not mentions:
  215. return await bot.say("You didn't mention anyone")
  216. if option not in ['+', '-', 'add', 'remove']:
  217. return await bot.say('Invalid option "%s" specified, use +, -, add, or remove' % option, expire_in=20)
  218. for user in mentions:
  219. if user.id == owner_id:
  220. print("[Commands:Blacklist] The owner cannot be blacklisted.")
  221. await bot.say("The owner cannot be blacklisted.")
  222. mentions.remove(user)
  223. if option in ['+', 'add']:
  224. with open("blacklist.txt", "r") as fp:
  225. for user in mentions:
  226. for line in fp.readlines():
  227. if user.id+"\n" in line:
  228. mentions.remove(user)
  229. with open("blacklist.txt","a+") as fp:
  230. lines = fp.readlines()
  231. for user in mentions:
  232. if user.id not in lines:
  233. fp.write("{}\n".format(user.id))
  234. blacklist_amount += 1
  235. return await bot.say('{} user(s) have been added to the blacklist'.format(blacklist_amount))
  236. elif option in ['-', 'remove']:
  237. with open("blacklist.txt","r") as fp:
  238. lines = fp.readlines()
  239. with open("blacklist.txt","w") as fp:
  240. for user in mentions:
  241. for line in lines:
  242. if user.id+"\n" != line:
  243. fp.write(line)
  244. else:
  245. fp.write("")
  246. blacklist_amount += 1
  247. return await bot.say('{} user(s) have been removed from the blacklist'.format(blacklist_amount))
  248. @bot.command(pass_context=True)
  249. async def addrole(ctx, role: discord.Role = None):
  250. # Add Remove List Help
  251. if not owner(ctx):
  252. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  253. else:
  254. config[ctx.message.server.id]["self-assign_roles"]["roles"].append(role.id)
  255. updateconfig()
  256. return await bot.say('Role "{}" added'.format(str(role)))
  257. @bot.command(pass_context=True)
  258. async def removerole(ctx, role: discord.Role = None):
  259. if not owner(ctx):
  260. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  261. if role.id in config[ctx.message.server.id]["self-assign_roles"]["roles"]:
  262. config[ctx.message.server.id]["self-assign_roles"]["roles"].remove(role.id)
  263. return await bot.say('"{}" has been removed from the self-assignable roles.'.format(str(role)))
  264. else:
  265. return await bot.say("That role was not in the list.")
  266. @bot.command(pass_context=True)
  267. async def enablemodule(ctx, module):
  268. if not owner(ctx):
  269. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  270. else:
  271. if module in config[ctx.message.server.id]:
  272. if not config[ctx.message.server.id][module]["enabled"]:
  273. config[ctx.message.server.id][module]["enabled"] = 1
  274. updateconfig()
  275. return await bot.say("'{}' was enabled!".format(module))
  276. else:
  277. config[ctx.message.server.id][module]["enabled"] = 0
  278. updateconfig()
  279. return await bot.say("'{}' was disabled :cry:".format(module))
  280. else:
  281. return await bot.say("That module dont exist fam. You made the thing")
  282. @bot.command(pass_context=True)
  283. async def set_welcomechannel(ctx, channel: discord.Channel = None):
  284. if not owner(ctx):
  285. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  286. config[ctx.message.server.id]["greets"]["welcome-channel"] = channel.id
  287. updateconfig()
  288. return await bot.say("{} has been set as the welcome channel!".format(channel.mention))
  289. @bot.command(pass_context=True)
  290. async def set_goodbyechannel(ctx, channel: discord.Channel = None):
  291. if not owner(ctx):
  292. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  293. config[ctx.message.server.id]["goodbyes"]["goodbye-channel"] = channel.id
  294. updateconfig()
  295. return await bot.say("{} has been set as the goodbye channel!".format(channel.mention))
  296. @bot.command(pass_context=True)
  297. async def set_twitchchannel(ctx, channel: discord.Channel = None):
  298. if not owner(ctx):
  299. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  300. config[ctx.message.server.id]["twitch_shilling"]["twitch-channel"] = channel.id
  301. updateconfig()
  302. return await bot.say("{} has been set as the twitch shilling channel!".format(channel.mention))
  303. @bot.command(pass_context=True)
  304. async def ts_enablewhitelist(ctx):
  305. if not owner(ctx):
  306. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  307. else:
  308. if not config[ctx.server.id]["twitch_shilling"]["whitelist"]["enabled"]:
  309. config[ctx.server.id]["twitch_shilling"]["whitelist"]["enabled"] = 1
  310. updateconfig()
  311. return await bot.reply("Whitelist for Twitch shilling has been enabled.")
  312. else:
  313. config[ctx.server.id]["twitch_shilling"]["whitelist"]["enabled"] = 0
  314. updateconfig()
  315. return await bot.reply("Whitelist for Twitch shilling has been disabled.")
  316. @bot.command(pass_context=True)
  317. async def ts_whitelist(ctx, option, *mentions):
  318. if not owner(ctx):
  319. return await bot.reply("You do not have permission to do this command.", delete_after=20)
  320. whitelist_count = 0
  321. if not ctx.message.mentions and option != 'list':
  322. return await bot.reply("You haven't mentioned anyone to whitelist.")
  323. if option not in ['+', '-', 'add', 'remove', 'list']:
  324. return await bot.say('Invalid option "%s" specified, use +, -, add, or remove' % option, expire_in=20)
  325. if option in ['+', 'add']:
  326. for user in ctx.message.mentions:
  327. config[ctx.message.server.id]["twitch_shilling"]["whitelist"]["list"].append(user.id)
  328. updateconfig()
  329. whitelist_count += 1
  330. return await bot.say('{} user(s) have been added to the whitelist'.format(whitelist_count))
  331. elif option in ['-', 'remove']:
  332. for user in ctx.message.mentions:
  333. if user.id in config[ctx.message.server.id]["twitch_shilling"]["whitelist"]["list"]:
  334. config[ctx.message.server.id]["twitch_shilling"]["whitelist"]["list"].remove(user.id)
  335. updateconfig()
  336. whitelist_count += 1
  337. return await bot.say('{} user(s) have been removed to the whitelist'.format(whitelist_count))
  338. elif option == 'list':
  339. return await bot.say(config[ctx.message.server.id]["twitch_shilling"]["whitelist"]["list"])
  340. if __name__ == "__main__":
  341. config = load_config()
  342. bot.run(token)