-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialogpt_irc.py
117 lines (91 loc) · 3.52 KB
/
dialogpt_irc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#! /usr/bin/env python3
"""
It also responds to DCC CHAT invitations and echos data sent in such
sessions.
dcc -- Let the bot invite you to a DCC CHAT connection.
"""
import random
from time import time, sleep
import textwrap
import irc.bot
import irc.strings
from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
from some_response import i_am_1, i_am_2, i_am_3, die
class DialogptIrcBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, realname, server, port=6667):
super().__init__([(server, port)], nickname, realname)
self.channel = channel
self.question = ""
self.response = "I'm stupid"
# #self.response_old = "Je suis un bug à la 1ère question"
# #self.t_block = time()
self.num = 0
self.quest_rep = {}
self.alive = 1
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
print("on_nicknameinuse")
def on_welcome(self, c, e):
c.join(self.channel)
print("Welcome on #lalabomedia IRC")
def on_pubmsg(self, c, e):
# a est le messge reçu
msg = e.arguments[0].split(":", 1)
i_am = irc.strings.lower(self.connection.get_nickname())
# Si le message commence par "TheGeneral: "
if len(msg) > 1 and irc.strings.lower(msg[0]) == i_am:
# La commande est la suite de "TheGeneral: texte_du_message
self.do_command(e, msg[1].strip())
def do_command(self, e, cmd):
if "quoi?" in cmd:
self.alive = 0
sleep(1)
self.die()
elif "who are you" in cmd:
self.send_pubmsg(["who are you ?", "i_am"])
elif "comment te détruire" in cmd:
text = "Pour me détruire, posez-moi la question: quoi?"
self.send_pubmsg(["comment te détruire ?", text])
else:
self.question = cmd.lower()
self.quest_rep[self.num] = [self.question]
while len(self.quest_rep[self.num]) == 1:
sleep(0.01)
if len(self.quest_rep[self.num]) == 2:
msg = [self.quest_rep[self.num][0],
self.quest_rep[self.num][1]]
self.send_pubmsg(msg)
self.num += 1
def send_pubmsg(self, msg):
if msg[1] == "i_am":
self.connection.privmsg("#lalabomedia", i_am_1)
sleep(0.3)
self.connection.privmsg("#lalabomedia", i_am_2)
sleep(0.3)
self.connection.privmsg("#lalabomedia", i_am_3)
else:
self.connection.privmsg("#lalabomedia", "Q: " + msg[0])
sleep(0.1)
self.connection.privmsg("#lalabomedia", "R: " + msg[1])
def dialogpt_irc_bot_main():
server = "irc.libera.chat"
port = 6667
channel = "#lalabomedia"
nickname = "TheGeneral"
realname = "IA Computer at The Prisoner"
bot = DialogptIrcBot(channel, nickname, realname, server, port)
bot.start()
if __name__ == "__main__":
dialogpt_irc_bot_main()
# #t = time()
# #if t - self.t_block > 5:
# #self.do_command(e, msg[1].strip())
# #self.t_block = t
# #else:
# #self.send_pubmsg("Je ne réponds pas à: " + msg[1].strip())
# #if len(msg[1]) > 460:
# #wrapper = textwrap.TextWrapper(width=460)
# #text = wrapper.fill(text=msg[1])
# #lines = text.splitlines()
# #for line in lines:
# #self.connection.privmsg("#labomedia", line)