2

Basically, I want to run the connect function but I keep getting the CMD error message 'class StraussBot has no attribute 'connectSock' but I can obviously see it does. I've tried searching on here and I can't find any resolutions to this issue. SO it will be greatly appreciated if you could help me find why this isn't finding the 'connectSock' function.

Code:

import socket
 from config import HOST, PORT, CHANNEL
# User Info
USER = "straussbot" # The bots username
PASS = "oauth:sj175lp884ji5c9las089sm9vvaklf" # The auth code
class StraussBot:
 def __init__(self):
 self.Ssock = socket.socket()
 def connectSock(self):
 self.Ssock.connect((HOST, PORT))
 self.Ssock.send(str("Pass " + PASS + "\r\n").encode('UTF-8'))
 self.Ssock.send(str("NICK " + USER + "\r\n").encode('UTF-8'))
 self.Ssock.send(str("JOIN " + CHANNEL + "\r\n").encode('UTF-8'))
if __name__ == "__main__":
 print "Starting the bot..."
 while True:
 straussbot = StraussBot
 try:
 straussbot.connectSock()
 except Exception as e:
 print e
asked Jan 21, 2016 at 18:28
2
  • straussbot = StraussBot(), you're missing parens. Commented Jan 21, 2016 at 18:31
  • In your main block, you're setting straussbot to the class StraussBot, not an instance of the class. Commented Jan 21, 2016 at 18:31

3 Answers 3

2

You are getting confused by the error here. You get an AttributeError for self.Ssock because you do not have an instance.

You only created a reference to the class here:

straussbot = StraussBot

You need to call the class to produce an instance:

straussbot = StraussBot()

You are also mixing tabs and spaces:

tabs in source

Note how lines 5 through 9 have lines in the indentation, but the rest have dots? Those are tabs, and Python sees those as 8 spaces. So your connectSock method is indented inside of __init__ and not seen as a method on StrausBot.

You'll have to stick to either just tabs or just spaces. Python's styleguide strongly recommends you use spaces only.

answered Jan 21, 2016 at 18:31
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to instantiate an object of your class StraussBot.

straussbot = StraussBot

just assigns the name straussbot to refer to the class StraussBot. Change that line to

straussbot = StraussBot()

to actually create an instance of your class. You can then call the connectSock method on that instance as expected.

answered Jan 21, 2016 at 18:31

Comments

0

You've mixed tabs and spaces. You might think your StraussBot class has a connectSock method, but you actually put the definition of connectSock nested inside the __init__ method.

Turn on "show whitespace" in your editor to see the problem. There's probably a "convert tabs to spaces" option you can use to autofix it. Running Python with the -tt option will make Python notify you when something like this happens.

Also, you'll need to actually create an instance of StraussBot, rather than just setting straussbot to the class itself: straussbot = StraussBot().

answered Jan 21, 2016 at 18:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.