首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Praw无反应

Praw无反应
EN

Stack Overflow用户
提问于 2022-03-06 18:16:03
回答 1查看 57关注 0票数 -2

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

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-06 20:18:33

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

第一个循环永远运行,而其他循环永远不会启动。

您可以在分隔的线程/进程中运行每个循环。

但是更简单的方法是把所有的if item.body.startswith(...):...放在一个while True

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71373024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档