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.

445 lines
15KB

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