The QML bot example is a simplified version of the bot example, written in QML. See the QML compatibility article for more QML specific details.
The QML bot example in action
The following snippet illustrates how IrcConnection is prepared and opened, and how a command is queued to automatically join a channel when connected.
1 connection: IrcConnection {
4 host: "irc.freenode.net"
6 nickName: "QmlBot" + Math.round(Math.random() * 9999)
7 realName: qsTr("Communi %1 QML bot example").arg(irc.version())
9 Component.onCompleted: {
10 // queue a command to automatically join a channel when connected
11 sendCommand(command.createJoin(channel))
The example utilizes IrcCommandParser for parsing commands from messages received from other clients. In order to take the parser in use, the supported commands must be teached, as illustrated below. Some of the commands are context sensitive, and the parser must be therefore kept aware of the list of channels the bot is on. This is easily achieved by binding the IrcCommandParser::channels property to the value of the IrcBufferModel::channels property. Furthermore, like a typical IRC bot, the commands are triggered using a slightly different syntax in queries and channels.
1 property IrcCommandParser parser: IrcCommandParser {
4 // keep the parser aware of the context
5 channels: model.channels
7 // - on channel, respond to: "!<command> <params>" and "bot: <command> <params>"
8 // - in query, respond to: "!<command> <params>" and "<command> <params>"
9 triggers: connection.network.isChannel(target) ? ["!", connection.nickName + ":"] : ["!", ""]
11 Component.onCompleted: {
12 // teach the bot some commands
13 parser.addCommand(IrcCommand.Nick, "NICK <nick>");
14 parser.addCommand(IrcCommand.Join, "JOIN <#channel> (<key>)");
15 parser.addCommand(IrcCommand.Part, "PART (<#channel>) (<message...>)");
16 parser.addCommand(IrcCommand.Quit, "QUIT (<message...>)");
17 parser.addCommand(IrcCommand.Message, "SAY [target] <message...>");
The bot listens to incoming messages via IrcConnection::messageReceived(). The current target is chosen based on whether the incoming message is a channel or a private message. Each message content is parsed for commands that were taught earlier.
2 if (message.type === IrcMessage.Private) {
3 // - in private, reply to the message sender
4 // - on channel, reply to the target channel
5 parser.target = message.private ? message.nick : message.target
7 var command = parser.parse(message.content)
9 // send the command to the IRC server
11 if (command.type === IrcCommand.Quit) {
12 // close the connection & quit the app when a !quit command was received
Files: