首先,我需要说我以前从未尝试过在python中编码......
我正在尝试使Twitch IRC机器人工作,但我一直在失败......
我的bot.py代码如下所示:
from src.lib import irc as irc_
from src.lib import functions_general
from src.lib import functions_commands as commands
from src.config import config
class PartyMachine:
def __init__(self, config):
self.config = config
self.irc = irc_.irc(config)
self.socket = self.irc.get_irc_socket_object()
def sock(self):
irc = self.irc
sock = self.socket
config = self.config
kage = sock
while True:
data = sock.recv(2048).rstrip()
if len(data) == 0:
pp('Connection was lost, reconnecting.')
sock = self.irc.get_irc_socket_object()
if config['debug']:
print (data)
我的config.py在这里:
'socket_buffer_size': 1024
我的irc.py在这里:
def get_irc_socket_object(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
self.sock = sock
try:
sock.connect((self.config['server'], self.config['port']))
except:
pp('Cannot connect to server (%s:%s).' % (self.config['server'], self.config['port']), 'error')
sys.exit()
sock.settimeout(None)
def sock_send(sock, send, self):
sock.send('USER %s\r\n' % self.config['username'], sock.encode('utf-8'), send.encode('utf-8'))
sock.send('PASS %s\r\n' % self.config['oauth_password'])
sock.send('NICK %s\r\n' % self.config['username'])
if self.check_login_status(sock.recv(1024)):
pp('Login successful.')
else:
pp('Login unsuccessful. (hint: make sure your oauth token is set in self.config/self.config.py).', 'error')
sys.exit()
我的serve.py在这里:
from sys import argv
from src.bot import *
from src.config.config import *
bot = PartyMachine(config).sock()
它始终以“AttributeError 'nonetype' object has no attribute 'recv'
”失败。怎么会这样 ?
答案 0 :(得分:0)
您的get_irc_socket_object(self)
可能是问题所在。您可以使用第self.socket = self.irc.get_irc_socket_object()
行来调用它。这意味着python期望函数get_irc_socket_object(self)
返回一个套接字对象,但你不返回任何内容(你只需编写self.sock = sock
,因为你使用self.socket
而没有做任何事情你的其余代码)。结果,函数返回none
,所以现在self.socket
只将其作为值。因此,当您调用recv时,您会收到错误
另外,请清理变量名称。在代码中经常使用Sock并且很难遵循。