我真的不知道为什么我的代码不工作。当我的Reddit bot收到带有"+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()发布于 2022-03-06 20:18:33
不能同时运行两个while True循环。
第一个循环永远运行,而其他循环永远不会启动。
您可以在分隔的线程/进程中运行每个循环。
但是更简单的方法是把所有的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()https://stackoverflow.com/questions/71373024
复制相似问题