Python IR C Bot: как подключиться - PullRequest
0 голосов
/ 15 марта 2020

Я работал над тем, чтобы заставить бота присоединиться к комнате, но я не уверен, что происходит в данный момент. Может кто-нибудь уточнить, как я заставляю своего бота входить в комнату ir c?

Он успешно подключился к серверу один раз, но не изменил ничего, кроме оператора печати, при следующем запуске он выдал следующую ошибку ": abyss.no.eu.darkmyst.org 451 *: Вы не зарегистрированы"

import socket #Socket is used for connecting an communicating over a network port

ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = "irc.darkmyst.org"
channel = "#Cerebus"
botnick = "Cerebus"
godKingName = "userName"
onExit = "bye"

ircsock.connect((server,6667)) #Connecting through port 6697, SSL connection
ircsock.send(bytes("NICK :"+ botnick +"\r\n", "UTF-8")) # assign the nick to the bot
ircsock.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick + " " + botnick + "\r\n", "UTF-8"))


#Specification of encoding is necessary
def joinchan(chan): #To handle multiple channels, we will be implementing this with a function
    ircsock.send(bytes("JOIN "+ channel +"\n", "UTF-8"))
    ircmsg = ""
    while ircmsg.find("End of /NAMES list.") == -1: #This ensures our bot doesn't fire too early. Succesful chan join upon this requirement
        if ircmsg.find("PING :") != -1:
            ircmsg = ircmsg.split(":", 3)
            ircmsg[3] = ircmsg[3].strip('\r\n')
            ping(ircmsg[3])
            return
        ircmsg = ircsock.recv(2048).decode("UTF-8")
        ircmsg = ircmsg.strip('\r\n')
        print(ircmsg)


def sendmsg(msg, target=channel): #Function to send messages
    ircsock(bytes("PRIVMSG " + target +" :"+ msg +"\n", "UTF-8"))

def ping(randString): # respond to server Pings.
    #The Server demands it's share of blood
    print(randString)
    ircsock.send(bytes("PONG :" + str(randString) +"\r\n", "UTF-8"))
    return

def main():
    print("Joining Channel")
    joinchan(channel)
    while 1:
        print("Begin")
        ircmsg = ircsock.recv(2048).decode("UTF-8") #Receive up to 2048 bytes and decode as UTF-8
        ircmsg = ircmsg.strip('\r\n') #Get rid of special chars
        print(ircmsg)
        if ircmsg.find("PRIVMSG") != -1:
            name = ircmsg.split('!',1)[0][1:]
            message = ircmsg.split('PRIVMSG',1)[1].split(':',1)[1]

        if len(name) < 17:
            if message.find('Hi ' + botnick) != -1:
                sendmsg("Hello " + name + "!")
            if message[:5].find('.tell') != -1:
                target = message.split(' ', 1)[1]
                if target.find(' ') != -1:
                    message = target.split(' ', 1)[1]
                    target = target.split(' ')[0]
                else:
                    target = name
                    message = "Could not parse. The message should be in the format of ‘.tell [target] [message]’ to work properly."
                sendmsg(message, target)

            if name.lower() == godKingName.lower() and message.rstrip() == onExit:
                sendmsg("oh...okay. :'(")
                ircsock.send(bytes("QUIT n", "UTF-8"))
                return
            else:

                if ircmsg.find("PING :") != -1:
                    print("Ping Found")
                    ircmsg = ircmsg[6:]
                    ping(ircmsg)

        print("Endline")

main()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...