【问题标题】:Low-level socket Error; irc.chat-Bot gives back an Attribute error低级套接字错误; irc.chat-Bot 返回属性错误
【发布时间】:2021-11-14 13:03:43
【问题描述】:

各位错误搜索者,您好,

因此,我尝试使用准系统套接字导入连接到 twitch 服务器,因为我不想导入模块,而是想学习一些基础知识,无论如何,它会以如下的对象归属错误终止;

line 27, in connect
    self.irc.connect((self.irc_server, self.irc_port))
AttributeError: 'Bot' object has no attribute 'irc_server'

我似乎无法找到错误,非常感谢您的帮助,我从昨天开始一直在寻找我的错误,并且它开始受到严重伤害。

发生错误的部分;

def connect(self):
    self.irc = socket.socket()
    self.irc.connect((self.irc_server, self.irc_port))
    self.send_command(f"PASS {self.oauth_token}")
    self.send_command(f"NICK {self.username}")
    for channel in self.channels:
        self.send_command(f"JOIN #{channel}")
        self.send_privmsg(channel, "Hey there!")
    self.loop_for_messages() 

完整代码;

import socket

oauth_token = "oauth:notmyrealauthtoken"


class Bot:
    def _init__(self):
        self.irc_server = "irc.twitch.tv"
        self.irc_port = 6667
        self.oauth_token = oauth_token
        self.username = "slimkimun"
        self.channels = ["slimkimun"]


    def send_privmsg(self, channel, text):
        self.send_command(f"PRIVMSG #{channel} :{text}")


    def send_command(self, command):
        if "PASS" not in command:
            print(f"< {command}")
        self.irc.send((command + "\r\n").encode())


    def connect(self):
        self.irc = socket.socket()
        self.irc.connect((self.irc_server, self.irc_port))
        self.send_command(f"PASS {self.oauth_token}")
        self.send_command(f"NICK {self.username}")
        for channel in self.channels:
            self.send_command(f"JOIN #{channel}")
            self.send_privmsg(channel, "Hey there!")
        self.loop_for_messages()


    def handle_message(self, reveive_msg):
        print(f"> {received_msg}")


    def loop_for_messages(self):
        while True:
            received_msgs = self.irc.recv(2048).decode()
            for received_msg in received_msgs.split("\r\n"):
                self.handle_message(received_msg)


def main():
    bot = Bot()
    bot.connect()

if __name__ == "__main__":
    main()

【问题讨论】:

  • 你会讨厌这个的。 __init__ 上缺少下划线

标签: python bots irc


【解决方案1】:

一般来说可能不太有用,但是您的类没有被初始化,因为您拼错了__init__

这至少会让你通过第一个错误:

改变

def _init__(self):

def __init__(self):

【讨论】:

  • 非常感谢我的人!它帮助并解决了错误,我昨天搜索了几个小时。先生,您是我今晚的英雄,谢谢。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 2016-03-30
  • 2018-06-12
相关资源
最近更新 更多