public class BotDriver
extends java.lang.Object
implements java.lang.Runnable
Summon
Modifier and Type | Field and Description |
---|---|
protected Bot |
bot
The bot.
|
protected static int |
BRIEF_PAUSE_MS
Time in seconds between touching the reddit api
|
protected CommentSummon[] |
commentSummons
The comment summons.
|
protected FileConfiguration |
config
The config.
|
protected Database |
database
The database.
|
protected org.apache.logging.log4j.Logger |
logger
The logger.
|
protected java.lang.Runnable |
maybeLoginAgainRunnable
A runnable that simply calls maybeLoginAgain
|
protected PMSummon[] |
pmSummons
The pm summons.
|
protected LinkSummon[] |
submissionSummons
The submission summons.
|
Constructor and Description |
---|
BotDriver(Database database,
FileConfiguration config,
Bot bot,
CommentSummon[] commentSummons,
PMSummon[] pmSummons,
LinkSummon[] submissionSummons)
Creates a bot driver based on the specified database, configuration info,
rest client, and bot.
|
Modifier and Type | Method and Description |
---|---|
protected void |
doLoop()
Performs one loop for the bot
|
protected void |
fail(java.lang.String message,
java.lang.Throwable... errors)
Terminates the program after logging
message at
Level.ERROR, as well as logging the list of errors |
protected me.timothy.jreddit.info.Listing |
getRecentComments()
Gets a list of recent comments, utilizing exponential-backoff.
|
protected me.timothy.jreddit.info.Listing |
getRecentMessages()
Gets a list of recent messages, utilizing exponential back-off.
|
protected me.timothy.jreddit.info.Listing |
getRecentSubmissions()
Gets a list of recent submissions, utilizing exponential back-off.
|
protected boolean |
handleComment(me.timothy.jreddit.info.Comment comment,
boolean debug,
boolean silentMode)
Handles a single comment
|
protected void |
handleFlair(java.lang.String linkId,
java.lang.String flair)
Flairs a link
|
protected void |
handlePM(me.timothy.jreddit.info.Thing m,
boolean silentMode)
Handles a single message in our inbox
|
protected void |
handlePMResponses(SummonResponse response)
Handles the PMResponses that are contained in the summon response, if any,
by sending pms to the specified users with the specified title and content.
|
protected void |
handleReply(me.timothy.jreddit.info.Thing replyable,
java.lang.String response)
Replies to the thing, utilizing exponential back-off.
|
protected void |
handleReport(java.lang.String thingFullname,
java.lang.String reportMessage) |
protected void |
handleSubmission(me.timothy.jreddit.info.Link submission,
boolean silentMode)
Handles a single submission
|
protected void |
login()
Logs into reddit based on the configuration.
|
protected void |
markRead(me.timothy.jreddit.info.Listing messages)
Marks the specified messages as read.
|
protected void |
maybeLoginAgain()
Checks if we need to refresh our access token, and if so,
purges it and logs in.
|
void |
run()
Runs the bot.
|
protected void |
scanComments()
Loops through recent comments, ignoring comments by banned
users or remembered fullnames, and handles them via the appropriate
summons.
|
protected void |
scanPersonalMessages()
Loops through unread personal messages and replies to the bot.
|
protected void |
scanSubmissions()
Loops through recent submissions, ignoring remembered fullnames,
and checks for "req" (case insensitive) in the title.
|
protected void |
sendMessage(java.lang.String to,
java.lang.String title,
java.lang.String message)
Sends a message to the specified user with the specified
title & message, using exponential back-off.
|
protected void |
sleepFor(long ms)
Sleeps for the specified time in milliseconds, as if by
Thread.sleep(ms) . |
protected static int BRIEF_PAUSE_MS
protected final CommentSummon[] commentSummons
protected final PMSummon[] pmSummons
protected final LinkSummon[] submissionSummons
protected FileConfiguration config
protected Database database
protected Bot bot
protected org.apache.logging.log4j.Logger logger
protected java.lang.Runnable maybeLoginAgainRunnable
maybeLoginAgain()
public BotDriver(Database database, FileConfiguration config, Bot bot, CommentSummon[] commentSummons, PMSummon[] pmSummons, LinkSummon[] submissionSummons)
database
- the databaseconfig
- the configurationrestClient
- rest clientbot
- the botcommentSummons
- the comment summonspmSummons
- the pm summonssubmissionSummons
- the submission summonspublic void run()
run
in interface java.lang.Runnable
protected void doLoop() throws java.io.IOException, org.json.simple.parser.ParseException, java.text.ParseException
java.io.IOException
- if an i/o related exception occursorg.json.simple.parser.ParseException
- if a parse exception occursjava.text.ParseException
- if a parse exception occursprotected void maybeLoginAgain()
protected void scanComments()
protected boolean handleComment(me.timothy.jreddit.info.Comment comment, boolean debug, boolean silentMode)
comment
- the comment to handledebug
- if debug messages should be printedsilentMode
- if this should not replyprotected void scanSubmissions() throws java.io.IOException, org.json.simple.parser.ParseException, java.text.ParseException
java.io.IOException
- if an i/o related exception occursjava.text.ParseException
- if the parse via Summon.CHECK_SUMMON is invalid (should not happen since the check is automatic)java.text.ParseException
- if the parse via Summon.CHECK_SUMMON is invalid (should not happen since the check is automatic)org.json.simple.parser.ParseException
protected void handleSubmission(me.timothy.jreddit.info.Link submission, boolean silentMode)
submission
- the submission the handlesilentMode
- if the bot should not respondprotected void scanPersonalMessages()
protected void handlePM(me.timothy.jreddit.info.Thing m, boolean silentMode)
m
- the pm to handlesilentMode
- if this should not respondprotected void markRead(me.timothy.jreddit.info.Listing messages)
messages
- the messagesRetryable
protected void login()
protected me.timothy.jreddit.info.Listing getRecentComments()
Retryable
protected void handleReply(me.timothy.jreddit.info.Thing replyable, java.lang.String response)
replyable
- the thing to reply toresponse
- the thing to reply withRetryable
protected me.timothy.jreddit.info.Listing getRecentSubmissions()
Retryable
protected me.timothy.jreddit.info.Listing getRecentMessages()
Retryable
protected void handleFlair(java.lang.String linkId, java.lang.String flair)
linkId
- the link to flairflair
- the css class of the flairprotected void handleReport(java.lang.String thingFullname, java.lang.String reportMessage)
protected void sleepFor(long ms)
Thread.sleep(ms)
. Logs the exception and terminates
the program on error.ms
- the time in milliseconds to sleepprotected void fail(java.lang.String message, java.lang.Throwable... errors)
message
at
Level.ERROR, as well as logging the list of errorsmessage
- the message to put in Level.ERRORerrors
- a list of applicable errors to outputprotected void sendMessage(java.lang.String to, java.lang.String title, java.lang.String message)
user
- the user to send the message totitle
- the title of the messagemessage
- the text of the messageprotected void handlePMResponses(SummonResponse response)
response
- the response which might have pmresponses.