- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试向我的 python IRC 机器人添加一个功能,当我在 IRC 上输入“join #channel-name”时,机器人将加入 channel 。
这是我的代码:
# IRC bot written by syrius
import socket
server = "irc.freenode.net" # IRC server
channel = "#syrius-test" # Channel
botnick = "syrius-bot" # Nickname of the bot
master = "syrius_" # Nickname of the bot's master
exitcode = "bye " + botnick #Text that we will use to make the bot quit
def ircwrite(message):
global ircsock
ircsock.send(str(message).encode('latin-1', 'ignore'))
def ping():
ircwrite("PONG :pingis\n")
def sendmsg(chan , msg):
ircwrite("PRIVMSG "+ chan +" :"+ msg +"\n")
def joinchan(channel):
ircsock.send(bytes("JOIN "+ channel + "\n"))
def join():
ircsock.send(bytes("JOIN %s"))
def hello():
ircwrite("PRIVMSG "+ channel +" :Hello!\n")
def quitting():
ircwrite("PRIVMSG "+ channel +" :Okay boss, leaving now.\n")
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))
ircwrite("USER "+ botnick +" "+ botnick +" "+ botnick +" :IRC bot coded by syrius.\n")
ircwrite("NICK "+ botnick +"\n")
joinchan(channel)
while 1:
ircmsg = ircsock.recv(2048).decode() # receive data from the server
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
print(ircmsg) # Here we print what's coming from the server
name = ircmsg.split('!',1)[0][1:] # We split out the name
if ircmsg.find(":Hello "+ botnick) != -1: # If we can find "Hello Mybot" it will call the function hello()
hello()
if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
ping()
if name.lower() == master.lower() and ircmsg.find(":quit " + botnick) != -1:
quitting()
ircsock.send(bytes("QUIT \n", "UTF-8"))
if name.lower() == master.lower() and ircmsg.find(":join %s") != -1:
join()
main()
当然,下面的代码是不正确的:
第23行:
def join():
ircsock.send(bytes("JOIN %s"))
第 56 行:
if name.lower() == master.lower() and ircmsg.find(":join %s") != -1:
join()
我想知道我应该在那里放什么,以便机器人可以加入 channel 。
任何帮助将不胜感激。
最佳答案
我发现这个解决方案存在一些问题,我的建议是您应该尝试使用 IRC 框架,例如 Pydle 或其他十亿个已经处理协议(protocol)的框架之一。
我看到的第一个问题是使用 latin-1 进行编码,通常您应该使用 utf-8,您也可以使用服务器在 RPL_ISUPPORT 回复的 CHARSET 中通告的任何内容,尽管这不再常见。沿着编码行,您还可以从 utf-8 解码 IRC 行,这样您就可以处理字符串而不是字节,只需在输出处重新编码即可。
下一个 IRC 问题将是您的行结束,IRC 消息应始终以 CR-LS(回车换行)结束,这将是\r\n 字符,而不仅仅是\n。
我想提到的最后一件事是你的字符串格式化,你应该使用 "JOIN {}".format(channel)
这是目前首选的字符串格式化方法,除非你使用 python 3.7 那么你将使用类似的 fstrings。
按照您现在尝试进行格式化的方式,您正在通过连接(例如“USER”+ channel
)来完成它,但您也尝试通过%使用旧式字符串格式化s。如果您想使用 %s 格式,正确的方法是 "JOIN %s"% (channel)
,尽管现在使用 .format 和 fstrings 是首选方法。
关于python - Python编写的IRC机器人——加入 channel 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46184196/
基于浏览器的 irc 客户端是否可以在不使用任何网关的情况下工作,即直接与 irc 服务器通信? 最佳答案 HTML5 WebSocket 无法连接到非 WebSocket 服务器。使用 WebSoc
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
在我的 IRC Bot 中,有一些命令我希望只能由我使用,并且我想检查以确保使用我的名字的任何人都被识别为 nickserv (idented)。但是,我无法找到一种简单的方法来确定这一点,这就是我在
我想在我的网站上实现聊天服务,想知道如何从头开始创建一个简单的基于浏览器的 IRC 客户端? 我有一个 linux box,可以在上面安装任何我想要的东西。 Google 找到了关于如何设置 IRC
我希望能够在不加入 IRC channel 的情况下从 IRC channel 获取用户列表及其详细信息,从而增加其用户数量。 所以不要发出 JOIN #chan然后 NAMES #chan要获取用户
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我正在尝试编写我自己的 IRC 服务器。我引用的是 IRC RFC,但它让我有点困惑。如果我严格遵守 RFC,我认为任何流行的客户端都不会与我的服务器一起工作,比如 mIRC。例如,RFC 规定服务器
我正在使用标准库中的 TcpStream 开发一个 IRC 机器人。 我能够阅读输入的所有行,但 IRC 服务器似乎没有响应我的身份请求。我以为我发送请求的时间太早了,所以我尝试在发送 IDENT 之
我想连接到 ExIrc 中的两台服务器使用 Elixir ,我找不到一个简单的解决方案。我对 elixir 还很陌生,我能做的就是使用“伞”来运行两个应用程序并使它们相互连接? (我想用一个应用程序连
这个数字不是 RFC 1459 的一部分,但我希望能够使用它以便在 IRC 机器人中正确堆叠和取消堆叠模式。有没有不发送这个的主流 IRC 服务器? 最佳答案 这是规范的可选建议,这意味着编写客户端的
我对此进行了广泛的搜索,只找到了特定于机器人的客户端。我知道它们基本相同,但是对于我想做的事情,我就是想不通。 我正在尝试编写一个 python 客户端,它表面上是纯粹而简单的。例如,当从命令行运行时
我正在使用 net.Socket 模块连接到 EFNET 并测试我的代码,以便向我的机器人添加重新连接事件。 我可以通过哪些方式来模拟机器人断开连接?问题是 2-3 周后就会断开连接 - 我添加了事件
require "socket" server = "irc.rizon.net" port = "6667" nick = "Ruby IRC Bot" channel = "#0x40" s =
我正在尝试编写一个函数,使用 Twisted Python 将 IRC channel 中的昵称列表打印到该 channel 。我该怎么做呢?我已经阅读了 API 文档,并且在该站点上只看到一个与我的
我有一个命令,我想将其限制在某些级别。我正在使用 unreal3.2.6 IRC。 我读到这个: 但我仍然不确定我可以做些什么来检查用户排名。 我想看看用户是否是“语音”或更高级别。什么可以查找用户,
我正在计划建立一个 IRC 网络并一直在探索各种选择。我打算使用Inspircd IRC Server 。以及运行 meteor 的网络服务器。是否可以使用 websockets 构建一个 Web I
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 8年前关闭。 Improve this qu
如何获得 Weechat 配置变量?例如,我可以设置配置: /set irc.servers.freenode.ssl on 如何检索我设置的值? 最佳答案 要检查配置变量的值,您可以这样做: /se
有没有办法告诉irssi我要自动加入当前打开的所有 channel ? 我想要类似的东西: /channel add -auto /save 最佳答案 我找到了答案here: /alias adda
我整天都在搜寻Google,并且正在寻找一种方法来编写具有不同颜色的文本,就像我在其他IRC channel 上经常看到的那样。我想通过基于CLI的irssi实现此目的。我发现了多种无法正常工作的方法
我是一名优秀的程序员,十分优秀!