Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Travis Weir
- #8/1/17
- #FRC Discord Reddit Invitation Manager (restructured to fix stability issues)
- import praw, discord
- from datetime import datetime
- from discord.ext.commands import Bot
- discord_bot = Bot(command_prefix='^')
- reddit = praw.Reddit(client_id='', #Log in to Reddit
- client_secret='', password='', #Log in to Reddit
- user_agent='FRC Discord Reddit Invitation Bot by Travis', username='') #Log in to Reddit
- #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)
- #Required files in same directory: isMessage.txt author.txt
- def messageboolset(yesorno):
- messagebool = open("isMessage.txt", "r+")
- if yesorno:
- messagebool.write("True")
- elif not yesorno:
- messagebool.write("Fals")
- else:
- print("Error in calling messageboolset")
- messagebool.close()
- def approvalbot(isapproved, link):
- f = open("author.txt","r")
- user = f.read() #Make a copy of the user variable as a string
- f = open("author.txt","w")
- f.write('')
- f.close()
- if isapproved: #if the user is approved,
- reddit.redditor(user).message('DO NOT REPLY: FRC Discord: Approved', link) #Send the Reddit user a message with the link and an approval notice
- else: #Otherwise
- 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
- print("Message sent successfully to", user) #Put that the message was sent in the console
- def checkismessage():
- messagebool = open("isMessage.txt", "r+")
- isMessage = messagebool.read()
- isMessage = isMessage.rstrip('\n')
- print(isMessage)
- if isMessage == "Fals":
- return False
- elif isMessage == "True":
- return True
- else:
- print("Error in checkismessage")
- print(isMessage)
- @discord_bot.command() #Define a command
- async def approve(ctx): #Define the !approve command
- isMessage = checkismessage()
- if isMessage:
- print("Approval registered") #Print that the approval was registered to the debug shell
- 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
- approval = True #Set the approval variable to True
- link = await discord.utils.get(discord_bot.get_all_channels(), id=288856064089128960).create_invite(xkcd=True, max_uses=1) #Generate an invite link
- print(link) #Put the link in the debug shell
- invitation = str(link.url) #Make it a string
- messageboolset(False)
- approvalbot(approval, invitation) #Call the approval bot with the necessary variables
- else:
- 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
- @discord_bot.command() #Define a command
- async def deny(ctx): #Define the !deny command
- print("Denial registered") #Put the Denial registered message in the debug shell
- isMessage = checkismessage()
- if isMessage:
- await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send("Sending denial message") #Send "sending denial message" to the Discord channel
- approval = False #Approval is False
- link = "none" #No link
- messageboolset(False)
- approvalbot(approval, link) #Call the approval send bot with necessary variables
- else:
- 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
- @discord_bot.command() #Define a command
- async def skip(): #Define the !skip command
- print("Skip registered") #Print "Skip registered" to the debug console
- isMessage = checkismessage()
- if isMessage:
- await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send("Skipping...sorry about that") #Send "Skipping" message to the Discord channel
- messageboolset(False)
- else:
- 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
- @discord_bot.command()
- async def check(ctx):
- unread = 0 #Set the unread message number to a base of 0
- for msg in reddit.inbox.unread(limit=1): #For every unread message in the Reddit inbox,
- unread = unread + 1 #Add one to the unread message count
- print("Well, I got this far. I added one to the unread count")
- if unread == 0: #If no new messages,
- 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
- await discord.utils.get(discord_bot.get_all_channels(), id=343073901842137098).send("No new messages!") # Leave this ID, it's for a reason
- elif unread != 0:
- for item in reddit.inbox.unread(limit=1): #Grab an unread message
- unread_message = item #Put the unread message into a variable
- print("The unread message is in a variable")
- messagesubject = str(unread_message.subject)
- if unread != 0: #If there is an unread message,
- print("New message detected, parsing") #Post this debug message in the shell
- message_subject = unread_message.subject #Put the message subject in a variable
- message_body = str(unread_message.body) #Put the message into a variable
- message_author = unread_message.author #Put the message author into a variable
- print("Author:", message_author) #Print the Reddit username to the debug console
- print("Subject:", message_subject) #Print the subject to the debug console
- print("Body:", message_body) #Print the message body to the debug console
- unreadmsg = [unread_message] #Put the unread message ID in a list format
- author = str(message_author)
- f = open("author.txt","w")
- f.write(author)
- f.close()
- messageboolset(True)
- message = "**" + author + "** would like to join the server. Here's their message:\n" + message_body
- reddit.inbox.mark_read(unreadmsg) #Mark the message as read
- await discord.utils.get(discord_bot.get_all_channels(), id=354435647441731584).send(message) # Send message to the Discord channel
- @discord_bot.event
- async def on_ready():
- await discord.utils.get(discord_bot.get_all_channels(), id=343073901842137098).send("I'm alive!")
- discord_bot.run('') #Run my bot (add token here)
Add Comment
Please, Sign In to add comment