Guest User

My terrible Reddit Invitation Manager for Discord

a guest
Oct 12th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. #Travis Weir
  2. #8/1/17
  3. #FRC Discord Reddit Invitation Manager (restructured to fix stability issues)
  4.  
  5. import praw, discord
  6. from datetime import datetime
  7. from discord.ext.commands import Bot
  8. discord_bot = Bot(command_prefix='^')
  9. reddit = praw.Reddit(client_id='', #Log in to Reddit
  10. client_secret='', password='', #Log in to Reddit
  11. user_agent='FRC Discord Reddit Invitation Bot by Travis', username='') #Log in to Reddit
  12.  
  13. #To-do for you: Put in bot token, replace "333825924942397442" with the ID of the channel the invites go in. Give me a link to add it to my test server (this is important if you want the bot to auto-check)
  14. #Required files in same directory: isMessage.txt author.txt
  15. def messageboolset(yesorno):
  16. messagebool = open("isMessage.txt", "r+")
  17. if yesorno:
  18. messagebool.write("True")
  19. elif not yesorno:
  20. messagebool.write("Fals")
  21. else:
  22. print("Error in calling messageboolset")
  23. messagebool.close()
  24. def approvalbot(isapproved, link):
  25. f = open("author.txt","r")
  26. user = f.read() #Make a copy of the user variable as a string
  27. f = open("author.txt","w")
  28. f.write('')
  29. f.close()
  30. if isapproved: #if the user is approved,
  31. reddit.redditor(user).message('DO NOT REPLY: FRC Discord: Approved', link) #Send the Reddit user a message with the link and an approval notice
  32. else: #Otherwise
  33. reddit.redditor(user).message('FRC Discord: Denied', "Hello! Unfortunately, the mod team has decided to deny you access to the FRC Discord at this time.") #Send them a denial message
  34. print("Message sent successfully to", user) #Put that the message was sent in the console
  35. def checkismessage():
  36. messagebool = open("isMessage.txt", "r+")
  37. isMessage = messagebool.read()
  38. isMessage = isMessage.rstrip('\n')
  39. print(isMessage)
  40. if isMessage == "Fals":
  41. return False
  42. elif isMessage == "True":
  43. return True
  44. else:
  45. print("Error in checkismessage")
  46. print(isMessage)
  47. @discord_bot.command() #Define a command
  48. async def approve(ctx): #Define the !approve command
  49. isMessage = checkismessage()
  50. if isMessage:
  51. print("Approval registered") #Print that the approval was registered to the debug shell
  52. await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send("Sending invite link") #Tell the Discord channel that it's sending them the invite
  53. approval = True #Set the approval variable to True
  54. link = await discord.utils.get(discord_bot.get_all_channels(), id=288856064089128960).create_invite(xkcd=True, max_uses=1) #Generate an invite link
  55. print(link) #Put the link in the debug shell
  56. invitation = str(link.url) #Make it a string
  57. messageboolset(False)
  58. approvalbot(approval, invitation) #Call the approval bot with the necessary variables
  59. else:
  60. await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send("No message to interact with!") # Send "sending denial message" to the Discord channel
  61. @discord_bot.command() #Define a command
  62. async def deny(ctx): #Define the !deny command
  63. print("Denial registered") #Put the Denial registered message in the debug shell
  64. isMessage = checkismessage()
  65. if isMessage:
  66. await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send("Sending denial message") #Send "sending denial message" to the Discord channel
  67. approval = False #Approval is False
  68. link = "none" #No link
  69. messageboolset(False)
  70. approvalbot(approval, link) #Call the approval send bot with necessary variables
  71. else:
  72. await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send("No message to interact with!") # Send "sending denial message" to the Discord channel
  73. @discord_bot.command() #Define a command
  74. async def skip(): #Define the !skip command
  75. print("Skip registered") #Print "Skip registered" to the debug console
  76. isMessage = checkismessage()
  77. if isMessage:
  78. await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send("Skipping...sorry about that") #Send "Skipping" message to the Discord channel
  79. messageboolset(False)
  80. else:
  81. await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send("No message to interact with!") # Send "sending denial message" to the Discord channel
  82. @discord_bot.command()
  83. async def check(ctx):
  84. unread = 0 #Set the unread message number to a base of 0
  85. for msg in reddit.inbox.unread(limit=1): #For every unread message in the Reddit inbox,
  86. unread = unread + 1 #Add one to the unread message count
  87. print("Well, I got this far. I added one to the unread count")
  88. if unread == 0: #If no new messages,
  89. print("No new messages, checking again soon. To close the bot, press CTRL and C simultaneously. You'll get an ugly error message but the bot will turn off.") #Post debug message to shell
  90. await discord.utils.get(discord_bot.get_all_channels(), id=343073901842137098).send("No new messages!") # Leave this ID, it's for a reason
  91. elif unread != 0:
  92. for item in reddit.inbox.unread(limit=1): #Grab an unread message
  93. unread_message = item #Put the unread message into a variable
  94. print("The unread message is in a variable")
  95. messagesubject = str(unread_message.subject)
  96. if unread != 0: #If there is an unread message,
  97. print("New message detected, parsing") #Post this debug message in the shell
  98. message_subject = unread_message.subject #Put the message subject in a variable
  99. message_body = str(unread_message.body) #Put the message into a variable
  100. message_author = unread_message.author #Put the message author into a variable
  101. print("Author:", message_author) #Print the Reddit username to the debug console
  102. print("Subject:", message_subject) #Print the subject to the debug console
  103. print("Body:", message_body) #Print the message body to the debug console
  104. unreadmsg = [unread_message] #Put the unread message ID in a list format
  105. author = str(message_author)
  106. f = open("author.txt","w")
  107. f.write(author)
  108. f.close()
  109. messageboolset(True)
  110. message = "**" + author + "** would like to join the server. Here's their message:\n" + message_body
  111. reddit.inbox.mark_read(unreadmsg) #Mark the message as read
  112. await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send(message) # Send message to the Discord channel
  113. @discord_bot.event
  114. async def on_ready():
  115. await discord.utils.get(discord_bot.get_all_channels(), id=343073901842137098).send("I'm alive!")
  116. discord_bot.run('') #Run my bot (add token here)
Add Comment
Please, Sign In to add comment