大虾没反应

Praw not responding

我真的不知道为什么我的代码不起作用。当我的 Reddit 机器人收到带有“+makeaccount”的消息或评论时,它应该“联系”bitcoind 并使用“getnewaddress”创建一个帐户并将消息打印到控制台,但它似乎甚至没有打印到控制台。这是我的代码:

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:%s@127.0.0.1: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:%s@127.0.0.1:22225" % ('user', 'pass'))
    withdraw = rpc_connection.sendtoaddress(address, amount)
    return withdraw

def getnewaddress(user):
    rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1: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()

您不能 运行 两个 while True 同时循环。

第一个循环 运行 永远,另一个循环永远不会开始。

您可以 运行 将每个循环分开 thread/processes

但更简单的是将所有 if item.body.startswith(...):... 放在一个 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()