Praw not responding

106 views Asked by At

I dont really know why my code doesn't work. When my Reddit bot gets a message or comment with "+makeaccount" it should "contact" bitcoind and make an account with "getnewaddress" and print a message to the console, but it doesn't seem to even print to the console. This is my code:

import praw
from contextlib import suppress
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

# Authenticate with Reddit
reddit = praw.Reddit(
    client_id='',
    client_secret='',
    user_agent='',
    username='',
    password=''
)

reddit.validate_on_submit = True

# Print the username of the bot
print(reddit.user.me())

# TODO: Collect this data
#def getinboxdata(inboxdata):
# for item in reddit.inbox.all(limit=None):
#    print(repr(item))


# Define the bot's response to a comment
def get_balance():
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:22225" % ('user', 'pass'))
    get_balance = rpc_connection.getbalance()
    return get_balance # This will print the balance

def withdraw(address, amount):
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:22225" % ('user', 'pass'))
    withdraw = rpc_connection.sendtoaddress(address, amount)
    return withdraw

def getnewaddress(user):
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:22225" % ('user', 'pass'))
    getnewaddress = rpc_connection.getnewaddress(user)
    return getnewaddress

# Commands
with suppress(Exception):
 while True:
  for item in reddit.inbox.unread(limit=None):
     if item.body.startswith("+withdraw"):
          print(item.author.name + " requested a withdraw")
          command=item.body
          command_splitted = command.split(" ")
          address = command_splitted[1]
          amount = command_splitted[2]
          print(address, amount)
          item.mark_read()
          withdraw(address, amount)
          item.reply("Withdrawal of " + amount + " BKC to " + address + " successful.")
          print("Withdrew " + amount + " to " + address)
          item.mark_read()

while True:
  for item in reddit.inbox.unread(limit=None):
     if item.body.startswith("+makeaccount"):
            print("bruh")
            print(item.author.name + " requested a new account")
            user = item.author.name
            withdraw(user)
            item.reply("It's dangerous to go alone, take this!")
            print("Created account for " + user)
            item.mark_read()
1

There are 1 answers

0
furas On BEST ANSWER

You can't run two while True loops at the same time.

First loop runs forever and other loop never starts.

You can run every loop in separated thread/processes

but simpler is to put all if item.body.startswith(...):... in one while True

with suppress(Exception):
 while True:
  for item in reddit.inbox.unread(limit=None):

     if item.body.startswith("+withdraw"):
          print(item.author.name + " requested a withdraw")
          command=item.body
          command_splitted = command.split(" ")
          address = command_splitted[1]
          amount = command_splitted[2]
          print(address, amount)
          item.mark_read()
          withdraw(address, amount)
          item.reply("Withdrawal of " + amount + " BKC to " + address + " successful.")
          print("Withdrew " + amount + " to " + address)
          item.mark_read()

     if item.body.startswith("+makeaccount"):
            print("bruh")
            print(item.author.name + " requested a new account")
            user = item.author.name
            withdraw(user)
            item.reply("It's dangerous to go alone, take this!")
            print("Created account for " + user)
            item.mark_read()