IRC Bot Python |识别昵称

时间:2015-08-27 12:21:31

标签: python sockets bots irc identify

这是我的麻烦: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

提前致谢。

解决方案: 通过将连接函数设置为已发布,它是第一个被调用的东西 然后服务器在使用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()

谢谢 i \ OFF

1 个答案:

答案 0 :(得分:3)

您尚未创建套接字,因此无法建立连接。

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