IRC Bot Python |识别昵称



我的问题是:Bot加载IRC信息很好,但是当它应该标识自己时,它没有。

下面是代码的相关部分。我猜问题出在第9行,但我不知道为什么。

import socket
server = #ServerName
channel = #ChannelName
botnick = #BotName
password = #Password (string)
def connect(channel, password): # This function is used on connect.
  ircsock.send("PRIVMSG" + " :NICKSERV Identify " + password +"n") #Problem Here
  ircsock.send("JOIN "+ channel +"n")
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +":Just testing .n") # user authentication
ircsock.send("NICK "+ botnick +"n")
connect(channel, password) #Join the channel and identify the nick using the functions we previously defined

提前感谢。

解决方案:通过设置connect函数,它是第一个要调用的函数然后服务器试图在USER/NICK之前连接。

    def create_connection():
    global ircsock
    ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667
    ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +":This bot is a result of GStones mastery .n") # user authentication
    ircsock.send("NICK "+ botnick +"n") # here we actually assign the nick to the bot
    time.sleep(5)
    connect(channel, password) # Join the channel and identify the nick using the functions we previously defined
create_connection()

谢谢

您还没有创建套接字,所以连接不会建立。

ircsock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)创建套接字。

另外,在使用ircsock.send()之前,您需要连接到服务器。

ircsock.connect ((server, serverPort))连接服务器。这个例子应该可以工作:

import socket
server = #ServerName
serverPort = #Server Port number
channel = #ChannelName
botnick = #BotName
password = #Password (string)
def connect(channel, password): # This function is used on connect.
  ircsock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
  ircsock.connect ((server, serverPort))
  ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +":Just testing .n") # user authentication
  ircsock.send("NICK "+ botnick +"n")
  ircsock.send("PRIVMSG" + " NICKSERV :identify " + password +"n") #Problem Here
  ircsock.send("JOIN "+ channel +"n")
connect(channel, password) #Join the channel and identify the nick using the functions we previously defined

最新更新